0
votes

I want to make a plugin that inserts into my database the prospect's name after the Opportunity is created, and then save into the opportunity notes the id returned by my web service.

I managed to create and deploy a plugin to insert from a web service, but I do not know how get the data I want, and save the returned id. Can you help me?

Here's my code with dummy data to test the web service functionality, it inserts into my database after the opportunity is saved.

public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); // Obtain the execution context from the service provider.                
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); // Obtain the organization service reference.                
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.
                Entity entity = (Entity)context.InputParameters["Target"];

                // Verify that the target entity represents an opportunity.                   
                if (entity.LogicalName != "opportunity")
                    return;

                nom = "Jerry";
                app = "Seinfeld";
                apm = "Costanza";                                       

                crmPlugins.crmPlugInsert.WebReference.websCRM webService = new crmPlugins.crmPlugInsert.WebReference.websCRM();
                folioS = webService.Insert(nom, app, apm);
            }
        }
1

1 Answers

1
votes

If I understand your question correctly, Target entity will have the details, you have to pull the needed information & assign it in nom, app & apm while creating using web service.

Once it’s created you have the created Id in folioS, use it to create associated note in opportunity record using below code.

            Entity annotation = new Entity("annotation");

            annotation.Attributes["objectid"] = new EntityReference("opportunity", new Guid(entity.Id));
            annotation.Attributes["objecttypecode"] = "opportunity";
            annotation.Attributes["subject"] = "Prospect note";
            annotation.Attributes["notetext"] = folioS;

            crmService.Create(annotation);