Sunday 28 January 2018

how to update the Lookup value using custom workflow in Dynamic CRM.

how to update the Lookup value using custom workflow in Dynamic CRM.

Description:

In this example we explain that how to create custom workflow in Dynamic CRM.or how to update contact detail based on parent account using custom workflow in dynamic CRM.or updating Contact Info through Parent Account using custom workflow in Dynamic CRM.how to update the Lookup value using custom workflow in Dynamic CRM.

Here we demonstrate that sample create a custom workflow activity in Dynamic CRM and update the contact details
Custom Workflow:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;

namespace ContactWorkflow.Workflow
{
    public class changeContactwithAccountBasedonLeadAccount  : CodeActivity
    {
        protected override void Execute(CodeActivityContext executioncontext)
        {
            IWorkflowContext Context = executioncontext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executioncontext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(Context.UserId);
            ExecuteWorkflowRequest req = new ExecuteWorkflowRequest();
            Entity lead = (Entity)Context.InputParameters["Target"];
            lead = service.Retrieve(lead.LogicalName, lead.Id, new ColumnSet(true));
            //Start Get GUID
            Guid leadId = lead.Id;//this.InputLead.Get(executioncontext).Id;
            QueryExpression ContactlList = new QueryExpression("contact");
          
            ContactlList.Criteria.AddCondition(
          
               "originatingleadid",
                ConditionOperator.Equal,
                leadId
            );
            var contactDetail = service.RetrieveMultiple(ContactlList);

            int contactDetailCount = contactDetail.Entities.Count;

            List<Entity> Collection = contactDetail.Entities.ToList();
            for (int i = 0; i < contactDetailCount; i++)
            {
                Entity entity = Collection[i];
                if (entity.LogicalName == "contact")
                {
                  
                    EntityReference selectedAccount = (EntityReference)lead.Attributes["parentaccountid"];
                     entity["parentcustomerid"] = selectedAccount;
                     entity["new_contacttype"] =  new OptionSetValue(1);
                     service.Update(entity);

                   
                }
            }
        }
     

        [RequiredArgument]
        [Input("InputLead")]
        [ReferenceTarget("lead")]
        public InArgument<EntityReference> InputLead { get; set; }

        [RequiredArgument]
        [Input("Workflow")]
        [ReferenceTarget("workflow")]
        public InArgument<EntityReference> InputWorkflow { get; set; }
    }
}





This entry was posted in :

0 comments:

Post a Comment