How to add if condition in GlideRecord

Shalika
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 locName = "";

var loc = new GlideRecord('cmn_location');

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 ="d2ca78121b5f5d5014811023b24bcb56";
}

It is not working for r the 2nd condition, i.e. to take the 1st word of the Location. 

Where am I wrong in my code

3 REPLIES 3

Upsilon
Giga Guru

Hi, 

I think that your encoded query is not OK. You have not operator neither AND (^) nor (^NQ). It will retrieve all table records

 

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

var loc = new GlideRecord('cmn_location');

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

 

 Regards

What will be the encoded query?

Community Alums
Not applicable

Hi @Shalika ,

As @Upsilon mentioned the encoded query should contain operators.

ex: gr.addEncodedQuery('nameSTARTSWITHs^emailSTARTSWITHsa');

to avoid ambiguity use addquery.

ex: gr.addQuery('name','STARTSWITH',locFirstName1);