locked
incident statecode error when using rest.sdk? RRS feed

  • Question

  • Hello, I'm using a REST service call to update an incident in my CRM implementation. However, I seem to be having an issue and it seems like I wasn't having the issue before lunch.  In the code below, I'm setting a single field value on incident for update:

             var incident = {};
             incident.new_MyTestField = 'test';
             //incident.statecode = 1;
             SDK.REST.updateRecord(
                  caseId,
                  incident,
                  "Incident",
                  function () {
                  },
                  errorCallback
             ); 

    However, if I execute this code as-is then the following error is returned in my errorCallback handler:

        Error : 500: Internal Server Error: Attribute: statecode cannot be set to NULL

    However, if I uncomment the line in my code above then the following error is returned in my errorCallback handler:

        Error : 400: Bad Request: Error processing request stream. The property name 'statecode' specified for type 'Microsoft.Crm.Sdk.Data.Services.Incident' is not valid.

    Any idea what the issue might be here?  Feels like a catch 22?

    Friday, February 21, 2014 8:06 PM

Answers

  • You can not set the statecode in an update operation. Statecode can only be set using a SetStateCodeRequest or DynamicSetStateRequest.

    Just remove these lines:

                incResolution.Attributes.Add("statecode", 1);
                incResolution.Attributes.Add("statuscode", 5);


    If this post is an answer or helpful, please do not forget to vote!

    Monday, February 24, 2014 9:45 PM

All replies

  • This code should work:

    var caseId = "C9BA2236-B57F-E311-BB6D-6C3BE5A881A4";
    var incident = {};
    incident.new_MyTestField = 'test';
    SDK.REST.updateRecord(
         caseId,
         incident,
         "Incident",
         function () {
            alert("ok");
         },
         function (error) {
            alert("error");
         }
    );

    You aren't trying to update a closed case are you?

    Also make absolutely sure your changes are published- clear your cache also. Lots of times CRM caches web resources and holds on to an old version even after you've published. 


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Friday, February 21, 2014 8:44 PM
    Moderator
  • It looks like my problem code is in the following block.  I added a few more attributes to the incidentresolution object below:

                var incResolution = new Entity("incidentresolution");
                incResolution.Attributes.Add("ownerid", new Guid("B29D9376-91F4-E211-904F-00155D760004"));
                incResolution.Attributes.Add("subject", "test1");
                incResolution.Attributes.Add("description", "test2");
                incResolution.Attributes.Add("statecode", 1);
                incResolution.Attributes.Add("statuscode", 5);
                var req = new CloseIncidentRequest
                {
                    IncidentResolution = incResolution,
                    RequestName = "CloseIncident",
                    Status = new OptionSetValue(5)
                };
                orgService.Execute(req);

    However, now I get the following error:

        Error : 500: Internal Server Error: Attribute: ownerid cannot be set to NULL

    This seems strange since I'm explicitly setting the ownerid value in my second line of code above. I'm using this same code in a slightly different context in a separate ETL app so I'm not sure why this code is breaking for me in this scenario?
    Monday, February 24, 2014 7:42 PM
  • Looks like I needed to set ownerid to an EntityReference.  However, now the following code now consistently thows a "SQL timeout expired" exception:

                incResolution.Attributes.Add("incidentid", new EntityReference("incident", incident.Id)); 
                incResolution.Attributes.Add("ownerid", new EntityReference("ownerid", new Guid("B29D9376-91F4-E211-904F-00155D760004")));
                incResolution.Attributes.Add("subject", "test1");
                incResolution.Attributes.Add("description", "test2");
                incResolution.Attributes.Add("statecode", 1);
                incResolution.Attributes.Add("statuscode", 5);
                var req = new CloseIncidentRequest
                {
                    IncidentResolution = incResolution,
                    RequestName = "CloseIncident",
                    Status = new OptionSetValue(5)
                };
                orgService.Execute(req);

    I don't think I've ever encountered this issue in CRM before.  Any idea what the issue might be?

    Monday, February 24, 2014 8:19 PM
  • Try this:

    incResolution.Attributes.Add("incidentid", new EntityReference("incident", incident.Id));
    incResolution.Attributes.Add("ownerid", new EntityReference("systemuser", new Guid("FC099CE9-78B0-4273-B921-C72E010B308F")));
    incResolution.Attributes.Add("subject", "test1");
    incResolution.Attributes.Add("description", "test2");
    var req = new CloseIncidentRequest
    {
    	IncidentResolution = incResolution,
    	RequestName = "CloseIncident",
    	Status = new OptionSetValue(5),                  
    };

    Removed the extra code to set state/status and updated he entity reference to "systemuser" as the entity type


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, February 24, 2014 8:28 PM
    Moderator
  • You can not set the statecode in an update operation. Statecode can only be set using a SetStateCodeRequest or DynamicSetStateRequest.

    Just remove these lines:

                incResolution.Attributes.Add("statecode", 1);
                incResolution.Attributes.Add("statuscode", 5);


    If this post is an answer or helpful, please do not forget to vote!

    Monday, February 24, 2014 9:45 PM
  • @henk - good call.  I also followed that logic and removed the ownerid reference which fixed the issue

    Monday, February 24, 2014 10:15 PM