简体   繁体   中英

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}"
}

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM