Glide Record on to two table is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2024 09:58 AM
Hi All,
I had a requirement to find if there is any match of product in the custom software product table to the software Model table. If there is no match found then de active the custom software product.
I have written a fix script but it is not updating, not sure where i am missing the logic.
var sp=new GlideRecord("samp_custom_sw_product");
sp.addEncodedQuery("active=true");
sp.query();
while(sp.next())
{
var sm=new GlideRecord("cmdb_software_product_model");
sm.addQuery('product',sp.prod_name);
sm.query();
if(!sm.next())
{
sw.active=false;
sw.setWorkflow(false);
sw.update();
gs.log("prod name :"+sp.prod_name);
}
}
Anyone can please help me on this,
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2024 10:47 AM
Hi @Community Alums .
You can try this script to achieve your functionality:
var sp=new GlideRecord("samp_custom_sw_product");
sp.addEncodedQuery("active=true");
sp.query();
while(sp.next())
{
var sm=new GlideRecord("cmdb_software_product_model");
sm.addQuery('product',sp.prod_name);
sm.query();
while(!sm.next()) {
sp.active=false;
sp.setWorkflow(false);
sp.update();
gs.log("prod name :"+sp.prod_name);
}
}
Changes did at:
-> while doing gliderecord for second table if you deactive all the not matched product, You have use 'While' instead of 'If'.
-> change sw to sp . There is no sw object in the script you mentioned.
If my solution helps you to get solve your problem, Make my reply as Correct answer and give me a like.
Thanks,
Aditya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2024 04:34 AM
Hi Aditya,
I have written a logic using array,it is working fine in background script,but I need to run this on regularly,So I tried the same code in schedule job that run 12 pm us/central time zone.But I execute the schedule the code is not running
gs.log("Deactivated product schedule job is started running");
var productModelGR = new GlideRecord('cmdb_software_product_model');
productModelGR.query();
// Store all existing product models in an array
var productModelArray = [];
while (productModelGR.next()) {
productModelArray.push(productModelGR.getDisplayValue('product'));
}
gs.info('Product Model Array: ' + JSON.stringify(productModelArray));
var customProductGR = new GlideRecord('samp_custom_sw_product');
customProductGR.addQuery('active', true);
customProductGR.query();
while (customProductGR.next()) {
var productName = customProductGR.getDisplayValue('prod_name');
if (productModelArray.indexOf(productName) === -1) // No match found, deactivate the custom product
{
customProductGR.setValue('active', false);
customProductGR.update();
gs.log('Deactivated product: ' + productName);
}
}
Not sure,Why the scheduled job is not running
can you please help me on this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2024 06:21 AM
Hi @Community Alums ,
May i know, Where you are getting error in this script. In program, at item line you are failing to execute.
Check once at this line:
if (productModelArray.indexOf(productName) === -1) // No match found, deactivate the custom product
i think here its failing check once.... by putting any log here.. check once with '==' instead of '==='.
Thanks,
Aditya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2024 06:26 AM
What is SW
Looks like you have mistypes d a wrong Glide Record variable name/. It should be SP by the looks of it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2024 06:56 AM
Hi @Community Alums Try below code
var sp = new GlideRecord("samp_custom_sw_product");
sp.addEncodedQuery("active=true");
sp.query();
while (sp.next()) {
var sm = new GlideRecord("cmdb_software_product_model");
sm.addQuery('product_name', sp.prod_name); // Assuming 'product_name' is the correct field in cmdb_software_product_model
sm.query();
if (!sm.hasNext()) { // Use hasNext to check for match existence
sp.active = false;
sp.setWorkflow(false);
sp.update();
gs.log("Deactivated custom software product: " + sp.prod_name);
}
}