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)

Getting lookup value on load

(0) ShareShare
ReportReport
Posted on by

I have a form that requires two lookups.  The first one is loaded with a value from the previous form that it is being added from.  I want to get the id of that lookup in the onLoad event, but it is not loaded yet.  This code works in an onChange event, so I know the code is sound.

var thisRMA = Xrm.Page.getControl("lena_fr_rma");

var curRMAKey = thisRMA.getAttribute().getValue()[0].name;

I get the curRMAKey.

BUT - the onLoad seems to run before the value is actually loaded into the lookup.  Is there a way to wait until the values are loaded on to the form?  Both values are Business Required, so I can't run this in the onSave event.

*This post is locked for comments

I have the same question (1)
  • Suggested answer
    Mahadeo Matre Profile Picture
    17,021 on at

    Hi..

    You can add wait  by using setTimeout javascript function.

     setTimeout(Getlookup, 2000);

    parameters for this function are, GetLookup: your function name, 2000- time in mil. sec.

    your code will be similar to below.. code will wait until lena_fr_rma is not loaded.

    function Getlookup()

    {

     var thisRMA = Xrm.Page.getControl("lena_fr_rma");

     if(thisRMA == null)

    {

      setTimeout(Getlookup, 2000);

      return;

    }

      var curRMAKey = thisRMA.getAttribute().getValue();

    }

    Hope this will help..

  • Suggested answer
    Bruno Lucas Profile Picture
    5,421 on at

    Have you tried to separate in two methods and load one first? maybe you can do that along with a call-back. something like this: nycrmdev.blogspot.co.nz/.../an-alternative-approach-to-loading-form.html

    last resource you can use javascript wait but it is not the very best approach because it will cause a delay.

  • Community Member Profile Picture
    on at

    Yeah, I thought about that, but the control is not returning null.  I think it's because it's a lookup.  Here is the code:

       var thisRMA = Xrm.Page.getControl("lena_fr_rma");

       if (document.getElementById('lena_fr_rma') != null) {

           if ((typeof (thisRMA) != "undefined" && thisRMA != null)) {

               var rmaid = thisRMA.getAttribute().getValue()[0].id;

               var dlpid = getDLPSN(rmaid);

           }

       } else {

           setTimeout("FindControl()", 1000);

       }

    As I step through the code, it passes through the if statements, meaning the control is NOT null or undefined, but when it gets to the following statement it gives an error.

    var rmaid = thisRMA.getAttribute().getValue()[0].id;

    Here is the error: "Unable to get property '0' of undefined or null reference"

  • Verified answer
    Mahadeo Matre Profile Picture
    17,021 on at

    try this way..

     function getLookup() {
    
                var thisRMA = Xrm.Page.getControl("lena_fr_rma");
                if (thisRMA == null) {
                    setTimeout(getLookup, 1000);
                }
                else if (thisRMA.getAttribute() == null || thisRMA.getAttribute().getValue()) {
                    setTimeout(getLookup, 1000);
                }
                else
                {
                    var rmaid = thisRMA.getAttribute().getValue()[0].id;
                    var dlpid = getDLPSN(rmaid);
                }
            }
    


  • Community Member Profile Picture
    on at

    Mahadeo,

    I just had to change one thing:

    else if (thisRMA.getAttribute() == null || !thisRMA.getAttribute().getValue())

    I added a NOT (!) for the second part.

    This now retrieves the value - thx.

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