Friday, December 16, 2011

Performing CRUD Operations synchronously in CRM 2011 through JSON

Synchronous methods to create, update and delete the records for the Account entity through JSON. These methods works synchronously.

Create Records: To create the record into the CRM, you need to create an object for that record. After that you need to create the object of the XMLHttpRequest and open this request through “POST” method and need to call the send method with this jsonEntity (create from the target record).

function createRecord( ) {
var account = new Object( );
account.Name = "Create Sample";
account.Telephone1 = "555-0123";
account.AccountNumber = "ABCDEFGHIJ";
account.EMailAddress1 = "someone1@example.com";

var jsonEntity = window.JSON.stringify(account);
var createRecordReq = new XMLHttpRequest( );
var ODataPath = Xrm.Page.context.getServerUrl( ) + "/XRMServices/2011/OrganizationData.svc";
createRecordReq.open('POST', ODataPath + "/" + "AccountSet", false);
createRecordReq.setRequestHeader("Accept", "application/json");
createRecordReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
createRecordReq.send(jsonEntity);

var newRecord = JSON.parse(createRecordReq.responseText).d;
//Alert the Id of the created record
alert("Id is: " + newRecord.AccountId);
}

Update Record: Update record request is same as the create but for the Update we need to set the Additional request header for the Method in the XMLHttpRequest. Here we have added the “MERGE” method. As shown below.
updateRecordReq.setRequestHeader("X-HTTP-Method", "MERGE");

And when we open the request, we need to pass the guid of the record. As given in the below script.

function updateRecord( ) {
var account = Object( );
account.Name = "Update Name of this Account";
var jsonEntity = window.JSON.stringify(account);
var updateRecordReq = new XMLHttpRequest( );
var ODataPath = Xrm.Page.context.getServerUrl( ) + "/XRMServices/2011/OrganizationData.svc";
updateRecordReq.open('POST', ODataPath + "/" + "AccountSet" + "(guid'" + "E72B45B9-58E0-E011-B700-00155D005515" + "')", false);
updateRecordReq.setRequestHeader("Accept", "application/json");
updateRecordReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
updateRecordReq.setRequestHeader("X-HTTP-Method", "MERGE");
updateRecordReq.send(jsonEntity);

}

Delete Record: This is same as update request but here we need to call the Method delete and call send method with the null parameter.

function deleteRecord( ) {
var deleteRecordReq = new XMLHttpRequest( );
var ODataPath = Xrm.Page.context.getServerUrl( ) + "/XRMServices/2011/OrganizationData.svc";
deleteRecordReq.open('POST', ODataPath + "/" + "AccountSet" + "(guid'" + "E72B45B9-58E0-E011-B700-00155D005515" + "')", false);

deleteRecordReq.setRequestHeader("Accept", "application/json");
deleteRecordReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
deleteRecordReq.setRequestHeader("X-HTTP-Method", "DELETE");
deleteRecordReq.send(null);

}


-------------------------------------------------------
Posted by: Inogic
For more information/discussions (documents, sample code snippets, detailed work flow or diagrams),
Please be free to visit the following links or email us:
Web: http://www.inogic.com
Blog: http://inogic.blogspot.com
Email: news@inogic.com
-----------------------------------------------------

7 comments:

  1. Thanks for the tuto was really helpful
    Please im a bit confuse can you please explain to me why when we want to use an attribute of an Entity like account.AccountNumber, we shoul write AccountNumber instead of "accountnumber" but when you how the attributename is written is "accountnumber",
    For new customsfield with a name like "new_field" should i write "Account.New_Field"??
    Thanks
    Please answer

    ReplyDelete
  2. For ODATA request use the "Schema Name" of the attribute.
    Go to Setting>>Customizations>>Select the entity and after that select the Fields of the selected entity.
    You will find the Grid with all the attributes for the selected entity. Use the "Schema Name" of the attribute i.e. Account.new_FieldName.

    ReplyDelete
  3. where have you mentioned that you are creating or updating a record

    function createRecord( ) {
    var account = new Object( );
    account.Name = "Create Sample";
    account.Telephone1 = "555-0123";
    account.AccountNumber = "ABCDEFGHIJ";
    account.EMailAddress1 = "someone1@example.com";

    var jsonEntity = window.JSON.stringify(account);
    var createRecordReq = new XMLHttpRequest( );
    var ODataPath = Xrm.Page.context.getServerUrl( ) + "/XRMServices/2011/OrganizationData.svc";
    createRecordReq.open('POST', ODataPath + "/" + "AccountSet", false);
    createRecordReq.setRequestHeader("Accept", "application/json");
    createRecordReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    createRecordReq.send(jsonEntity);

    var newRecord = JSON.parse(createRecordReq.responseText).d;
    //Alert the Id of the created record
    alert("Id is: " + newRecord.AccountId);
    }


    can you provide the specific syntax that mention so

    ReplyDelete
  4. Hi Vikas,

    For create and retrieve, we don’t need to specify the method into the request header. But for update and delete, we need to specify the methods as given below.

    For update
    updateRecordReq.setRequestHeader("X-HTTP-Method", "MERGE");

    For delete
    deleteRecordReq.setRequestHeader("X-HTTP-Method", "DELETE");

    For create, you just need to send the json object in the request as given below.
    createRecordReq.send(jsonEntity);

    HTH

    ReplyDelete
  5. Hi,
    Can we use POST request to retrieve data ?
    I try to do this, but I don't have any responses :
    var request = new XMLHttpRequest();
    request.open("POST", oDataUrl + queryPart, true);
    request.setRequestHeader("Accept", "application/json");
    request.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    request.onreadystatechange = function () {...};
    request.send()

    Thx in advanced,
    BRMD.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Yes, you are right you can’t read the data using the POST method. You need to use the GET method for this. Please refer below link to read data through JSON.

    http://inogic.blogspot.in/2011/12/read-records-synchronously-using-json_13.html

    HTH

    ReplyDelete