Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to auto-populate the lookup field depends on the selected- lookup

Posted on by Microsoft Employee

I try using the workflow but it need to save the transaction before it populate, I try mapping it but doesn't work.

So I have a 3 Entities Expense,Vendor,Transaction.

In Vendor Entity I have a field of Vendor Name and a lookup field for Expense Category(Expense Entity).

In Transaction Entity I have a lookup for Vendor name and Expense Category, so when I select a vendor name in the lookup it will populate the lookup of expense category.

I already used the filter, yes it show the expense category depends on the vendor name selected, but the problem in there is I need to click the lookup field of expense tracker to show the value. I want is it will auto fill the lookup field without clicking it. Thank you

*This post is locked for comments

  • Abdelrhman Hiba Profile Picture
    Abdelrhman Hiba 321 on at
    RE: How to auto-populate the lookup field depends on the selected- lookup

    Hi all i have the same problem

    but if i want to Apply this solution between 2 forms ?!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to auto-populate the lookup field depends on the selected- lookup

    any way to fix that??

  • prt33k Profile Picture
    prt33k 6,907 on at
    RE: How to auto-populate the lookup field depends on the selected- lookup

    yes.. because even changing value to null might be triggering the webAPI call. It is better to do null check at every stage.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to auto-populate the lookup field depends on the selected- lookup

    Hi prt33k

    I already got my answer in my question, my problem now is when I clear the vendor name it having an error "TypeError: Cannot read property '0' of null at setExpenseCategorybasedonVendor " so  I created a if else

    if (lookup1 != null){

    Xrm.Page.getAttribute("cse_expense_category").setValue(value);

    Xrm.Page.getControl("cse_expense_category").setFocus();

    Xrm.Page.getControl("cse_amount").setFocus();

    }

    else{

    Xrm.Page.getAttribute("cse_expense_category").setValue(null);

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to auto-populate the lookup field depends on the selected- lookup

    Hi Ptr33k,

    Thanks,It already fill the field but the problem is I'm getiing this error in the side of the field

    "Lookup control error:cannot add item of typename= to the lookup control"

    So when I hit save, it will go to the lookup again and I need to select the value to save it

  • Verified answer
    prt33k Profile Picture
    prt33k 6,907 on at
    RE: How to auto-populate the lookup field depends on the selected- lookup

    I hope the javascript is running on onchange event of field "cse_vendor_name"
    Also, the second webAPI call is to retrieve the primary field of expense category.
    So I think it should be change.

    Just to brief:
    There are 3 entities Expense,Vendor,Transaction.
    In Vendor Entity we have lookup field for Expense Category.
    In Transaction Entity we have a lookup for Vendor name and Expense Category.

    Requirement : On transaction entity, when we select a vendor name in the lookup, the lookup of expense category should auto poulate.

    Javacript registered entity: Transaction
    attribute and event: cse_vendor_name and onchange

    function setExpenseCategorybasedonVendor() {
    var lookup1 = Xrm.Page.getAttribute("cse_vendor_name").getValue()[0].id;
    var clientUrl = Xrm.Page.context.getClientUrl();
    var query = clientUrl + "/api/data/v8.0/cse_vendormasters(" + lookup1.slice(1, -1) + ")?$select=_cse_expense_category_value";
    makeRequest('GET', query)
    .then(function (res) {
    var res2 = JSON.parse(res);
    var guid = res2._cse_expense_category_value;
    var query2 = clientUrl + "/api/data/v8.0/cse_ExpenseCategory(" + guid + ")?$select=cse_name";
    makeRequest('GET', query2)
    .then(function (response) {
    var res3 = JSON.parse(response);
    var value = new Array();
    value[0] = new Object();
    value[0].id = guid;
    value[0].name = res3.cse_name;
    value[0].entityType = "cse_ExpenseCategory";
    Xrm.Page.getAttribute("cse_expense_category").setValue(value);
    })
    .catch(function (err) {
    console.error('there was an error!', err.statusText);
    });
    })
    .catch(function (err) {
    console.error(' there was an error!', err.statusText);
    });
    }


    Also do share where exactly (line number) we are getting error along with description.

    Thanks,

    PS

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to auto-populate the lookup field depends on the selected- lookup

    Hi prt33k.

    In my case the form would be Checking Transaction, so when the Vendor name is selected(the lookup field of Vendor Master Entity) it will get the  value of Expense Category field. I'm getting error

    my form Entity is Checking Transaction, There will be a lookup field "cse_vendor_name" and a field for "cse_expense_tracker".

    function makeRequest(method, url) {

       return new Promise(function (resolve, reject) {

           var xhr = new XMLHttpRequest();

           xhr.open(method, url);

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

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

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

           xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");

           xhr.onload = function () {

               if (this.status >= 200 && this.status < 300) {

                   resolve(xhr.response);

               } else {

                   reject({

                       status: this.status,

                       statusText: xhr.statusText

                   });

               }

           };

           xhr.onerror = function () {

               reject({

                   status: this.status,

                   statusText: xhr.statusText

               });

           };

           xhr.send();

       });

    }

    function setParentAccountbasedonPrimaryContact() {

       var lookup1 = Xrm.Page.getAttribute("cse_vendor_name").getValue()[0].id;

       var clientUrl = Xrm.Page.context.getClientUrl();

       var query = clientUrl + "/api/data/v8.0/cse_vendormasters(" + lookup1.slice(1, -1) + ")?$select=_cse_expense_category_value";

       makeRequest('GET', query)

      .then(function (res) {

          var res2 = JSON.parse(res);

          var guid = res2._cse_expense_category_value;

          var query2 = clientUrl + "/api/data/v8.0/cse_checkingaccounttransactions(" + guid + ")?$select=cse_vendor_name";

          makeRequest('GET', query2)

      .then(function (response) {

          var res3 = JSON.parse(response);

          var value = new Array();

          value[0] = new Object();

          value[0].id = guid;

          value[0].name = res3.name;

          value[0].entityType = "cse_checkingaccounttransactions";

          Xrm.Page.getAttribute("cse_expense_category").setValue(value);

      })

          .catch(function (err) {

              console.error('there was an error!', err.statusText);

          });

      })

      .catch(function (err) {

          console.error(' there was an error!', err.statusText);

      });

    }

  • prt33k Profile Picture
    prt33k 6,907 on at
    RE: How to auto-populate the lookup field depends on the selected- lookup

    Hi,

    EntityType refers to the schema name of the entity so in you case it will be just "new_Category"

    Use can probably use this code. Here I am setting up parentaccountid on Account form when Primary Contact field is selected to same as parentcustomer of that contact.

    (the code need null check and error handling)

    function makeRequest(method, url) {
        return new Promise(function (resolve, reject) {
            var xhr = new XMLHttpRequest();
            xhr.open(method, url);
            xhr.setRequestHeader("OData-MaxVersion", "4.0");
            xhr.setRequestHeader("OData-Version", "4.0");
            xhr.setRequestHeader("Accept", "application/json");
            xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            xhr.onload = function () {
                if (this.status >= 200 && this.status < 300) {
    
                    resolve(xhr.response);
                } else {
                    reject({
                        status: this.status,
                        statusText: xhr.statusText
                    });
                }
            };
            xhr.onerror = function () {
                reject({
                    status: this.status,
                    statusText: xhr.statusText
                });
            };
            xhr.send();
        });
    }
    
    function setParentAccountbasedonPrimaryContact() {
        var lookup1 = Xrm.Page.getAttribute("primarycontactid").getValue()[0].id;
        var clientUrl = Xrm.Page.context.getClientUrl();
        var query = clientUrl + "/api/data/v8.0/contacts(" + lookup1.slice(1, -1) + ")?$select=_parentcustomerid_value";
        makeRequest('GET', query)
       .then(function (res) {
           var res2 = JSON.parse(res);
           var guid = res2._parentcustomerid_value;
           var query2 = clientUrl + "/api/data/v8.0/accounts(" + guid + ")?$select=name";
           makeRequest('GET', query2)
       .then(function (response) {
           var res3 = JSON.parse(response);
           var value = new Array();
           value[0] = new Object();
           value[0].id = guid;
           value[0].name = res3.name;
           value[0].entityType = "account";
           Xrm.Page.getAttribute("parentaccountid").setValue(value);
       })
           .catch(function (err) {
               console.error('there was an error!', err.statusText);
           });
       })
    
       .catch(function (err) {
           console.error(' there was an error!', err.statusText);
       });
    }


  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to auto-populate the lookup field depends on the selected- lookup

    Hi prt33k

    value[0].entityType = retrievedCategory.results[0].new_Category.LogicalName; what is this??

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to auto-populate the lookup field depends on the selected- lookup

    I'm using this js but it's not working, can someone tell me the problem??

    function Set_ExpeneCategory()

    {

    var val_vn = Xrm.Page.getAttribute("cse_vendor_name").getValue();

    var val_vnn = val_vn[0].id

    if(val_vn != null)

    {

    var val_output = val_vnn.filter;

    Xrm.Page.getAttribute("cse_expense_category").setValue(val_output);

    }

    Xrm.Page.getAttribute("cse_expense_category").setSubmitMode("always");

    }

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans