Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Setting Lookup field value in CRM Portals

Posted on by 480

Hi everyone,

I need to set a lookup value on Entity Form in CRM Portals. On Entity form I have a button which opens a form in modal where look is present but I am unable to set lookup value.

What I am doing is:-

1. From URL getting the id of the record
2. Running a fetchxml in liquid code 
3. Inside {% if > 0 %} condition I am trying to set lookup's value like below using the value i have stored in variables.

 

{% fetchxml eventt %}
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="new_event">
    <attribute name="new_name" />    
    <attribute name="new_eventid" />
    <order attribute="new_name" descending="false" />
    <filter type="and">
      <condition attribute="new_eventid" operator="eq" value="{{request.params['id']}}" />
    </filter>
  </entity>
</fetch>

{% endfetchxml %}


{% if eventt.results.entities.size > 0 %}

{% assign eventID = eventt.results.entities[0].new_eventid %}
{% assign eventName = eventt.results.entities[0].new_name %}


<script>
    $(document).ready(function() {
alert("before set ");
       
      $("#new_event_name").attr("value", "{{eventName }}");
      $("#new_event").attr("value", "{{eventID}}");
      $("#new_event_entityname").attr("value", "new_event");

      
alert("after set");
    });
  </script>

{% endif %}

My Form inside Webform (It has 3 step):-

1004.Tt.png

I was able to set the lookup value using similar code for another project but this isn't working.

Thanks in Advance.

*This post is locked for comments

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Setting Lookup field value in CRM Portals

    Hi Ananta,

    Please  refer the below thread on populating lookup values-

    community.dynamics.com/.../setting-value-on-lookup-field-using-javascript-in-crm-portals

    If this doesn't work, create a new thread and post what you tried and what didn't work along with your own requirements.

    Hope this helps.

  • AnantaYogi Profile Picture
    AnantaYogi 45 on at
    RE: Setting Lookup field value in CRM Portals

    Hi Rizwan,

    Can you please share the code. I am also trying to set lookup value but it's not working.

  • AnantaYogi Profile Picture
    AnantaYogi 45 on at
    RE: Setting Lookup field value in CRM Portals

    Can anyone have a solution for this? I am also trying to do that. If anyone has code for this, it would be highly appreciable 

  • Verified answer
    Rizwan Aarif Profile Picture
    Rizwan Aarif 480 on at
    RE: Setting Lookup field value in CRM Portals

    Hi Everyone,

    Thanks for your help so far. This has been solved now.

    The Popup/Modal was opening in a different iframe control that's why it was not being found using document.getElementById or other methods. I was able to retrieve the controls using code like

    $('iframe').contents("controlsName")

    What I did is:-

    • Added a custom handler on the click of the button opening popup
    • Added a wait of 3 seconds to the handler so it allows the popup to fully load and then sets the Lookup value
  • Verified answer
    Rizwan Aarif Profile Picture
    Rizwan Aarif 480 on at
    RE: Setting Lookup field value in CRM Portals

    Hi Era,

    I tried printing the "Size" or result which I am getting and it's "1" means I am able to fetch event record. But not getting any alert message.

  • Suggested answer
    Irena Benja Profile Picture
    Irena Benja 412 on at
    RE: Setting Lookup field value in CRM Portals

    Hello,

    Do you ever get the alert messages?

    Try to print somewhere the size of the fetch rezult, probably you are receiving -1, in that case I would check the entity permissions.

    If you get the records but fail to populate the field I would try to print somewhere on the page the eventName and eventID and run the Jquery form the browser console, it will be easy to find the error there.

    $("#new_event_name").attr("value", "{{eventName }}");

    would also work without "" sorrounding the your variable > $("#new_event_name").attr("value", {{eventName }});

    Regards,

    Irena,

  • Rizwan Aarif Profile Picture
    Rizwan Aarif 480 on at
    RE: Setting Lookup field value in CRM Portals

    Hi Sandeep,

    I have checked and I can confirm I have (as authenticated user) have access to that entity (form which is opening in model)

    Can you suggest what else could be the reason, the similar code works in another project.

  • Suggested answer
    sandeepstw Profile Picture
    sandeepstw 4,601 on at
    RE: Setting Lookup field value in CRM Portals

    Hi,

    Please entity permission of another form for add and edit.

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