locked
Creating CRM Record using ODATA Service RRS feed

  • Question

  • Hi,

    Below is a code written for creating a copy of Opportunity record using ajax function to call ODATA service. 
    While creating a duplicate record, name attribute value is changed.  Anyone, please help me to understand how the attribute value can be changed. Is it done in the beforeSend method. If yes how can we check this.

    PFB the code

    function CopyOpportunity() {
        if (!Xrm.Page.data.entity.getIsDirty()) {
            Xrm.Page.ui.setFormNotification("Copying opportunity, please wait.", "INFO", "CopyingOpportunityMessage");
            var context = Xrm.Page.context;
            var serverUrl = context.getServerUrl();
            var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
            var CRMObject = new Object();

            ///////////////////////////////////////////////////////////// 
            // Specify the ODATA entity collection 
            var ODATA_EntityCollection = "/OpportunitySet";

            // Define attribute values for the CRM object you want created 
            CRMObject.Name = "Temporary copy";
            CRMObject.mca_CopyReference = Xrm.Page.data.entity.getId();
            //CRMObject.statuscode = 1;

            //Parse the entity object into JSON 
            var jsonEntity = window.JSON.stringify(CRMObject);

            //Asynchronous AJAX function to Create a CRM record using OData
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection,
                data: jsonEntity,
                beforeSend: function (XMLHttpRequest) {
                    //Specifying this header ensures that the results will be returned as JSON. 
                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                },
                success: function (data, textStatus, XmlHttpRequest) {
                     var NewCRMRecordCreated = data["d"];
                    Xrm.Utility.openEntityForm("opportunity", NewCRMRecordCreated.OpportunityId);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                  Xrm.Page.ui.clearFormNotification("CopyingOpportunityMessage");
                    Xrm.Utility.alertDialog("Cannot copy the record at this time. An opportunity with the same name and same account already exists. Please try again later. If you keep having problems, please contact your system administrator");
                }
            });

     

    Monday, December 1, 2014 4:31 AM

All replies