Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Trouble with Xrm.WebApiretrieveRecord When Lookup is Blank

Posted on by 25

Hi All,

Did some searching and didn't really find an answer to this. To start with, here's the code I'm executing as a JS webresource onLoad of the and onChange of the customerid field on the Order entity.

Xrm.WebApi.retrieveRecord("account", <shipToCustomerGUID>, "?$expand=myLookupField($select=accountid,name)").then(
function success(result) {
if (result != null) {
if (verboseLogs) alert("Web API Called Successfully");
var myLookup = result.myLookupField;
if (verboseLogs) alert("myLookup: " + myLookup);
var myLookupName = result.myLookupField.name;
if (verboseLogs) alert("myLookupName:" + myLookupName);
var myLookupId = result.myLookupField.accountid;
if (verboseLogs) alert("myLookupId: " + myLookupId);
if (verboseLogs) alert("Setting to myLookup");
var myLookupArray = new Array();
myLookupArray[0] = new Object();
myLookupArray[0].id = myLookupId;
myLookupArray[0].name = myLookupName;
myLookupArray[0].entityType = "account";
Xrm.Page.getAttribute("customerid").setValue(myLookupArray);

}
else {
if (verboseLogs) alert("Clearing customerid");
Xrm.Page.getAttribute("customerid").setValue(myLookupArray);.setValue(null) && alert("Please choose a valid account");
}
},
function (error) {
if (verboseLogs) alert(error.message);
});

Now, when I pass a GUID of an account record that has myLookupField populated, everything goes swimmingly. I can turn on verbose logs and watch the whole thing unfold line by line.

However, if myLookupField does not contain data, it still appears as if the API is successfully called, but I don't get any alerts after if (verboseLogs) alert("myLookup: " + myLookup). It's almost like the function dies after that point. None of my remaining variables alert (at all), it doesn't try to build the array, nothing. I even have the whole thing nested in a try/catch, and it doesn't trigger the catch. Can anyone offer any insight as to how this is behaving? And how I might accomplishthe following:

If myLookupField (on the ShipToCustomer account record) contains a value, build an array from that and push it into customerid

if myLookupField is blank, clear out customerid on the order and display an alert

*This post is locked for comments

  • necsa Profile Picture
    necsa 3,455 on at
    RE: Trouble with Xrm.WebApiretrieveRecord When Lookup is Blank

    Hi,

    Please can you try following code. (match your lookup field name)

    function GetParentAccount(){
        var account=Xrm.Page.getAttribute("customerid").getValue();
        if(account != null && account[0] != null){
            Xrm.Page.getAttribute("customerid").setValue(null);
            var accountId=account[0].id.slice(1,-1);
            var filter="?$select=_nec_shiptocustomer_value";
        
            Xrm.WebApi.retrieveRecord("account",accountId,filter).then(    
            function success(result){
                
                
                alert("Web API Called Succesfully");
                var myLookup=result._nec_shiptocustomer_value;
                if(myLookup !=null){
                    
                    alert("myLookup: " + myLookup);
                    var myLookupName=result['_nec_shiptocustomer_value@OData.Community.Display.V1.FormattedValue'];
                    
                    alert("myLookupName: " + myLookupName);
                    var myLookupId= result._nec_shiptocustomer_value;
                    
                    alert("myLookupId : " + myLookupId);
                    
                    alert("Setting to myLookup");
                    var myLookupArray=new Array();
                    myLookupArray[0]= new Object();
                    myLookupArray[0].id=myLookupId;
                    myLookupArray[0].name=myLookupName;
                    myLookupArray[0].entityType="account";
                    
                    Xrm.Page.getAttribute("customerid").setValue(myLookupArray);
                }
                  else{
                alert("Clearing customerid");
                Xrm.Page.getAttribute("customerid").setValue(null);
                alert("Please choose a valid account");
            }

        },
        function(error){
            
            alert(error.message);
        });
        }

    }

  • Suggested answer
    Aditya Profile Picture
    Aditya 260 on at
    RE: Trouble with Xrm.WebApiretrieveRecord When Lookup is Blank

    You should put a validation after this step var myLookup = result.myLookupField;

    if(myLookup !=null)

    {

    if (verboseLogs) alert("myLookup: " + myLookup);
    var myLookupName = result.myLookupField.name;
    if (verboseLogs) alert("myLookupName:" + myLookupName);
    var myLookupId = result.myLookupField.accountid;
    if (verboseLogs) alert("myLookupId: " + myLookupId);
    if (verboseLogs) alert("Setting to myLookup");
    var myLookupArray = new Array();
    myLookupArray[0] = new Object();
    myLookupArray[0].id = myLookupId;
    myLookupArray[0].name = myLookupName;
    myLookupArray[0].entityType = "account";
    Xrm.Page.getAttribute("customerid").setValue(myLookupArray);

    }

  • Mr. Dynamics Profile Picture
    Mr. Dynamics 25 on at
    RE: Trouble with Xrm.WebApiretrieveRecord When Lookup is Blank

    You're very close. I want to retrieve the account in a "ship to" field, and based on the contents of the myLookupField on that Account, I want to update the customerid field or clear customerid out and alert. Like this:

    WebAPIIssue.png

  • necsa Profile Picture
    necsa 3,455 on at
    RE: Trouble with Xrm.WebApiretrieveRecord When Lookup is Blank

    Hi,

    just to be sure. You have 'customerid' fields as Potential Customer on the Order. You retrieve the Account over this field and check myLookupField. If it has a value you want to replace same value on the Potential Customer ('customerid') field and if it is blank you want have alert that user must select an account for myLookupField and WebAPI fill the 'customerid'

    Am I correct understand?

  • Mr. Dynamics Profile Picture
    Mr. Dynamics 25 on at
    RE: Trouble with Xrm.WebApiretrieveRecord When Lookup is Blank

    Hi Necdet,

    myLookupField is on the Account entity. The script runs on the Order, gets the GUID of Account A (selected in a lookup on the Order), and then use the expand to grab the accountid and name from Account B, which is selected in myLookupField on Account A. The issue occurs when myLookupField is blank, but I have no way of testing that before running the API call, do I?

  • necsa Profile Picture
    necsa 3,455 on at
    RE: Trouble with Xrm.WebApiretrieveRecord When Lookup is Blank

    Hi,

    please just to clearify. myLookupField is on the Account entity or Order?

  • Mr. Dynamics Profile Picture
    Mr. Dynamics 25 on at
    RE: Trouble with Xrm.WebApiretrieveRecord When Lookup is Blank

    Ravi,

    I've updated my code with your suggestions and I'm still observing the same behavior. The script still alerts "Web API Called Successfully", but nothing happens after that. No further alerts, no data is updated. Any thoughts?

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Trouble with Xrm.WebApiretrieveRecord When Lookup is Blank

    Hi,

    This is because when there is no values, the statement "result.myLookupField.name" will throw an error because result.myLookupField would be null. I have tried and changed your code as below. Please try this,

    ===========

    function GetParentAccount() {

       var verboseLogs = true;

       var account = Xrm.Page.getAttribute("new_account").getValue();

       if (account != null && account[0] != null) {

           var accountId = account[0].id.slice(1, -1);

           Xrm.WebApi.retrieveRecord("account", accountId, "?$expand=parentaccountid($select=accountid,name)").then(

               function success(result) {

                   if (result != null) {

                       if (verboseLogs) alert("Web API Called Successfully");

                       var myLookup = result.parentaccountid;

                       if (myLookup != null) {

                           if (verboseLogs) alert("myLookup: " + myLookup);

                           var myLookupName = result.parentaccountid.name;

                           if (verboseLogs) alert("myLookupName:" + myLookupName);

                           var myLookupId = result.parentaccountid.accountid;

                           if (verboseLogs) alert("myLookupId: " + myLookupId);

                           if (verboseLogs) alert("Setting to myLookup");

                           var myLookupArray = new Array();

                           myLookupArray[0] = new Object();

                           myLookupArray[0].id = myLookupId;

                           myLookupArray[0].name = myLookupName;

                           myLookupArray[0].entityType = "account";

                           Xrm.Page.getAttribute("parentaccountid").setValue(myLookupArray);

                       }

                       else {

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

                       }

                   }

                   else {

                       if (verboseLogs) alert("Clearing customerid");

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

                       alert("Please choose a valid account");

                   }

               },

               function (error) {

                   if (verboseLogs) alert(error.message);

               });

       }

    }

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

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans