crm 2011 deactivate record (change status) thru Javascript and JSON

How to change status / disable record in CRM using Javascript and JSON notation? All sources just link to http://mileyja.blogspot.cz/2011/07/set-status-or-state-of-record-using.html using XML. I tried to make it working with JSON, however, without sucess. Below is my code and error returned. The code works when changing other fields.

function update(oldObject,id){

   var newObject = new Object();
   newObject.statecode=oldObject.statecode; //take the state object from record of the same entity
   newObject.statuscode=oldObject.statuscode; // -||-

   newObject.statecode.Value=1;   
   newObject.statuscode.Value=2;  

   var jsonEntity = window.JSON.stringify(newObject);
   var ODataPath = serverUrl + 
      "/XRMServices/2011/OrganizationData.svc/ort_hodinyzesmluvSet(guid'" + id + "')";

   var r = new XMLHttpRequest();
   r.open("POST", ODataPath, false);
   r.setRequestHeader("Accept", "application/json");
   r.setRequestHeader("Content-Type", "application/json; charset=utf-8");
   r.setRequestHeader("X-HTTP-Method", "MERGE");
   r.onreadystatechange = function () {

     r=this;
     if (r.readyState == 4 ) {
       if (r.status == 200 || r.status == 201 || r.status == 202 || r.status==1223){
         alert('suc');
       } 
       else
         prompt('error',JSON.stringify(r));
     }

   };
   r.send(jsonEntity);

}

Error:

{
"timeout":0,
"responseXML":{},
"ontimeout":null,
"status":500,
"readyState":4,
"statusText":"Internal Server Error",
"responseText":"{\r\n\"error\": {\r\n\"code\": \"-2147187704\", \"message\": {\r\n\"lang\": \"en-US\", \"value\": \"**2 is not a valid status code for state code ort_hodinyzesmluvState.Active on ort_hodinyzesmluv**.\"\r\n}\r\n}\r\n}"
}

Answers


By the looks of the error message, the problem is the value you are trying to assign to statecode for the entity ort_hodinyzesmluv

The status of an entity is described by two fields: statecode and statuscode For each entity, only certain combinations of the two fields are valid.

From the code you have posted above, you are assigning:

newObject.statecode.Value=1; newObject.statuscode.Value=2;

I suggest you check the table StatusMap. This holds all the valid combinations for each entity (you'll need the entity ObjectTypeCode as well).


Need Your Help

Update multiple rows of database in Laravel

php laravel laravel-4 orm

I want a code for update multiple rows of database, somthing like this:

Issue rendering .NET form in Windows 7 vs XP

c# .net winforms windows-7

I'm working an a form with a progressbar, which is meant to have certain colors (foreground and background). On my XP machine, the colors are correct. On my Windows 7 machine, the form looks all se...