web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plugin Assistance (Create & Update Messages)

(0) ShareShare
ReportReport
Posted on by

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);
                    }
                }
            }   
        }
    }
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I believe you have issues with your select query. Here is updated version:

                           var lqOpp = (from o in orgService.CreateQuery("opportunity")

                                        where o.Contains("new_member") && o.GetAttributeValue<EntityReference>("new_member").Id == memberID &&

                                             o.Contains("new_vendor") && o.GetAttributeValue<EntityReference>("new_vendor").Id == vendorID &&

                                             (!o.Contains("new_opportunitytype") || o.GetAttribute<OptionSetValue>("new_opportunitytype").Value != 100000001) &&

                                             o.GetAttributeValue<OptionSetValue>("statecode").Value == 1

                                       orderby o.GetAttributeValue<DateTime>("createdon") descending

                                       select o).ToList();

    Also I would suggest you how to learn how to troubleshoot plugins. Here is article that shows how to do that - blogs.msdn.microsoft.com/.../debug-crm-online-plugins

  • Community Member Profile Picture
    on at

    Having issues with the GetAttribute portion of your code ..Should it be GetAttributeValue ?

    (!o.Contains("new_opportunitytype") || o.GetAttribute<OptionSetValue>("new_opportunitytype").Value != 100000001) &&

  • Community Member Profile Picture
    on at

    Error Log:

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred in the plug-in.Detail:

    <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance&quot; xmlns="schemas.microsoft.com/.../Contracts&quot;>

     <ErrorCode>-2147220891</ErrorCode>

     <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic&quot;>

       <KeyValuePairOfstringanyType>

         <d2p1:key>OperationStatus</d2p1:key>

         <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema&quot; i:type="d4p1:string">0</d2p1:value>

       </KeyValuePairOfstringanyType>

       <KeyValuePairOfstringanyType>

         <d2p1:key>SubErrorCode</d2p1:key>

         <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema&quot; i:type="d4p1:string">-2146233088</d2p1:value>

       </KeyValuePairOfstringanyType>

     </ErrorDetails>

     <Message>An error occurred in the plug-in.</Message>

     <Timestamp>2016-08-26T16:56:21.353415Z</Timestamp>

     <InnerFault i:nil="true" />

     <TraceText>

    [Purchase.PluginsPurchase: Purchase.PluginsPurchase.PrePurchasesCreate]

    [57f3ed3e-5c5e-e611-80da-c4346bac1b18: PrePurchasesCreate]

    Entered Purchase.PluginsPurchase.PrePurchasesCreate.Execute(), Correlation Id: 5bdb4737-1abb-4249-9388-9cb94e4db69a, Initiating User: ca299dee-6a00-4fde-942f-d28812d2fe19

    Purchase.PluginsPurchase.PrePurchasesCreate is firing for Entity: new_purchases, Message: Create, Correlation Id: 5bdb4737-1abb-4249-9388-9cb94e4db69a, 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: 5bdb4737-1abb-4249-9388-9cb94e4db69a, Initiating User: ca299dee-6a00-4fde-942f-d28812d2fe19

    </TraceText>

    </OrganizationServiceFault>

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Yes. It should be GetAttributeValue.

    Regarding why your plugin throws an error - just troubleshoot it using VS. I believe I posted url already.

  • Community Member Profile Picture
    on at

    I have also noticed when it is registered as Pre-Operation I received the Business Process Error but when it is Post-Operation nothing happens

  • Community Member Profile Picture
    on at

    Yes I have tried using the Plugin Profiler but it just says Running in VS .. Maybe I don't know where to put the breakpoints, the video didn't really speak that.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    That's because obviously you use CRM Developer Toolkit and when you created plugin you've chosen Pre-Operation. That's why plugin is not even executed when you re-register it to fire in Post-Operation mode.

  • Community Member Profile Picture
    on at

    I see .. I tried the Plugin Profiler, it looks like the value is null but there should be a record returning in the query

    2251.Debug.PNG

  • Community Member Profile Picture
    on at

    I got it to work .. I just retyped my code and redeployed .. Not sure what happen there but that was strange. Can I use the same code for the Update message?

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    I believe so.

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans