This is our example : we want to get attributes from a Contact related to a Phonecall :
This is a MANY-TO-ONE relationship, therefore :
Primary Entity = Contact
Related Entity = Phonecall
Link Entity = regardingobjectid_phonecall_contact
(this logical name can be inferred from the N:1 link as seen in the Customizations : see this snapshot : )
How to Retrieve Many-To-One related Entity attributes using Web API and Javascript
This approach works by creating only one web resource embedded into the Form HTML page of the entity :
1) Open the Form - Add a new Region
2) Add a Web Resource to the Region
3) Click on Create New Web Resource
4) Add the following Building Block code to the Web Resource :
This code:
- loads Bootstrap
- uses JQuery form the Parent Form
- Gets Attributes from both the current Entity (Phonecall) + the related MANY-TO-ONE Entity (Contact) :
<html><head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script>
function fnGetContactDetails() {
debugger;
let gPrimaryEntityId = parent.Xrm.Page.data.entity.getId();
if (gPrimaryEntityId) {
gPrimaryEntityId = gPrimaryEntityId.replace("{", "").replace("}", "");
parent.$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: encodeURI(parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/phonecalls(" + gPrimaryEntityId + ")?$select=activityid,phonenumber&$expand=regardingobjectid_contact_phonecall($select=contactid,fullname,accountrolecode,emailaddress1,telephone1,mobilephone,company)"),
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
XMLHttpRequest.setRequestHeader("Accept", "application/json");
XMLHttpRequest.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
},
async: true,
success: function (data, textStatus, xhr) {
var result = data;
var activityid = result["activityid"];
var phonenumber = result["phonenumber"];
if (result.hasOwnProperty("regardingobjectid_contact_phonecall")) {
var contactid = result["regardingobjectid_contact_phonecall"]["contactid"];
var fullname = result["regardingobjectid_contact_phonecall"]["fullname"];
let accountrolecode = result["regardingobjectid_contact_phonecall"]["accountrolecode"];
let emailaddress1 = result["regardingobjectid_contact_phonecall"]["emailaddress1"];
let telephone1 = result["regardingobjectid_contact_phonecall"]["telephone1"];
let company = result["regardingobjectid_contact_phonecall"]["company"];
let mobilephone = result["regardingobjectid_contact_phonecall"]["mobilephone"];
document.getElementById("msg").innerHTML = fullname + "<br>" + // accountrolecode + "<br>" +
emailaddress1 + "<br>" + telephone1 + "<br>" + company + "<br>" + mobilephone;
document.getElementById("btnGetContact").style.display = "none";
// parent.Xrm.Utility.alertDialog(fullname);
}
},
error: function (xhr, textStatus, errorThrown) {
parent.Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
}
});
}
else {
parent.Xrm.Page.ui.alert("No ID for Primary Entity");
}
}
</script>
<meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta><meta></head><body style="direction: rtl; overflow-wrap: break-word;">
<div class="container">
<div class="jumbotron">
<h3>Contact Details</h3>
<button id="btnGetContact" class="btn btn-secondary" onclick="fnGetContactDetails()">Get Contact Details </button>
<b><div id="msg"></div></b>
</div>
</div></body></html>
}
That's all...
In this article we've seen Step by step How to Retrieve Many-To-One related Entity attributes using Web API and Javascript in 5 minutes.
Enjoy Microsoft Dynamics 365 CRM!
In this article we've seen Step by step How to Retrieve Many-To-One related Entity attributes using Web API and Javascript in 5 minutes.
Enjoy Microsoft Dynamics 365 CRM!
by Carmel Schvartzman
כתב: כרמל שוורצמן
No comments:
Post a Comment