Monday, February 19, 2018

Get the Created Records when Lead Gets Qualified - Microsoft dynamics CRM

Hi All,

Once Lead Gets Qualify, as per oob behavior it will automatically create opportunity, contact and accounts.

As per my logic whenever an opportunity gets created need to set created lead id to custom opportunity field.

Plugin Step Creation:


Register Plugin as Asynchronously


            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            OrganizationServiceContext orgContext = new OrganizationServiceContext(service);

EntityReference getLeadid = (EntityReference)context.InputParameters["LeadId"];
Entity lead = service.Retrieve("lead", leadid.Id, new ColumnSet(true));

foreach (EntityReference CreatedEntityFromLead in ((IEnumerable)context.OutputParameters["CreatedEntities"]))
                {
                    if (CreatedEntityFromLead.LogicalName == "opportunity")
                    {
                        Entity entOpportunity = service.Retrieve("opportunity", CreatedEntityFromLead.Id, new ColumnSet(true));
                         entOpportunity["new_customLookupid"] = erLookUpField;
                        service.Update(entOpportunity);
                    }

                }




No comments:

Post a Comment

How to Remove Unmanged layers in Microsoft Dynamics CRM/Dataverse

 Issues Faced : When we need to create a new field or update a failed, we are having an different environment we are create in that and move...