Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

Plugin Assistance (Create & Update Messages)

$
0
0

Hello I am new to CRM Development (If anyone know if any helpful resources to deep dive into this that would be appreciated!) .. I am trying to create a plugin for both create and update of a record. The logic is simply: Look to see if an Opportunity record exist with the same 2 Account lookups (Called Member - Lookup Field 1 & Vendor - Lookup Field 2) based on the current record which is a custom entity called Purchase.

The code is getting the member and vendor guids but error out after that .. This is the message in the error log:

Entered Purchase.PluginsPurchase.PrePurchasesCreate.Execute(), Correlation Id: 42a3142e-a615-4ea5-a986-5d35e10fcad3, Initiating User: ca299dee-6a00-4fde-942f-d28812d2fe19
Purchase.PluginsPurchase.PrePurchasesCreate is firing for Entity: new_purchases, Message: Create, Correlation Id: 42a3142e-a615-4ea5-a986-5d35e10fcad3, Initiating User: ca299dee-6a00-4fde-942f-d28812d2fe19
memberID = 34cb1bd5-1068-e611-80d7-fc15b428cc94
vendorID = 4be4391d-1168-e611-80d7-fc15b428cc94
Exiting Purchase.PluginsPurchase.PrePurchasesCreate.Execute(), Correlation Id: 42a3142e-a615-4ea5-a986-5d35e10fcad3, Initiating User: ca299dee-6a00-4fde-942f-d28812d2fe19

Any assistance to get this running? Also how can I then modify this for an update event (this is code is for a create event):

protected void ExecutePrePurchasesCreate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }

            OrganizationServiceContext orgService = new OrganizationServiceContext(localContext.OrganizationService);

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
            ITracingService traceService = localContext.TracingService;

            //if (context.Depth > 1) { return; }

            Entity current = null;

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

                if (current.LogicalName == "new_purchases")
                {
                    try
                    {
                        // Get current Member and Vendor from purchase entity
                        Guid memberID = Guid.Empty;
                        Guid vendorID = Guid.Empty;

                        if (current.Contains("new_member"))
                        {
                            memberID = ((EntityReference)current["new_member"]).Id;
                            traceService.Trace("memberID = " + memberID.ToString());
                        }

                        if (current.Contains("new_vendor"))
                        {
                            vendorID = ((EntityReference)current["new_vendor"]).Id;
                            traceService.Trace("vendorID = " + vendorID.ToString());
                        }
                       
                        // find the match fields in oppoturnity
                        var lqOpp = (from o in orgService.CreateQuery("opportunity")
                                     where (Guid)o["new_member"] == memberID &&
                                          (Guid)o["new_vendor"] == vendorID &&
                                          (int)o["new_OpportunityType"] != 100000001 &&
                                          (int)o["statecode"] == 1
                                    orderby o["createdon"] descending
                                    select o).ToList();
                        if (lqOpp.Count > 0)
                        {
                            //This is a match and store value into sik_relatedopportunity field
                            traceService.Trace("This is a match, store value into new_opportunity field");
                            current["new_opportunity"] = new EntityReference("opportunity", lqOpp.First().Id); 
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                    }
                }
            }   
        }
    }
}


Viewing all articles
Browse latest Browse all 46379

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>