Accessing Other Entities Attributes in Dynamics CRM/365 Forms with javaScript

3.2k Views Asked by At

This function buttonBuzz() works inside the Forms of the Entities Account, Contacts and Leads. But not in the Opportunity form. Mainly because there is no telephone1 attribute. There is however a Contact entity added with "Quick View" in a section with a telephonenumber inside.

View of the Opportunity Form w/ Contact Quick View marked in red

I think it can be accessed with the telephone1 as well just not with Xrm.page

Any ideas how i can grab the attribute from inside the "quick view"?

I dont know if the "Quick view" window is a form of an iFrame. And if it is i have no clue how to access it with the Xrm.Page.getAttribute("telephone1").getValue();

function buttonBuzz(exObj) {
var phoneNumber;

// Here i store the "telephone1" Attribute from the current .page
phoneNumber = Xrm.Page.getAttribute("telephone1").getValue();

if (phoneNumber != null) {      **Sends phonenumber**           } ...
2

There are 2 best solutions below

0
On BEST ANSWER

Quick Views display data from a record selected in a lookup field, in this case a Contact. You can query data from related records using the OData endpoint.

You first need to get the Guid of the record selected:

var contactId = Xrm.Page.getAttribute("parentcontactid")[0].id || null;

You would then need to send a SDK.REST request, passing parameters for the Id of the record (contactId), entityName and the columns:

var entityName = "Contact";
var columns = "Address1_Telephone1, FirstName, LastName";    

SDK.REST.retrieveRecord(contactId, entityName, columns, null, function(result) {
    // Success, logic goes here.
    var address1_Telephone1 = result.Address1_Telephone1;
}, function(e) {
    console.error(e.message);
});

As well as your JavaScript file, you would need to include the SDK.REST.js file that is included in the MS CRM SDK download within your Opportunity form libraries.

1
On

You can pull that field up from the Contact into the Opportunity by creating a Calculated Field, setting it equal to parentcontactid.telephone1

Put the field on the form, and you'll be able to .getAttribute() it like any other Opportunity field (being Calculated, it updates itself whenever the source changes).