Why am I getting Object Reference not set to an instance of an object error RRS feed

  • Question

  • internal void MergeContact(LocalPluginContext localContext)
                IPluginExecutionContext pluginContext = localContext.PluginExecutionContext;
                EntityReference entityReferenceMaster = (EntityReference)pluginContext.InputParameters["Target"];
                Entity updateContentData = (Entity)pluginContext.InputParameters["UpdateContent"];

                 // Get Master record information. Specify the attributes which you want to retrieve
                Entity entMasterContact = CrmSdkHelper.Retrieve("contact", entityReferenceMaster.Id, new ColumnSet(new string[] { "str_s3username" }));

                // Get Subordinate record information. Specify the attributes which you want to retrieve
               // Entity entOrphanContact = CrmSdkHelper.Retrieve("contact", guidSubOrdinate, new ColumnSet(new string[] { "str_s3username" }));

                //Get Updated record information. Specifying the attributes which we want to retrieve
                Entity updateOrphanContact = CrmSdkHelper.Retrieve("contact", updateContentData.Id, new ColumnSet(new string[] { "str_s3username" }));
                String masterS3Username = string.Empty;
               // String subordinateS3Username = string.Empty;
                String updateSubOrdinate = string.Empty;
                if (entMasterContact.Attributes.Contains("str_s3username"))
                    masterS3Username = entMasterContact["str_s3username"].ToString();
               // if (entOrphanContact.Attributes.Contains("str_s3username"))
              //  {
              //      subordinateS3Username = entOrphanContact["str_s3username"].ToString();
              //  }
                if (updateOrphanContact.Attributes.Contains("str_s3username"))
                    updateSubOrdinate = updateOrphanContact["str_s3username"].ToString();
                if (masterS3Username != string.Empty & updateSubOrdinate != string.Empty)
                    if (masterS3Username != updateSubOrdinate)
                        throw new InvalidPluginExecutionException("You are trying to merge the records with different S3 Usernames. Please check the S3 Usernames");
                else if (masterS3Username == string.Empty & updateSubOrdinate != string.Empty)
                   throw new InvalidPluginExecutionException("Master S3 record cannot be empty");
                else if (masterS3Username != string.Empty & updateSubOrdinate == string.Empty)
                  throw new InvalidPluginExecutionException(" You cannot overwrite null value for Master S3 record");
    Friday, June 24, 2016 10:46 AM

All replies

  • Hi Ragavendra,

    My guess would be that the error occurs when attempting to retrieve 'updateContentData'.

    It would be helpful if you could use tracing to pinpoint the problem, please see;


    ~ Nathan

    Monday, June 27, 2016 12:19 AM
  • I don't think you're using UpdateContent correctly.

    First of all, this is an optional parameter in the Merge request, so pluginContext.InputParameters["UpdateContent"] might be null. Also, this property is used as a container of attributes; it is not a representation of a record in CRM, so the Id property may be empty, and there's no meaning in trying to Retrieve the data - all data you want will be in the attributes you already have

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Monday, June 27, 2016 8:26 AM