[英]Javascript Create and Update Record In Dynamics CRM from Custom Button
I have a requirement to create and update some records from custom button. 我需要从“自定义”按钮创建和更新一些记录。 This custom button placed on homepage of the contact.
此自定义按钮放置在联系人的主页上。 First, i need to check one or more contact.
首先,我需要检查一个或多个联系人。 Once the custom button is clicked, its creating an account in account entity then update look up field of the contact that i checked before with account that we create.
单击自定义按钮后,其将在帐户实体中创建一个帐户,然后更新我之前使用我们创建的帐户检查的联系人的查找字段。
I am trying the step inside this link and this , but it's seem doesn't work for Dynamics CRM 2016 on-premis 我正在尝试此链接和this中的步骤,但对于本地Dynamics CRM 2016似乎不起作用
Could you please help me and give me a suggestion or another documentary that you think it can satisfy my requirement above. 您能否帮助我,并给我建议或其他纪录片,使您认为它可以满足我的上述要求。
EDITED : on the sdk, there is an example to use JQueryRestDataOperation. 编辑 :在sdk上,有一个使用JQueryRestDataOperation的示例。 From there, i write a code like this:
从那里,我写这样的代码:
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
);
}
I add this code as webresource in my solution, then call it from custom button. 我将此代码添加为解决方案中的webresource,然后从“自定义”按钮调用它。 But nothing happen.
但是什么也没发生。
Thanks 谢谢
To Create Account you can use code given below. 要创建帐户,您可以使用下面给出的代码。 On click of button give call to function CreateAccount()
单击按钮时调用函数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"); } }); }
To Uppdate contact 更新联络人
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); } }); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.