2016-08-21 50 views
0

我有一個需求來創建和更新自定義按鈕的一些記錄。這個自定義按鈕放置在聯繫人的主頁上。首先,我需要檢查一個或多個聯繫人。點擊自定義按鈕後,在帳戶實體中創建一個帳戶,然後使用我們創建的帳戶更新之前查看的聯繫人的查找字段。Javascript從自定義按鈕創建和更新動態CRM中的記錄

我想這linkthis裏面的一步,但它似乎對動態CRM 2016上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在我的解決方案,然後從自定義按鈕調用它。但沒有發生。

謝謝

回答

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"); 
 
     } 
 
    }); 
 
}

要Uppdate接觸

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); 
 
     } 
 
    }); 
 
}

+0

感謝您的代碼。一個問題。您爲web參考添加什麼.js文件?我只是添加jquery.1.9.1和sdk.jquery。是否有任何js文件從我的webresource中丟失? –

+0

我不知道這裏有什麼錯,但是你的代碼不工作。我認爲有要求,我錯過了 –

+0

您是否嘗試過使用F12調試代碼?如果是,那麼請讓我知道,因爲這段代碼對我來說工作得很好。 – Meet

相關問題