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

  • Mahendar Pal Profile Picture
    45,095 on at
    RE: Auto Populate a text field on selection of a lookup field

    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.

  • gdas Profile Picture
    50,089 Moderator on at
    RE: Auto Populate a text field on selection of a lookup field

    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?

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at
    RE: Auto Populate a text field on selection of a lookup field

    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

  • Suggested answer
    gdas Profile Picture
    50,089 Moderator on at
    RE: Auto Populate a text field on selection of a lookup field

    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.

  • sardar ahmed Profile Picture
    520 on at
    RE: Auto Populate a text field on selection of a lookup field

    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

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,865 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,723 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans