Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Get Attribute from related entity using Javascript

Posted on by Microsoft Employee

Hello everyone!

I 'm trying to this without using plugins and i can't make it. It can be done using workflow but i want to run real time when the lookup field changes value.

So here is my entity form:

6406.Screenshot.png

I have added a Quick View Form at the bottom.

I've tried with unsupported way to do it using this code:

function getQuickValue() {

setTimeout(setValue,2000);


}

function setValue() {
	var productQuickViewcost =	document.getElementById("QuickViewProduct_QuickViewProduct_ral_product_ral_cost_d").getAttribute("title");

alert(productQuickViewcost);

Xrm.Page.getAttribute("ral_cost").setValue(productQuickViewcost);
Xrm.Page.getAttribute("ral_cost").setSubmitMode("always");
}


It doesn't work but it could work after some tweaking because using console the alert displays correctly the cost in quick view form . However i want to do this with a supported way so i found this url which uses REST Endpoint to do it.

https://blogs.msdn.microsoft.com/emeadcrmsupport/2015/09/24/dynamics-crm-how-to-access-a-quickform-component-via-jscript-in-turbo-forms/

I have tried this code :

function getCost() {
	var product = Xrm.Page.data.entity.attributes.get("ral_product").getValue()[0];
alert("productid =" + product.id);
	retrieveCost(product.id);
}


function retrieveCost(productId) {

	SDK.REST.retrieveRecord(
		productId,
		"Product",
		null, null,
		function (ral_product)
		{
			testFunction(ral_product);
		},
		function () {
			alert("Error Occured.");
		}
		);
}

function testFunction(ral_product) {
	alert(ral_product.ral_cost);
}


Where "Product" is the display name of my custom entity and ral_product is the internal name. When running it i get "Error Occured". Any ideas what i did wrong?

Thanks!

*This post is locked for comments

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Get Attribute from related entity using Javascript

    Both solutions are supported.

    It's my personal opinion but I use Chrome for everything including troubleshooting.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get Attribute from related entity using Javascript

    It worked ! Thanks a lot .

    Two questions if you can please. Out of the two approaches (using SDK and using XRMServiceToolkit) which is the best way to do it? I mean are both solutions supported?

    And lastly which browser you believe offers the best debugging tools for javascript with CRM?

    Thanks again!

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Get Attribute from related entity using Javascript

    If ral_Cost is currency field that you should try something like:

    alert(ral_product.ral_Cost.Value);

    also you should learn how to troubleshoot code without alerts using browser debuggin tools - blogs.msdn.microsoft.com/.../debugging-custom-javascript-code-in-crm-using-browser-developer-tools

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get Attribute from related entity using Javascript

    Thanks for the reply.

    i have changed my code to this one:

    function getCost() {

    var product = Xrm.Page.data.entity.attributes.get("ral_product").getValue()[0];

    alert("productid =" + product.id);

    retrieveCost(product.id);

    }

    function retrieveCost(productId) {

    SDK.REST.retrieveRecord(

    productId,

    "ral_product",

    null, null,

    function (ral_product)

    {

    testFunction(ral_product);

    },

    function () {

    alert("Error Occured.");

    }

    );

    }

    function testFunction(ral_product) {

    alert("success in testfunction");

    alert(ral_product.ral_Cost);

    }

    Now it goes into testFunction , but i get alert popup [object] [object]

    I have tried an alternative solution to use XRMServiceToolkit library and it does work great , however i'm a bit stubborn why the retrieveRecord function doesn't work.

    Any ideas?

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Get Attribute from related entity using Javascript

    Hello,

    You should not use Display Name in query. You should use Schema Name - ral_product but remember that it is case-sensitive.

    I would suggest to use github.com/.../CRMRESTBuilder to prepare query and get initial version of code

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans