locked
Update Using the REST Enpoint with JavaScript fails RRS feed

  • Question

  • I'm trying to update a custom entity field value (an auto number) from the onsave event of a form. Here's the relevant script saved in the WebResource attached to the form's onsave event there (_orgDataPath is retrieved from the page context and works e.g. for a Retrieve request on the same form, so it does not appear to be the source of the issue here).

    When I run this in the form's onsave event, the new_CurrentIndex value of the custom entity is not updated and no error message is given.

    So my questions are: 1) Is the code below correct for a synchronous REST update using javascript? 2) If the code for the basic REST update above is correct, are there CRM configuration settings that would prevent the update from succeeding?

    (new to this...thanks for help)

    ///<summary>
    /// Entry point.
    ///</summary>
    function GetAutoNumber() {
        var targetGuid = "90DD7D29-D129-E311-93D4-00155D001A03";  // the guid of the ECRs auto number entity
        var targetName = "new_autonumbersetting";  // the name of the entity      
    
        // get the current record for targetName entity
        var autoNumberSetting = retrieveRecord(targetGuid, targetName);
    
        // set retrieved next index number to form field
        Xrm.Page.getAttribute("new_ecr_number").setValue("ECR-" + autoNumberSetting.new_CurrentIndex.toString());
    
        // increment
        autoNumberSetting.new_CurrentIndex++;
    
        // update
        updateRecord(targetGuid, autoNumberSetting, targetName);
    }
    
    ///<summary>
    /// Sends a synchronous request to update a record.
    ///</summary>
    function updateRecord(id, object, type) {
        ///<param name="id" type="String">
        /// A String representing the GUID value for the record to retrieve.
        ///</param>
        ///<param name="object" type="Object">
        /// A JavaScript object with properties corresponding to the Schema Names for
        /// entity attributes that are valid for update operations.	
        ///</param>
        ///<param name="type" type="String">
        /// The Schema Name of the Entity type record to retrieve.
        /// For an Account record, use "Account"
        ///</param>    
    
        var req = new XMLHttpRequest();
        req.open("POST", encodeURI(this._orgDataPath() + type + "Set(guid'" + id + "')"), false);  // synchronous call
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("X-HTTP-Method", "MERGE");
       
        var jsonEntity = window.JSON.stringify(object);
    
        req.send(jsonEntity);
    }
    

    Friday, December 27, 2013 5:36 PM