Javascript从自定义按钮在Dynamics CRM中创建和更新记录

时间:2016-08-21 08:29:22

标签: javascript dynamics-crm dynamics-crm-2013

我需要从自定义按钮创建和更新一些记录。此自定义按钮位于联系人的主页上。首先,我需要检查一个或多个联系人。单击自定义按钮后,它会在帐户实体中创建一个帐户,然后使用我们创建的帐户更新之前检查过的联系人的查找字段。

我正在尝试此linkthis中的步骤,但它似乎不适用于Dynamics CRM 2016 on-premis

请你帮我,给我一个你认为可以满足我上述要求的建议或其他纪录片。

EDITED : 在sdk上,有一个使用JQueryRestDataOperation的示例。从那里,我写了这样的代码:

    function createAccount() {

 startButton.attr("name");

 var account = {};

 account.Name = "Test Account Name";

 account.Description = "This account was created by the JQueryRESTDataOperations sample.";



 //Create the Account

 SDK.JQuery.createRecord(

     account,

     "account",

     function (account) {

      writeMessage("The account named "" + account.Name + "" was created with the AccountId : "" + account.AccountId + "".");

      writeMessage("Retrieving account with the AccountId: "" + account.AccountId + "".");

      retrieveAccount(account.AccountId)

     },

     errorHandler

   );



}

我在我的解决方案中将此代码添加为webresource,然后从自定义按钮调用它。但什么都没发生。

由于

1 个答案:

答案 0 :(得分:0)

要创建帐户,您可以使用下面给出的代码。单击按钮可调用函数CreateAccount()



function CreateAccount() { 
    var accountId, accountName;
    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 = "/AccountSet"; 
    ///////////////////////////////////////////////////////////// 
    // Define attribute values for the CRM object you want created 
    CRMObject.Name = "TEST";
    accountName = "TEST"; 
    CRMObject.Telephone1 = "123"; 
    CRMObject.Fax = "456"; 
    //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) { 
            alert("success"); 
            var NewCRMRecordCreated = data["d"]; 
            accountId = NewCRMRecordCreated.AccountId;
            UpdateContact(accountId,accountName); 
        }, 
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
            alert("failure"); 
        } 
    }); 
}




要更新联系



function UpdateContact(accountId, accountName) {
    var serverUrl = Xrm.Page.context.getServerUrl().toString();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    var ODATA_EntityCollection = "/AccountSet";

    var objContact = new Object();
    // set the name of Account
    objContact.Id = Xrm.Page.data.entity.getId();

    // set the Primary Contact lookup field
    objContact.AccoutId = { Id: accountId, LogicalName: "account", Name: accountName };

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

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection,
        data: jsonEntity,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (response) {
            if (response != null && response.d != null) {
                alert(response.d.ContactId);
            }
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
            alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
        }
    });
}