Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Update Business process flow stage using custom workflow

Posted on by 900

I am using below mentioned custom work flow code for updating BPF stage automatically. I have bind my custom workflow with CRM standard work flow.

1. Code :

  public void UpdateBusinessProcessStage(rmContext dbContext, IOrganizationService service, Account objAccount)
        {
            // Get the Process Id based on the process name
            string procesName = "Create Account From NAV";
            var workflow = (from p in dbContext.WorkflowSet
                            where p.Name == procesName && p.StateCode == WorkflowState.Activated
                            select p).FirstOrDefault();

            if (workflow == null)
            {
                throw new InvalidPluginExecutionException(string.Format("Worflow not found with name {0}", procesName));
            }
            //Get the stage id based on the Stage Name
            string stageName = "Qualified Prospect";
            var stage = (from p in dbContext.ProcessStageSet
                         where p.StageName == stageName && p.ProcessId.Id == workflow.WorkflowId
                         select p).FirstOrDefault();

            //foreach (var item in dbContext.ProcessStageSet)
            //{
            //    stageName += item.StageName + "-" + item.Id;
            //}

            if (stage == null)
            {
                throw new InvalidPluginExecutionException(string.Format("Stage not found with name {0}", stageName));
            }
            //Update the record with the new stage
            //Entities.new_enquiry updateEnquiry = new Entities.new_enquiry();
            // updateEnquiry.Id = context.PrimaryEntityId;
            
            //service.Update(updateEnquiry);

            //QueryExpression queryStage = new QueryExpression { EntityName = "processstage" };
            //queryStage.ColumnSet = new ColumnSet("stagename", "processid");
            //queryStage.Criteria.AddCondition("stagename", ConditionOperator.Equal, stageName);
            //queryStage.Criteria.AddCondition("processid", ConditionOperator.Equal, workflow.WorkflowId);
            //EntityCollection stages = service.RetrieveMultiple(queryStage);

            objAccount.StageId = stage.Id;
            objAccount.ProcessId = workflow.WorkflowId;
            service.Update(objAccount);

        }

This code is not working. Please let me know if any correction in this code.

2. I got stage name and directly assigned to Entity stageid but it didn't work for me.

Please do needful if any one have solution for this issue.

*This post is locked for comments

  • Suggested answer
    keyur7379 Profile Picture
    keyur7379 900 on at
    RE: Update Business process flow stage using custom workflow

    Please check code for updating BPF stage. This code shows how to skip BPF stage and update stage. 

    Ex. Assume , I have four business process stage.

    1. Suspect

    2. Prospect

    3. Qualified Prospect

    4. Customer

    I need to move 2nd stage(Prospect) directly when record is created. For this we need to skip 1st stage . I had used TraversedPath for skipping the stage. This is a default field of business process flow. Please check how to use traversedPath.

    #region Update BPF stage of Account

                               ProcessStage p2 = dbContext.ProcessStageSet.Where(c => c.StageName == "Prospect" && c.ProcessId.Id == objAccountUpdate.ProcessId).FirstOrDefault();

                               //OptionSetValue statusCode = crmAccountByCompanyCode.StatusCode;

                               objAccountUpdate.StageId = p2.ProcessStageId;

                               objAccountUpdate.TraversedPath += "," + p2.ProcessStageId;

                               dbContext.ClearChanges();

                               dbContext.Attach(objAccountUpdate);

                               dbContext.UpdateObject(objAccountUpdate);

                               dbContext.SaveChanges();

                               [tag:endregion]

  • Andre Margono Profile Picture
    Andre Margono 2,602 on at
    RE: Update Business process flow stage using custom workflow

    Based on the error message and the source code, it means the process stage doesn't exist. Make sure that the process stage is not a typo.

    And on this condition: "p.ProcessId.Id == workflow.WorkflowId", have a try to compare the Id with Id, "p.ProcessId.Id == workflow.WorkflowId.Id". As i think workflow.WorkflowId is an entity reference, thus not equal to the Id.

  • keyur7379 Profile Picture
    keyur7379 900 on at
    RE: Update Business process flow stage using custom workflow

    hey Andre ,

    1. what error message or any other information of "not working" that you could share?

    Stage not found with name "Qualified Prospect". 

    2. what version of Dynamics 365/CRM are you on?

    Dynamic 365 latest version.

    Actually I need to create record(Account) from third party application. I have created one standard workflow and custom work flow . Custom work call to web service to get third party application data. If i created record (account) manually from my instance(Web CRM) then its by default BPF stage is stage 1 (Suspect) but when I create record using custom work flow then It will be directly in BFP third stage (Qualified Prospect) as par my requirement. 

    Now hope you can clear ..

  • Andre Margono Profile Picture
    Andre Margono 2,602 on at
    RE: Update Business process flow stage using custom workflow

    what error message or any other information of "not working" that you could share?

    And what version of Dynamics 365/CRM are you on? Dynamics 365 introduced a new design of Business Process Flow.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans