<?xml version="1.0" encoding="UTF-8"?>
<Flow xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>63.0</apiVersion>
    <areMetricsLoggedToDataCloud>false</areMetricsLoggedToDataCloud>
    <assignments>
        <name>Assign_Ids_to_Collection</name>
        <label>Assign Ids to Collection</label>
        <locationX>138</locationX>
        <locationY>611</locationY>
        <assignmentItems>
            <assignToReference>PermissionsIds</assignToReference>
            <operator>Add</operator>
            <value>
                <elementReference>Loop_Permissions.Id</elementReference>
            </value>
        </assignmentItems>
        <connector>
            <targetReference>Loop_Permissions</targetReference>
        </connector>
    </assignments>
    <customErrors>
        <name>Throw_Error</name>
        <label>Throw Error</label>
        <locationX>182</locationX>
        <locationY>1019</locationY>
        <customErrorMessages>
            <errorMessage>The selected user doesn’t have Content Acquisition permissions to be the owner of this Opportunity.</errorMessage>
            <fieldSelection>OwnerId</fieldSelection>
            <isFieldError>true</isFieldError>
        </customErrorMessages>
    </customErrors>
    <decisions>
        <name>Is_CA_RecordType</name>
        <label>Is CA RecordType?</label>
        <locationX>248</locationX>
        <locationY>287</locationY>
        <defaultConnectorLabel>No</defaultConnectorLabel>
        <rules>
            <name>Yes</name>
            <conditionLogic>and</conditionLogic>
            <conditions>
                <leftValueReference>$Record.RecordType.Name</leftValueReference>
                <operator>StartsWith</operator>
                <rightValue>
                    <stringValue>Content Acquisition</stringValue>
                </rightValue>
            </conditions>
            <connector>
                <targetReference>Get_CA_Edit_All_PS</targetReference>
            </connector>
            <label>Yes</label>
        </rules>
    </decisions>
    <decisions>
        <name>Owner_has_PS</name>
        <label>Owner has PS?</label>
        <locationX>50</locationX>
        <locationY>911</locationY>
        <defaultConnector>
            <targetReference>Throw_Error</targetReference>
        </defaultConnector>
        <defaultConnectorLabel>No</defaultConnectorLabel>
        <rules>
            <name>Yes2</name>
            <conditionLogic>and</conditionLogic>
            <conditions>
                <leftValueReference>PSA_for_New_Owner</leftValueReference>
                <operator>IsNull</operator>
                <rightValue>
                    <booleanValue>false</booleanValue>
                </rightValue>
            </conditions>
            <label>Yes</label>
        </rules>
    </decisions>
    <environments>Default</environments>
    <interviewLabel>CA - {!$Flow.CurrentDateTime}</interviewLabel>
    <label>CA - Validate Owner Changes</label>
    <loops>
        <name>Loop_Permissions</name>
        <label>Loop Permissions</label>
        <locationX>50</locationX>
        <locationY>503</locationY>
        <collectionReference>Get_CA_Edit_All_PS</collectionReference>
        <iterationOrder>Asc</iterationOrder>
        <nextValueConnector>
            <targetReference>Assign_Ids_to_Collection</targetReference>
        </nextValueConnector>
        <noMoreValuesConnector>
            <targetReference>PSA_for_New_Owner</targetReference>
        </noMoreValuesConnector>
    </loops>
    <processMetadataValues>
        <name>BuilderType</name>
        <value>
            <stringValue>LightningFlowBuilder</stringValue>
        </value>
    </processMetadataValues>
    <processMetadataValues>
        <name>CanvasMode</name>
        <value>
            <stringValue>AUTO_LAYOUT_CANVAS</stringValue>
        </value>
    </processMetadataValues>
    <processMetadataValues>
        <name>OriginBuilderType</name>
        <value>
            <stringValue>LightningFlowBuilder</stringValue>
        </value>
    </processMetadataValues>
    <processType>AutoLaunchedFlow</processType>
    <recordLookups>
        <name>Get_CA_Edit_All_PS</name>
        <label>Get CA Edit All PS</label>
        <locationX>50</locationX>
        <locationY>395</locationY>
        <assignNullValuesIfNoRecordsFound>false</assignNullValuesIfNoRecordsFound>
        <connector>
            <targetReference>Loop_Permissions</targetReference>
        </connector>
        <filterLogic>and</filterLogic>
        <filters>
            <field>Label</field>
            <operator>StartsWith</operator>
            <value>
                <stringValue>Content Acquisition Edit All</stringValue>
            </value>
        </filters>
        <getFirstRecordOnly>false</getFirstRecordOnly>
        <object>PermissionSet</object>
        <storeOutputAutomatically>true</storeOutputAutomatically>
    </recordLookups>
    <recordLookups>
        <name>PSA_for_New_Owner</name>
        <label>PSA for New Owner</label>
        <locationX>50</locationX>
        <locationY>803</locationY>
        <assignNullValuesIfNoRecordsFound>false</assignNullValuesIfNoRecordsFound>
        <connector>
            <targetReference>Owner_has_PS</targetReference>
        </connector>
        <filterLogic>and</filterLogic>
        <filters>
            <field>AssigneeId</field>
            <operator>EqualTo</operator>
            <value>
                <elementReference>$Record.OwnerId</elementReference>
            </value>
        </filters>
        <filters>
            <field>PermissionSetId</field>
            <operator>In</operator>
            <value>
                <elementReference>PermissionsIds</elementReference>
            </value>
        </filters>
        <getFirstRecordOnly>true</getFirstRecordOnly>
        <object>PermissionSetAssignment</object>
        <storeOutputAutomatically>true</storeOutputAutomatically>
    </recordLookups>
    <start>
        <locationX>122</locationX>
        <locationY>0</locationY>
        <connector>
            <targetReference>Is_CA_RecordType</targetReference>
        </connector>
        <filterLogic>and</filterLogic>
        <filters>
            <field>OwnerId</field>
            <operator>IsChanged</operator>
            <value>
                <booleanValue>true</booleanValue>
            </value>
        </filters>
        <filters>
            <field>Is_Reverted_to_Lead__c</field>
            <operator>EqualTo</operator>
            <value>
                <booleanValue>false</booleanValue>
            </value>
        </filters>
        <object>Opportunity</object>
        <recordTriggerType>Update</recordTriggerType>
        <triggerType>RecordBeforeSave</triggerType>
    </start>
    <status>Active</status>
    <variables>
        <name>PermissionsIds</name>
        <dataType>String</dataType>
        <isCollection>true</isCollection>
        <isInput>false</isInput>
        <isOutput>false</isOutput>
    </variables>
</Flow>
