How to get value from a lookup or parent entity using Calculated field in Microsoft Dynamics CRM

Did you ever need to get values from other entity or from a lookup on current entity? What comes to your mind, bet it is javascript , plugin etc?

Well, lets pause the coding life for minute and do it using Calculated Fields which used simple UI like business rules.

In Scenario below , i am retrieving the Budget Allocated amount from source campaign on a lead record.

  1. Create a currency Calculated field on lead record :1Give the appropriator name , select data type as Currency or as per your need > Select Field type as Calculated and click on Edit. As soon as you click on edit, system will actually create this field for you.
  2. After clicking on Edit button , you will be presented with the UI where you have put in  your logic:2

    As a first step i am checking if Budget Allocated field on campaign actually has data.To do this you have select Source campaign in the Entity and the corresponding field.

  3. Once this condition is set click on the Action :3

    In the editor start the typing the name of source campaign lookup or you can scroll though all fields/lookups available on the lead entity. Here i have select campaignid

  4. Once selected the campaignid , enter a dot(.) – post which you will start getting fields from the campaign:4

    Here i am selecting the budgetcost(Allocated Budget)

  5. Finally it will look like below :5Save and close the editor and also the field property window.
  6. Put this calculated field on the form and publish customization.
  7. Open a lead which has campaign associated to it:

    6campaign :

    7It is a very basic example i showed , i tried not to make it complex as it might become very confusing for a beginner.  You can do a lot manipulation with the data by using out of the box functions that are available for a calculated field or apply your custom logic.hope this helps!

    cheers!

Advertisement

how to retrieve lookup value and set it on another lookup field in Dynamics CRM 2016 Javascript

In this blog i am going to show how you can retrieve a lookup value and set it.

for e.g if you need to set one contact which in a regarding field on an activity to another contact field.

use the below simple JavaScript:

function yourFunctionName()

var lookup = new Array();  // create a new array
lookup = Xrm.Page.getAttribute("regardingobjectid").getValue(); // get field value
if (lookup != null) 
{
var name = lookup[0].name; // get the name of the record
var id = lookup[0].id; // get the  id of the record
var entityType = lookup[0].entityType; // get the entitytype
now we have retrieved all information that we needed ; its time we set it 🙂

if (entityType == "contact")
{ // ignore this step if its not a multilookup field
var value = new Array(); //create a new object array
value[0] = new Object();
value[0].id = id; // set ID to ID
value[0].name = name; //set name to name
value[0].entityType = "contact"; //optional
Xrm.Page.getAttribute("from").setValue(value); //set the value.
}
}
}
}

Call this function on Onload of the required form.

hope it helps !!

cheers