0

I'm trying to receive data from a variable I've declared outside the two functions I'm working with.

var AccountOwnerId;

function checkUserContact() {

    var lookup = new Array();
    lookup = Xrm.Page.getAttribute("pp_qcustomerid").getValue();
    if (lookup != null) {
        var name = lookup[0].name;
        var id = lookup[0].id.substring(1, 37);
        var entityType = lookup[0].entityType;
    }

    var res = retrieveRecord(id, "AccountSet", UserRetrievedSuccess, UserRetrievedError, "OwnerId");

    alert(AccountOwnerId);
}

function UserRetrievedSuccess(data, textStatus, XmlHttpRequest) {
    AccountOwnerId = data.OwnerId.Id;
    alert(AccountOwnerId);
    return AccountOwnerId;
}

The alert box in "UserRet.". gets the correct value, but the alert in "checkUs.." gets "UNDEFINED".

Been trying different ways to solve this, but all results in the same problem. So I'm out of ideas.

Ulrich Eckhardt
  • 662
  • 6
  • 14
user1770961
  • 101
  • 2
  • 2
  • 9
  • 1
    well, if you don't set it to anything, it will be undefined. – meskobalazs Oct 15 '15 at 10:22
  • 4
    I'm guessing `retrieveRecord` results in an asynchronous call? If so, see [this question](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call). – James Thorpe Oct 15 '15 at 10:22
  • @meskobalazs It is set, in `UserRetrievedSuccess`, but chances are that's running _after_ the alert in `checkUserContact`. – James Thorpe Oct 15 '15 at 10:23
  • 2
    Seems like alert(AccountOwnerId) is being executed before the value gets set. You would need to work with callback functions here in order to make the porgramm wait for the value to be set – noa-dev Oct 15 '15 at 10:23

0 Answers0