locked
object not set in an instance while updating a record in a plugin RRS feed

  • Question

  • Dear,

    MS CRM 2011, Please note that I am using a plugin which updates some information of a Custom entity. On update I am receiving error "Object Reference not set to an instance of an object". I have put a trace and all the fields of the Card are filled. The Service is not able to do the update or any creation through the plugin, could it be a credential or authentication issue?

    Best Regards,

    Joe Mouawad.

    Wednesday, September 10, 2014 6:14 PM

Answers

  • This is not related to the code, after deployment there was duplicated sdk messages in the plugin.

    After I removed the duplicated SDK Messages, everything worked as before.

    • Marked as answer by Joe Mouawad Saturday, September 13, 2014 8:17 AM
    Saturday, September 13, 2014 8:17 AM

All replies

  • post your plugin code

    My blog: www.crmanswers.net - Rockstar 365 Profile

    Wednesday, September 10, 2014 6:37 PM
  •   public void Execute(IServiceProvider ServiceProvider)
            {
                #region Define Services

                //Create CRM context and service instance.
                IPluginExecutionContext objContext;
                IOrganizationService objService = Utilities.GetOrganizationService(ServiceProvider, out objContext);

                #endregion

                try
                {

                    #region Processing

                    if (objContext.InputParameters.Contains("Target") && objContext.InputParameters["Target"] is Entity)
                    {
                        //Get target entity
                        Entity objEntity = (Entity)objContext.InputParameters["Target"];

                        //get Values from Barcode Tracking entity
                        var guidPhysicalStatus =
                            (objEntity.Attributes.Contains("ntx_cardphysicalstatus")) ?
                            ((EntityReference)objEntity.Attributes["ntx_cardphysicalstatus"]).Id : new Guid("00000000-0000-0000-0000-000000000000");
                       
                        var guidFromParty =
                            (objEntity.Attributes.Contains("ntx_from")) ?
                            ((EntityReference)objEntity.Attributes["ntx_from"]).Id : new Guid("00000000-0000-0000-0000-000000000000");
                       
                        var guidToParty =
                            (objEntity.Attributes.Contains("ntx_to")) ?
                            ((EntityReference)objEntity.Attributes["ntx_to"]).Id : new Guid("00000000-0000-0000-0000-000000000000");
                        var guidUser = (objEntity.Attributes.Contains("ownerid")) ?
                            ((EntityReference)objEntity.Attributes["ownerid"]).Id : new Guid("00000000-0000-0000-0000-000000000000");
                        var boolAction = (objEntity.Attributes.Contains("ntx_action")) ?
                            ((Boolean)objEntity.Attributes["ntx_action"]) : false;
                        var strShippingWayBillNumber = (objEntity.Attributes.Contains("ntx_shippingwaybillnumber")) ?
                            ((String)objEntity.Attributes["ntx_shippingwaybillnumber"]) : null;
                        var dateDeliveryDate = (objEntity.Attributes.Contains("ntx_deliverydate")) ?
                            (objEntity.Attributes["ntx_deliverydate"]) : null;

                        //set Card values
                        Entity card = new Entity("ntx_card");
                        card.Id = ((EntityReference)objEntity.Attributes["ntx_cardid"]).Id;
                        card.LogicalName = ((EntityReference)objEntity.Attributes["ntx_cardid"]).LogicalName;
                        ColumnSet attributes = new ColumnSet(new string[] { "ntx_physicalstatus", "ntx_from", "ntx_to", "ntx_user", "ntx_shippmentwaybillnumber", "ntx_action", "ntx_deliverydate", "ntx_hiddendeliveredstatus", "ntx_hidden" });
                        card = objService.Retrieve(card.LogicalName, card.Id, attributes);
                        Log.WriteEntry("1- Barcode Tracking Module", "Barcode Tracking Create", "Internal Plugin", Log.Level.Error.ToString(), "0", card.Id.ToString(), objContext.InitiatingUserId, System.Diagnostics.EventLogEntryType.Error);

                        if (guidFromParty.ToString() != "00000000-0000-0000-0000-000000000000".ToString() && guidToParty.ToString() != "00000000-0000-0000-0000-000000000000".ToString())
                        {
                            if (card.Attributes.Contains("ntx_from") && card.Attributes.Contains("ntx_to"))
                            {
                                Entity cardtrackingparty = new Entity("ntx_cardtrackingparty");
                                cardtrackingparty.Id = ((EntityReference)(card.Attributes["ntx_to"])).Id;
                                cardtrackingparty.LogicalName = ((EntityReference)(card.Attributes["ntx_to"])).LogicalName;
                                ColumnSet att = new ColumnSet(new string[] { "ntx_type" });
                                cardtrackingparty = objService.Retrieve(cardtrackingparty.LogicalName, cardtrackingparty.Id, att);

                                if (cardtrackingparty.Attributes.Contains("ntx_type"))
                                {
                                    if (((OptionSetValue)cardtrackingparty.Attributes["ntx_type"]).Value == 961200002)
                                    {//Front Office
                                    EntityReference FromParty = new EntityReference("ntx_cardtrackingparty", guidFromParty);
                                    EntityReference ToParty = new EntityReference("ntx_cardtrackingparty", guidToParty);
                                    if (((EntityReference)(card.Attributes["ntx_from"])).Id == FromParty.Id && ((EntityReference)(card.Attributes["ntx_to"])).Id == ToParty.Id)
                                    {
                                        if (IsReversalLastCheckIn(objService, card.Id))
                                        {
                                            card["ntx_hidden"] = "Dont Stop";
                                        }
                                        else
                                        {
                                            card["ntx_hidden"] = "Stop";
                                        }
                                    }
                                    else
                                    {
                                        card["ntx_hidden"] = "";
                                    }
                                    }
                                }
                            }
                        }

                        if (guidPhysicalStatus.ToString() != "00000000-0000-0000-0000-000000000000".ToString())
                        {
                            EntityReference PhysicalStatus = new EntityReference("ntx_cardphysicalstatus", guidPhysicalStatus);
                            card["ntx_physicalstatus"] = PhysicalStatus;
                            Log.WriteEntry("1- Barcode Tracking Module", "Barcode Tracking Create", "Internal Plugin", Log.Level.Error.ToString(), "1", PhysicalStatus.ToString(), objContext.InitiatingUserId, System.Diagnostics.EventLogEntryType.Error);
                        }
                        if (guidFromParty.ToString() != "00000000-0000-0000-0000-000000000000".ToString())
                        {
                            EntityReference FromParty = new EntityReference("ntx_cardtrackingparty", guidFromParty);

                            card["ntx_from"] = FromParty;
                            Log.WriteEntry("1- Barcode Tracking Module", "Barcode Tracking Create", "Internal Plugin", Log.Level.Error.ToString(), "2", FromParty.ToString(), objContext.InitiatingUserId, System.Diagnostics.EventLogEntryType.Error);
                        }

                        if (guidToParty.ToString() != "00000000-0000-0000-0000-000000000000".ToString())
                        {
                            EntityReference ToParty = new EntityReference("ntx_cardtrackingparty", guidToParty);
                            card["ntx_to"] = ToParty;
                            Log.WriteEntry("1- Barcode Tracking Module", "Barcode Tracking Create", "Internal Plugin", Log.Level.Error.ToString(), "3", guidToParty.ToString(), objContext.InitiatingUserId, System.Diagnostics.EventLogEntryType.Error);
                        }

                        if (guidUser.ToString() != "00000000-0000-0000-0000-000000000000".ToString())
                        {
                            EntityReference User = new EntityReference("systemuser", guidUser);
                            card["ntx_user"] = User;
                            Log.WriteEntry("1- Barcode Tracking Module", "Barcode Tracking Create", "Internal Plugin", Log.Level.Error.ToString(), "4", User.ToString(), objContext.InitiatingUserId, System.Diagnostics.EventLogEntryType.Error);
                        }
                        if (strShippingWayBillNumber != null)
                        {
                            card["ntx_shippmentwaybillnumber"] = strShippingWayBillNumber.ToString();
                            Log.WriteEntry("1- Barcode Tracking Module", "Barcode Tracking Create", "Internal Plugin", Log.Level.Error.ToString(), "5", strShippingWayBillNumber.ToString(), objContext.InitiatingUserId, System.Diagnostics.EventLogEntryType.Error);
                        }
                        //if (boolAction != null)
                        //{
                            card["ntx_action"] = boolAction;
                            Log.WriteEntry("1- Barcode Tracking Module", "Barcode Tracking Create", "Internal Plugin", Log.Level.Error.ToString(), "6", boolAction.ToString(), objContext.InitiatingUserId, System.Diagnostics.EventLogEntryType.Error);
                            //}
                        if (dateDeliveryDate != null)
                        {
                            card["ntx_deliverydate"] = dateDeliveryDate;
                            Log.WriteEntry("1- Barcode Tracking Module", "Barcode Tracking Create", "Internal Plugin", Log.Level.Error.ToString(), "7", dateDeliveryDate.ToString(), objContext.InitiatingUserId, System.Diagnostics.EventLogEntryType.Error);
                        }

                        # region Check if Delivered
                        // Get the related record
                    string entitySchemaNameInputStr = "ntx_cardphysicalstatus";
                    string codeSchemaNameInputStr = "ntx_cardphysicalstatuscode";
                    string codeValueInputStr = "DELIVERED";

                    QueryExpression entityQuery = new QueryExpression
                    {
                        EntityName = entitySchemaNameInputStr,
                        ColumnSet = new ColumnSet(new string[] { codeSchemaNameInputStr }),
                        Criteria = new FilterExpression
                        {
                            Conditions =
                            {
                                new ConditionExpression
                                {
                                    AttributeName = codeSchemaNameInputStr,
                                    Operator = ConditionOperator.Equal,
                                    Values = { codeValueInputStr }
                                }
                            }
                        }
                    };

                    DataCollection<Entity> entityCollection = objService.RetrieveMultiple(entityQuery).Entities;
                    Guid guidDeliveredReference = new Guid("00000000-0000-0000-0000-000000000000");

                    if (entityCollection.Count > 0)
                    {
                        Entity entity = entityCollection[0];

                       // DeliveredReference = new EntityReference(entitySchemaNameInputStr, entity.Id);
                        guidDeliveredReference = entity.Id;
                    }


                    if (guidPhysicalStatus.ToString() != "00000000-0000-0000-0000-000000000000".ToString() || guidDeliveredReference.ToString() != "00000000-0000-0000-0000-000000000000".ToString())
                    {
                       var Hidden = "";
                       if (card.Attributes.Contains("ntx_physicalstatus"))
                       {
                           if (card.Attributes.Contains("ntx_hiddendeliveredstatus"))
                           {
                               Hidden = card["ntx_hiddendeliveredstatus"].ToString();
                               if (guidPhysicalStatus != guidDeliveredReference && Hidden.ToString() == "DELIVERED")
                               {
                                   card["ntx_hiddendeliveredstatus"] = null;
                               }
                           }
                           else
                           {
                               //Hidden == null
                               if (guidPhysicalStatus == guidDeliveredReference)
                               {
                                   card["ntx_hiddendeliveredstatus"] = "DELIVERED";
                               }
                           }
                       }
                       else
                       {
                           //Hidden == null
                            if (guidPhysicalStatus == guidDeliveredReference)
                        {
                            card["ntx_hiddendeliveredstatus"] = "DELIVERED";
                        }

                       }

                       
                       
                    }

                    #endregion

                    Log.WriteEntry("1- Barcode Tracking Module", "Barcode Tracking Create", "Internal Plugin", Log.Level.Error.ToString(), "6", "6", objContext.InitiatingUserId, System.Diagnostics.EventLogEntryType.Error);

                    objService.Update(card);

                    }

                    #endregion

                }

                catch (FaultException ex)
                {
                    Log.WriteEntry("1- Barcode Tracking Module", "Barcode Tracking Create", "Internal Plugin", Log.Level.Error.ToString(), ex.Message.ToString(), ex.InnerException.Message.ToString(), objContext.InitiatingUserId, System.Diagnostics.EventLogEntryType.Error);
                    throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                }

                catch (Exception ex)
                {
                    Log.WriteEntry("2- Barcode Tracking Module", "Barcode Tracking Create", "Internal Plugin", Log.Level.Error.ToString(), ex.Message.ToString(), ex.StackTrace, objContext.InitiatingUserId, System.Diagnostics.EventLogEntryType.Error);

                    Log.CreateErrorLog("Barcode Tracking", "Internal Plugin", ServiceProvider, objContext, ex, ex.Message.ToString(), "");

                    throw new InvalidPluginExecutionException("Card update through Barcode Tracking Module has failed!");
                }
            }

    on "objService.Update(card);" the exception is being raised.

    Wednesday, September 10, 2014 6:41 PM
  • Dear,

    Please disregard my code, this is occurring whenever I am creating the CRM Web service. The Created CRM Web service is able to fetch but not able to Update or Create or delete.

    This is occurring on all the application which are creating the CRM web service.

    Could it credential or authentication on the User which is running the CRM application pool? any help

    Best Regards,

    Joe Mouawad.

    Wednesday, September 10, 2014 7:47 PM
  • hi Joe,

    are you sure that all of your Card-attributes are of the right type?

    I think I had the same Problem and when I remember right, I tried to add a decimal-value to a moneyfield.

    After I changed it to new Money(myvalue) I didn't receive this error anymore.

    so please check the following fields that they are really of this type! (you set this in your code with the following types):

    card["ntx_hidden"] --> string
    card["ntx_physicalstatus"] --> EntityReference;
    card["ntx_from"] --> EntityReference;
    card["ntx_to"] --> EntityReference;
    card["ntx_user"] --> EntityReference;
    card["ntx_shippmentwaybillnumber"] -->string
    card["ntx_action"] --> bool
    card["ntx_deliverydate"] --> DateTime
    card["ntx_hiddendeliveredstatus"] --> string

    If you are sure they are right, please comment out all card[...] parts so that the update has "nothing" to set. if the error disapears you know that this is the error.

    after that you can uncomment one by one to see what is going wrong.

    Maybe the "xtx_deliverydate" is wrong (try to use DateTime instead of var -- just a try ;)) 

     Tobias


    Thursday, September 11, 2014 7:47 AM
  • Hi Tobias,

    This issue is occurring on all the plugins using the CRM Organization Service to perform an action.

    Best Regards,

    Joe Mouawad.

    Thursday, September 11, 2014 9:15 AM
  • This is not related to the code, after deployment there was duplicated sdk messages in the plugin.

    After I removed the duplicated SDK Messages, everything worked as before.

    • Marked as answer by Joe Mouawad Saturday, September 13, 2014 8:17 AM
    Saturday, September 13, 2014 8:17 AM