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

I checked using logs that what value is coming in loc.full_name , the value is showing as undefined

I'm not sure where you are logging the loc.full_name at, but it looks like the encoded query may be incorrectly formatted. It should have a "^OR" string in there to indicate joining on an or condition. The encoded query should look like: 

loc.addEncodedQuery("full_name="+locFirstName + "^ORfull_nameSTARTSWITH" + locFirstName1);

additionally, we can't see the fields in your tables, but on my table, the field is "name" not "full_name" so you may need to swap out each instance of "full_name" with "name". Do some other checks against your table to ensure that the script makes sense and let us know what the results are.

Hii,

I used the query as 
loc.addEncodedQuery("name="+locFirstName + "^ORnameSTARTSWITH" + locFirstName1);

But if the location is Espoo head Office in Chromebook Table then it is updating as Espoo DC location in Computer table. 
Note - There are two locations - Espoo and Espoo DC in Location table