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)

Javascript - checking a lookup value for null failing

(0) ShareShare
ReportReport
Posted on by 2,665

Hi,

I am writing a odata request in a script which is retrieving the values that I need correctly. Problem is when the retrieved value is null, I am checking if the value is null or not before proceeding further but it ignores the if condition and enters the loop even though the value is null.

Country is a lookup field. Can someone please guide me through this? Thanks.

Here is my script:

function fillAppName()
{
debugger;
var  formType= Xrm.Page.ui.getFormType();
if(formType==1)
{
var userId=Xrm.Page.context.getUserId();

  var serverUrl;
    if (Xrm.Page.context.getClientUrl !== undefined) {
        serverUrl = Xrm.Page.context.getClientUrl();
    } else {
        serverUrl = Xrm.Page.context.getServerUrl();
    }
    var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; 
    var userRequest = new XMLHttpRequest(); 
    userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + Xrm.Page.context.getUserId() + "')", false); 
    userRequest.setRequestHeader("Accept", "application/json"); 
    userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
    userRequest.send();
    if (userRequest.status === 200) {
        var retrievedUser = JSON.parse(userRequest.responseText).d; 
        var userApplication = retrievedUser.new_Application;
        var userCountry=retrievedUser.new_country;
        //return userFullName;

if(userApplication!=null){
var appValue=new Array();
appValue[0]=new Object();
appValue[0].id=userApplication.Id;
appValue[0].name=userApplication.Name;
appValue[0].entityType=userApplication.LogicalName;

Xrm.Page.getAttribute("new_application").setValue(appValue);
}
if(userCountry!=null && userApplication.Name!='xxxx')//it enters the loop here even if userCountry is null
		{
		var countryValue = new Array();
		countryValue[0] = new Object();
		countryValue[0].id = userCountry.Id;
		countryValue[0].name = userCountry.Name;
		countryValue[0].entityType = userCountry.LogicalName;

		Xrm.Page.getAttribute("new_country").setValue(countryValue);
}

    }
    else {
        return "error";
    }
}
}

Thanks for any help!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Roxanne,

    Assign application name in a variable and then use the variable in the if condition like below. You should give space before and after !=.

    var ApplicationName = userApplication.Name;

    if(userCountry != null && ApplicationName != 'xxxx')

  • crmprogrammer2013 Profile Picture
    2,665 on at

    Hi Goutam,

    problem is with the userCountry being null. It still passes the if condition. ApplicationName is working fine for other scenarios. I did add the spaces as you suggested, but the problem still exists.

    Thanks.

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Roxanna,

    Sorry , I don't agree with you .

    How do you know userCountry is getting null?

    If that is the case I would suggest to do following to make it clear , use two if condition.

    var  Application =   userApplication.Name

    if(userCountry != null  && userCountry != undefined)

    {

    if(Application != 'xxxx')

        {

    var countryValue = new Array();

    countryValue[0] = new Object();

    countryValue[0].id = userCountry.Id;

    countryValue[0].name = userCountry.Name;

    countryValue[0].entityType = userCountry.LogicalName;

    Xrm.Page.getAttribute("new_country").setValue(countryValue);

           }

    }

  • Verified answer
    crmprogrammer2013 Profile Picture
    2,665 on at

    Thanks Goutam, its a dumb mistake on my side!!!

    I figured I should be checking the if condition with userCountry's id or name to check for null. Its a lookup field. My bad.

    Its all working now. Thanks for looking into this. :)

  • gdas Profile Picture
    50,091 Moderator on at

    Its happen sometimes :) . Glad to hear that your problem has been resolved.

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