Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

setting a field value through jquery which is rendered as dropdown

(0) ShareShare
ReportReport
Posted on by 7,316

Hi,

I was setting up a lookup field through jquery on form load. It is working fine.

Then, I made that lookup to be rendered as dropdown through entity form metadata. Now on the form load, the dropdown shows empty and it won't default to the value I am setting up through jquery.

Is this approach not supported? What am I doing wrong?

Thanks for any help!

  • nickmanny Profile Picture
    136 on at
    RE: setting a field value through jquery which is rendered as dropdown

    Make sure you are executing your jQuery code after the dropdown has been populated with options from the entity form metadata. If your jQuery code is running before the dropdown is populated, it won't be able to find the desired option to set as the default value. Ensure that your jQuery code is placed after the code that populates the dropdown. Also, If you're trying to set the default value on form load, you may need to listen for the appropriate event. The exact event can vary depending on the form or framework you're working with. In some cases, you might need to use the ready event for the form or the change event for the dropdown. Experiment with different events to see which one works for your specific scenario.

  • RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: setting a field value through jquery which is rendered as dropdown

    Its a best practice to try the code in the developer console before putting it in script.

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: setting a field value through jquery which is rendered as dropdown

    Hi,

    This doesn't seem right because if your results contains more than 1 record then only the last value will be set. Can you check the script by just passing the hardoced guid. This will help you find if the problem is in that statement or somewhere else in your code.

    Also, the id you are setting should be available as an option in that dropdown list otherwise it will not work. For e.g. say you got response for Contact1 but in the dropdown there is not guid for contact1 then it won't work.

    Hope this helps.

  • cloflyMao Profile Picture
    25,202 on at
    RE: setting a field value through jquery which is rendered as dropdown

    Hi meenoo,

    Could you share me some of your code of implementation and your business requirements?

    In my opinion, if we want to change results in lookup field dropdown list with JS, we could work with client API 

    addPreSearch and addCustomFilter.

    Below is my example, filter out opportunities that its topic start with 'g':

    function filterAccountLookup() {  
        try {
            Xrm.Page.getControl("new_test_oppo").addPreSearch(function () {
                addCustomLookupfilter();
            });
        } catch (e) {

            throw new Error(e.message);
        }
    }

    function addCustomLookupfilter() {
        try {
            fetchxml = "<filter>" + "<condition attribute='name' operator='like' value = '11' />" + "</filter>";
            Xrm.Page.getControl("new_test_oppo").addCustomFilter(fetchxml, "opportunity");
            alert('ok');
        }
        catch (e) {
            throw new Error(e.message);
        }
    }

    1055.png

    We can add our filter criteria with collected value from other client API.

    From my previous test, I couldn't render my filtered results list on a default field of system entity(contact), then it worked on a new custom lookup field,

    you could try to close default view of your entity of its lookup field.

    Also, for your current implementation, you could surround actions/steps with try catch statement

    Regards,

    Clofly

  • meenoo Profile Picture
    7,316 on at
    RE: setting a field value through jquery which is rendered as dropdown

    Thanks Ravi. Tried that, not working.

    Here is what I am doing:

    if(response !== null){

          $.each(response, function (index, responseVal) {

                /*jshint -W087 */

           debugger;

         $("#new_country").val(responseVal.new_countryid);

          });                

    }      

    I am getting the response from odata query. And I am trying to assign the 'id' from the response to the country lookup field. Is this what you suggest?

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: setting a field value through jquery which is rendered as dropdown

    Hi Meeno,

    When you change the style, it renders the field in a different way thus you don't have the 3 separate field which you may have used in your script. Due to this, your script will not work.

    You could change your script as below to make it work with dropdown -

    ===============

    $(document).ready(function () {

       var accountId = "fd287253-e6b9-e711-811c-c4346bde3591" //getParameterByName('iaid');

       if (accountId !== null && accountId !== "") {

           $("#new_account").val(accountId );

       }

    });

    ===============

    Hope this helps.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

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