How to add "if" condition in addQuery
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2023 02:22 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2023 02:36 AM - edited 04-21-2023 02:43 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2023 04:43 AM
Hii ,
This is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2023 06:09 AM - edited 04-21-2023 06:11 AM
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>;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2023 01:58 AM
Hii,
It is not updating