web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Auto Populate a text field on selection of a lookup field

(0) ShareShare
ReportReport
Posted on by 347

Hi all,

I am new to Microsoft Dynamic CRM 2016. I have an entity Project, i want to display a lookup field of Projects in Lead along with a text field for Project Country. What i want is when i select Project from lookup field, the project country field automatically gets populated with project country value. But i have no idea how can i do this CRM? If any one can give suggestion or give help then please.

*This post is locked for comments

I have the same question (0)
  • sardar ahmed Profile Picture
    520 on at

    Hi waleed,

    You can write javascript  for the above and call the function on change of project field.

    function onChangeOfProject(){

    var projectId;

    var project = Xrm.Page.data.entity.attributes.get("project");

    projectId=this.GetLookupID(project);

    if(projectId==null){

    return;

    }

    var encodedFetchXML=this.getFetchXml(projectId);

    var units=this.GetUnitDetails(encodedFetchXML);

    }

    function getFetchXml(projectId){

    var fetchXML = ['<fetch distinct="false" mapping="logical" output-format="xml-platform" version="1.0">'+

    '<entity name="project">'+

    '<attribute name="country"/>'+

    '<filter type="and">'+

     '<condition attribute="projectid" value="'+projectId+'" operator="eq"/>'+

    '</filter>'+

     '</entity>'+

    '</fetch>'].join('');

           var encodedFetchXML = encodeURIComponent(unitFetchXML);

    return encodedFetchXML;

    }

    function setCountry(encodedFetchXML){

    var req = new XMLHttpRequest();

           req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/productpricelevels?fetchXml=" + encodedFetchXML, true);

           req.setRequestHeader("OData-MaxVersion", "4.0");

           req.setRequestHeader("OData-Version", "4.0");

           req.setRequestHeader("Accept", "application/json");

           req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");

    req.onreadystatechange=function(){

    if (req.readyState === 4){

    req.onreadystatechange = null;

    if (req.status === 200){

    var results = JSON.parse(this.response);

    if(results.value.length==1)

    {

    var unitDetails=results.value[0];

    var country=unitDetails['country'];

    Xrm.Page.getAttribute("cld_amount").setValue(country);

    }

    }

    }

    };

    req.send();

    }

    The above script is not accurate, you need to make the changes as per your entity and attribute names

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    Here is the step you need to follow -

    Step 1:  Create the relationship  between Lead and Project entity make sure relationship should be 1:N when you are creating from project entity. A lookup field in lead will automatically create when you create the 1:N relationship from project entity.

    Step 2: Add the create lookup field in the Lead entity form.

    Step 3: Create an JS webresource and add a function over there where you need to write WEB API request to retrieve the selected project lookup country. You  need to pass the project id in the API query.

    Step 3: To create the API query ,download the CRMRESTBuilder from github.com/.../CRMRESTBuilder , import the solution and it will give you a button in customization area , by which you can create Webapi query to retrieve particular project country.

    Step 4 : Once you get the country by executing on change method of lookup  you can set value to the lead country field in client side.

    Hope this helps.

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hello,

    Assuming you just to display text field related to project on lead form,if yes, you don't need to write any code,you can simply utility quick view form to display relatedentity data based on thelookup. As suggested

    > first create relationship (1:N) between your project entity and lead,

    > Create a quick view form for project entity and place your textfield there.

    > Add project lookup on lead entity and place project quick view form on lead form.

    you can refer this post: http://himbap.com/blog/?p=1179

  • gdas Profile Picture
    50,091 Moderator on at

    Hello Mahender,

    I am not sure but seems implement quick view form for one field having performance issue if we compare to retrieve single field using API.

    What do you think?

  • Mahendar Pal Profile Picture
    45,095 on at

    Hello Goutam,

    Not sure why you think so, as it's a best practise to use out of the box features, it will give better performance instead of writing custom code+ it removes extra development efforts and you don't require any technical knowledge to implement out of the box features so any business user can implement it without any dependency.

    If you have faced any issue you can share it here.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans