How to Create a Business Process Flow (BPF) on existing Contact and update the stage using conditions in D365 using Power Shell Script

 function UpdateProcessStage{

param(

[int]$desiredStagePosition,

$processInstance,

$retrieveActivePathResponse

)

$newStageId = $retrieveActivePathResponse.ProcessStages.Entities[$desiredStagePosition].Attributes["processstageid"]

$entity = [Microsoft.Xrm.Sdk.Entity]::new("APY_"+$processInstance["name"].ToLower(), $processInstance.Id)

$entity.Attributes.Add("activestageid", (New-CrmEntityReference -Id $newStageId -EntityLogicalName "processstage"))

$CRMConnection.Update($entity)

}

function SetBPFToFinish{

param(

$processInstance

)

Set-CrmRecordState -conn $CRMConnection -EntityLogicalName APY_Purushflow -Id $processInstance.Id -StateCode 1 -StatusCode 2

}

$conn = Get-CrmConnection -InteractiveMode

$pagingCookie=""

$page=1

$count=5000

do{

    $varFetch = Get-CrmRecordsByFetch -conn $CRMConnection -Fetch @"

   <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">

    <entity name="contact">

    <attribute name="fullname" />

    </entity>

    </fetch>

"@ -TopCount $count -PageNumber $page -PageCookie $pagingCookie

$pagingCookie=$varFetch.PagingCookie

$page++

foreach($record in $varfetch.CrmRecords){

$record=Get-CrmRecord contact $record.contactid * -IncludeNullValue

$parentReference = New-CrmEntityReference -EntityLogicalName APY_Purushflow -Id $record.contactid

$Purushflow1= New-CrmRecord -conn $CRMConnection -EntityLogicalName APY_Purushflow -Fields @{"bpf_contactid"=$parentReference}

$retrieveProcessInstancesRequest = [Microsoft.Crm.Sdk.Messages.RetrieveProcessInstancesRequest]::new()

$retrieveProcessInstancesRequest.EntityId = $record.contactid

$retrieveProcessInstancesRequest.EntityLogicalName = "contact"

$retrieveProcessInstancesResponse = $CRMConnection.Execute($retrieveProcessInstancesRequest)

$processInstance = $retrieveProcessInstancesResponse.Results.Values.Entities | Select -First 1


$retrieveActivePathRequest = [Microsoft.Crm.Sdk.Messages.RetrieveActivePathRequest]::new()

$retrieveActivePathRequest.ProcessInstanceId = $processInstance.Id

$retrieveActivePathResponse = $CRMConnection.Execute($retrieveActivePathRequest)


 $fullname=$record.fullname

$hashTable = [ordered]@{

          "Stage1" = '0'

          "Stage2" = '1'

          "Stage3" = '2'

}

$activeStagePosition=""

for($i = 0; $i -lt $retrieveActivePathResponse.ProcessStages.Entities.Count; ++$i) { 

    if($retrieveActivePathResponse.ProcessStages.Entities[$i].Attributes["processstageid"].ToString() -eq $processInstance.Attributes["processstageid"].ToString()) {

        $activeStageName = $retrieveActivePathResponse.ProcessStages.Entities[$i].Attributes["stagename"].ToString()

        $activeStagePosition = $i

    }

}

$processInstance["name"]= $processInstance["name"] -replace " ",""

        if(($fullname -ne $null) -and ($activeStagePosition -le $hashTable.'Stage0'))

                {

                    UpdateProcessStage $hashTable.'Stage1' $processInstance $retrieveActivePathResponse

                    SetBPFToFinish($processInstance);

                }

        elseif (($fullname -ne $null) -and ($activeStagePosition -le $hashTable.'Stage1'))

                {

                    UpdateProcessStage $hashTable.'Stage2' $processInstance $retrieveActivePathResponse

                }

        elseif (($fullname -ne $null) -and ($activeStagePosition -le $hashTable.'Stage2'))

                {

                    UpdateProcessStage $hashTable.'Stage3' $processInstance $retrieveActivePathResponse

                }

}

}while($varFetch.NextPage -ne $false)

Comments

Popular posts from this blog

Basic Plugin Code in D365 using C#

CURD (Create, Update, Retrieve and Delete) Operation in D365 using Power Shell Script

Meta Data Using WebApiRequest