locked
Error in CRM 2016 when creating Opportunity RRS feed

  • Question

  • I have a plugin triggered on the post create of an opportunity. I have not made any changes to it, and it is now producing the following error:

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: System.IO.FileLoadException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #51719786Detail: 
    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
      <ActivityId>bb418304-ae36-4713-a66e-6662fc13d09f</ActivityId>
      <ErrorCode>-2147220970</ErrorCode>
      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
      <Message>System.IO.FileLoadException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #51719786</Message>
      <Timestamp>2017-08-14T15:21:29.8585845Z</Timestamp>
      <ExceptionSource i:nil="true" />
      <InnerFault i:nil="true" />
      <OriginalException i:nil="true" />
      <TraceText>
    
    [Storm.CRM.Plugins: Storm.CRM.Plugins.OpportunityCreate_CreatePrimaryOppProdLine]
    [3461229c-7c68-e711-80c1-00155d016f85: Storm.CRM.Plugins.OpportunityCreate_CreatePrimaryOppProdLine: Create of opportunity]
    
    
    </TraceText>
    </OrganizationServiceFault>

    I have tried stripping out all the code in my plugin, re-registering it and creating an opportunity again, but I still get this error. I've been remote debugging it before now, looking at another issue, and it was hitting my breakpoints. However, it is not even hitting break points anymore. So I'm not convinced that there's something wrong in my code. Here's my plugin code anyway:

    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using Microsoft.Xrm.Sdk.Query;
    using MyCompany.CRM.Proxy;
    using MyCompany.CRM.StormSdk;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using System.ServiceModel;
    using System.ServiceModel.Description;
    
    namespace MyCompany.CRM.Plugins
    {
        public class OpportunityCreate_CreatePrimaryOppProdLine : PluginBase, IPlugin
        {
    
            public void Execute(IServiceProvider serviceProvider)
            {           
                // Get myProd data from Opp (product)
                // Create opportunity product line using that product
                // Mark it as the primary product (myCo_primaryopportunityproduct = true)
                // Register on post create            
                try
                {
                    StackTrace st = new StackTrace(new StackFrame(true));
                    
                    //// Obtain the execution context from the service provider.
                    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                    // Obtain the organization service reference.
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    CRMSdkHelper helper = new CRMSdkHelper(serviceProvider, context.UserId);
                    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;
    
                        #region Get entity data
                        try
                        {
                            EntityReference myProd = new EntityReference("product");
                            myProd = (EntityReference)(entity.Contains("myCo_product") ? entity["myCo_product"] : ((entityIn.Contains("myCo_product") ? entityIn["myCo_product"] : null)));
    
                            if (myProd != null)
                            {
                                // Get price list from Opp form
                                EntityReference priceList = new EntityReference("pricelevel");
                                priceList = (EntityReference)(entity.Contains("pricelevelid") ? entity["pricelevelid"] : ((entityIn.Contains("pricelevelid") ? entityIn["pricelevelid"] : null)));
                                // Get price list item for this product & price list                            
                                QueryExpression getPriceListItem = new QueryExpression();
                                getPriceListItem.EntityName = "productpricelevel"; // Price List Item is display name
                                ConditionExpression priceListCondition = new ConditionExpression("pricelevelid", ConditionOperator.Equal, priceList.Id); // Price list field on Price List item
                                ConditionExpression productCondition = new ConditionExpression("productid", ConditionOperator.Equal, myProd.Id);
                               
                                getPriceListItem.Criteria.AddCondition(priceListCondition); 
                                getPriceListItem.Criteria.AddCondition(productCondition); 
                                getPriceListItem.ColumnSet.AddColumns("amount", "uomid");
                                EntityCollection priceListItem = service.RetrieveMultiple(getPriceListItem);
    
                                Money price = new Money();
                                price = null;
                                EntityReference unit = new EntityReference("uom");
                                unit = null;
    
                                if (priceListItem.Entities.Count > 0)
                                {
                                    if (priceListItem.Entities[0].Contains("amount"))
                                    {
                                        price = priceListItem.Entities[0].GetAttributeValue<Money>("amount");
                                    }
                                   
                                    if (priceListItem.Entities[0].Contains("uomid"))
                                    {
                                        unit = priceListItem.Entities[0].GetAttributeValue<EntityReference>("uomid");
                                    }
                                }
    
                                // Create opp product line, mark it as primary product                                
                                Entity createOppProduct = new Entity("opportunityproduct");
                                createOppProduct["opportunityid"] = new EntityReference("opportunity", entity.Id);
                                createOppProduct["productid"] = new EntityReference("product", myProd.Id);
                                createOppProduct["myCo_primaryopportunityproduct"] = true; 
                                Decimal quantity = 1m;
                                createOppProduct["quantity"] = quantity;
                                createOppProduct["uomid"] = unit; 
                                createOppProduct["isproductoverridden"] = false; // Existing
                                createOppProduct["priceperunit"] = price; 
    
                                service.Create(createOppProduct);
                            }
                        }
                        catch (Exception ex)
                        {
                            tracingService.Trace("Could not get entity data to create Opportunity Product line, detail:" + ex.Message);
                        }
                        #endregion                    
                    }
                }
                catch (Exception ex)
                {
                    tracingService.Trace("Could not create Opportunity Product line, detail:" + ex.Message);
                }
                
    
    
            }
    
    
        }
    }
    


    • Edited by MOTO1978 Monday, August 14, 2017 3:37 PM
    Monday, August 14, 2017 3:36 PM

Answers

  • The error is System.IO.FileLoadException, which indicates CRM is failing to load your plugin assembly, or an assembly it references.

    Where is the assembly registered: Disk, GAC or Database ? And does it reference any other (non-CRM assemblies) ?

    I expect the problem is that either the assembly is registered on Disk or GAC, or it has a referenced assembly, and either the assembly is no longer where it should be, or the CRM service account doesn't have permission on the assembly. Also check that the assembly is not blocked after downloading from a remote location

    • Marked as answer by MOTO1978 Wednesday, August 16, 2017 4:32 PM
    Tuesday, August 15, 2017 3:51 PM

All replies

  • The error is System.IO.FileLoadException, which indicates CRM is failing to load your plugin assembly, or an assembly it references.

    Where is the assembly registered: Disk, GAC or Database ? And does it reference any other (non-CRM assemblies) ?

    I expect the problem is that either the assembly is registered on Disk or GAC, or it has a referenced assembly, and either the assembly is no longer where it should be, or the CRM service account doesn't have permission on the assembly. Also check that the assembly is not blocked after downloading from a remote location

    • Marked as answer by MOTO1978 Wednesday, August 16, 2017 4:32 PM
    Tuesday, August 15, 2017 3:51 PM
  • Thanks. You were right, I actually tried putting the required dll into Server/bin/assembly, and it sorted it. I still don't know why it worked without that dll being there, but suddenly seemed to need it!
    Wednesday, August 16, 2017 4:33 PM