locked
CRM 2011 - Update attribute on Relative Entity Using oData/JavaScript RRS feed

  • Question

  • Hi Forum,

    Is it possible to update related entity attribute using oData and javascript.

    For example, I have following 2 entities with one to many relationship.

    1. Apartment

    2. Tenant

    In CRM, on 'Apratment' form I have a lookup field  'Tenant'. In Tenant entity I have a attribute call "End Date".

    Now, whenever user change the Tenant lookup field on Apartment form, value of the "End Date" in tenant entity should get change to todays date.

    Is this possible by using oData ? If yes, any link or reference would be much appreciated. I know it can be done easily using pre and post image in plug-in

    but would like to try with oData and JavaScript

     

    Thanks in advance.

    Tuesday, January 31, 2012 4:34 AM

Answers

  • Hi

     

    function updateAccountRecord(Id) {
     var updateAccountReq = new XMLHttpRequest();
    var changes = new Object();
    changes.Name = "Updated Sample";
    changes.Telephone1 = "555-0123";
    changes.AccountNumber = "ABCDEFGHIJ";
    changes.EMailAddress1 = "someone1@example.com";

    updateAccountReq.open("POST", ODataPath + "/AccountSet(guid'" + Id + "')", true);
    updateAccountReq.setRequestHeader("Accept", "application/json");
    updateAccountReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    updateAccountReq.setRequestHeader("X-HTTP-Method", "MERGE");
    updateAccountReq.onreadystatechange = function () {
      updateAccountReqCallBack(this, Id);
    };
    updateAccountReq.send(JSON.stringify(changes));
    showMessage("updateAccountRecord function END.");
    }

     

    on change of Tenant modify the above code to update tenant, this code can also be found in crm 2011 SDK

    SampleCode\JS\RESTEndpoint\JScriptRESTDataOperations

    Cheers,


    Hassan
    • Marked as answer by CRM Thirsty Tuesday, January 31, 2012 10:03 PM
    Tuesday, January 31, 2012 4:53 AM
  • Hi,

    Your requirement can easily fullfilled with the oData endpoint of CRM. You can update any attribute of any entity if you know the id, entity logical name and the attribute to change. So in your case you know the id of the Tenant object from the apartment lookup as I assume and you know the attribute logical name of the End Date. The only thing you have to know is that for the oData Service from CRM you need to use the Schema-Name of the Attributes instead of the Logical Name. Look at the Entity Set of the oData Service to find out the correct Schema Names for your Attributes. (http://server/org/XrmServices/2011/OrganizationData.svc/TenantSet)

    When you know the Schema Names you can use the CrmRestKit to easily make Retrieve/RetrieveMultiple/Update/Create/Delete requests. You can find the CrmRestKit js library at http://crmrestkit.codeplex.com/ additionally you need to download the newest jquery lib and the JSON2 lib. Once you added all 3 js libs as webresource to your Crm form you are ready to use the code below in your crm formular in the on change event of the tenant lookup:

    var tenantUpdate = 
    {
        TenantId: <yourTenantId>,
        EndDate: new Date()
    };
    
    CrmRestKit.Update( "Tenant", <yourTenantId>, tenantUpdate);
    

    If you want to have intellisense in your js file for the CrmRestKit just add the following line in your js file at the begining:

    ///<REFERENCE path='CrmRestKit.js'/>
    

    Note that the path has to point to the location of your CrmRestKit.js file. (You can use relative Uris as well)

    hth & regards

    Uli

     

    • Marked as answer by CRM Thirsty Tuesday, January 31, 2012 10:03 PM
    Tuesday, January 31, 2012 10:37 AM

All replies

  • Hi

     

    function updateAccountRecord(Id) {
     var updateAccountReq = new XMLHttpRequest();
    var changes = new Object();
    changes.Name = "Updated Sample";
    changes.Telephone1 = "555-0123";
    changes.AccountNumber = "ABCDEFGHIJ";
    changes.EMailAddress1 = "someone1@example.com";

    updateAccountReq.open("POST", ODataPath + "/AccountSet(guid'" + Id + "')", true);
    updateAccountReq.setRequestHeader("Accept", "application/json");
    updateAccountReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    updateAccountReq.setRequestHeader("X-HTTP-Method", "MERGE");
    updateAccountReq.onreadystatechange = function () {
      updateAccountReqCallBack(this, Id);
    };
    updateAccountReq.send(JSON.stringify(changes));
    showMessage("updateAccountRecord function END.");
    }

     

    on change of Tenant modify the above code to update tenant, this code can also be found in crm 2011 SDK

    SampleCode\JS\RESTEndpoint\JScriptRESTDataOperations

    Cheers,


    Hassan
    • Marked as answer by CRM Thirsty Tuesday, January 31, 2012 10:03 PM
    Tuesday, January 31, 2012 4:53 AM
  • Hi,

    Your requirement can easily fullfilled with the oData endpoint of CRM. You can update any attribute of any entity if you know the id, entity logical name and the attribute to change. So in your case you know the id of the Tenant object from the apartment lookup as I assume and you know the attribute logical name of the End Date. The only thing you have to know is that for the oData Service from CRM you need to use the Schema-Name of the Attributes instead of the Logical Name. Look at the Entity Set of the oData Service to find out the correct Schema Names for your Attributes. (http://server/org/XrmServices/2011/OrganizationData.svc/TenantSet)

    When you know the Schema Names you can use the CrmRestKit to easily make Retrieve/RetrieveMultiple/Update/Create/Delete requests. You can find the CrmRestKit js library at http://crmrestkit.codeplex.com/ additionally you need to download the newest jquery lib and the JSON2 lib. Once you added all 3 js libs as webresource to your Crm form you are ready to use the code below in your crm formular in the on change event of the tenant lookup:

    var tenantUpdate = 
    {
        TenantId: <yourTenantId>,
        EndDate: new Date()
    };
    
    CrmRestKit.Update( "Tenant", <yourTenantId>, tenantUpdate);
    

    If you want to have intellisense in your js file for the CrmRestKit just add the following line in your js file at the begining:

    ///<REFERENCE path='CrmRestKit.js'/>
    

    Note that the path has to point to the location of your CrmRestKit.js file. (You can use relative Uris as well)

    hth & regards

    Uli

     

    • Marked as answer by CRM Thirsty Tuesday, January 31, 2012 10:03 PM
    Tuesday, January 31, 2012 10:37 AM
  • Thanks Hassan and Uli.

    Appreciate your time.

    Cheers

    Tuesday, January 31, 2012 10:03 PM
  • Hi Hasanz,

       I am also facing the same issue. here is my scenario.

    i am using crm 2013, i have two entities Quote and PriceList. In quote i have two fields. 'product' and 'product amount'.

    product is a lookup filed and it is pointing to PriceList entity. 'product amount' is currency field. when i select a product in lookup field the price of that product, we need to get the price of that product from pricelist entity and need to be updated in the 'product amount' field of Quote entity.

    for this i am using the below code.

    function retrieverecord() {
    alert('retrieverecord start');
      if (Xrm.Page.getAttribute("pricelevelid").getValue() != null) 
      {
         var pricelevelID = Xrm.Page.getAttribute("pricelevelid").getValue()[0].id;
         alert(pricelevelID);
    var pname=document.getElementById("pricelevelid").innerText;
    alert('hi '+pname);

    var amount;
    var serverUrl = window.parent.Xrm.Page.context.getClientUrl();
    var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/AccountSet?$select=telephone2&$filter=name eq 'test'";
    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", oDataSelect, false);
    retrieveReq.setRequestHeader("Accept", "application/json");

    retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    retrieveReq.onreadystatechange = function () {

    if (retrieveReq.readyState == 4){

    var retrieved = parent.JSON.parse(retrieveReq.responseText).d;

    if (retrieved != null) {
    alert('not null');
    //productrange= productrange+ retrieved.results[0].new_ProductRange_value;

    amount=retrieved.results.new_amount;
    }
    }
    //GetProductData(this);
    };
    retrieveReq.send();
    alert('Amount is:'+amount);
    Xrm.Page.getAttribute("new_amount").setValue(amount);
    }
     
      }

    Please help me if you have any idea on this.


    Nagaraj

    Tuesday, December 24, 2013 2:16 PM