How to add "if" condition in addQuery

surbhi_123
Tera Expert

I have two tables chromebook and computer table having a field location which is referring to Location table.

I have to update the location value from chromebook table to computer table. The scenario is as follows - 

1. If location given in Chromebook table matches with any location of location table then update as it is in Computer table.

2. Suppose the location in Chromebook table is Espoo 23 and the first word i.e. Espoo matches with any record in location table then update the 1st word of the location in the Computer table.

3. If no records is matching with the locations in the Location table, then update the location field with a dummy value in Computer table.

I have written the BR for this as follows -

 

var locFirstName = current.u_location;
var locFirstName1 = locFirstName.split(' ')[0];

var loc = new GlideRecord('cmn_location');
loc.addQuery('full_name', current.u_location.toString());
loc.query();

if (loc.next()) {
var locName = loc.sys_id;
}

else if(loc.next()==locFirstName1)
{
locName=loc.sys_id;
}
else {
locName = "d2ca78121b5f5d5014811023b24bcb56";
}

 

It is working for all the conditions except for the 2nd condition, i.e. to take the 1st word of the Location. 

Where am I wrong in my code

7 REPLIES 7

Prince Arora
Tera Sage
Tera Sage

@surbhi_123 

 

Please add one more Query as(Not tested just give you the idea):

 

 

var locFirstName = current.u_location;
var locFirstName1 = locFirstName.split(' ')[0];

var loc = new GlideRecord('cmn_location');
loc.addQuery('full_name', current.u_location.toString());

loc.addEncodedQuery("full_nameSTARTSWITH" + locFirstName1);
loc.query();

while(loc.next()){

if(loc.full_name == locFirstName){

locName=loc.sys_id;


}else if((loc.full_name).indexOf(locFirstName1)!=-1){

locName=loc.sys_id;

}

}

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

Hii ,

This is not working

@surbhi_123 ,

 

As Prince Arora stated, the code hadn't been tested yet. I noticed the query before the encoded query may conflict with the encoded query. Also, there was no where to meet your 3rd condition so I added it. If this doesn't work, please let us know what results you are seeing so we can further help you.

 

var locFirstName = current.u_location;
var locFirstName1 = locFirstName.split(' ')[0];
var locName = "";

var loc = new GlideRecord('cmn_location');
//loc.addQuery('full_name', current.u_location.toString()); //This line conflicts with the encoded query in the original script

loc.addEncodedQuery("full_name="+locFirstName + "full_nameSTARTSWITH" + locFirstName1);
loc.query();

while(loc.next()){

  if(loc.full_name == locFirstName){

    locName=loc.sys_id;


  }else if((loc.full_name).indexOf(locFirstName1)!=-1){

    locName=loc.sys_id;

  }

}
if (locName == "") {
  locName = <your_sys_id_here>;
}

 

 

Hii,
It is not updating