Glide Record on to two table is not working

Community Alums
Not applicable

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.

9 REPLIES 9

Aditya02
Tera Guru

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

Community Alums
Not applicable

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

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

Anurag Tripathi
Mega Patron
Mega Patron

What is SW

 

Looks like you have mistypes d a wrong Glide Record variable name/. It should be SP by the looks of it.

-Anurag

Sid_Takali
Kilo Patron
Kilo Patron

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);
    }
}