Alm_asset cannot access state or substate in a script

ppicazo1
Tera Contributor

In "Scripts - Background" I pasted the code below the dotted line.   I really want to update the substate field but I see when I run the script below it comes up undefined.   Increasing the size of x does not matter.   I tried to do an update based on what Google's AI told me but it did not work.   I even tried a setValue but it did not work or give me an error.   Can anyone help me?

-------------------------------------------------------------------------------------------------------------------------------------------------

var assetUnAssign = new GlideRecord('alm_asset');
assetUnAssign.query();
var x = 0;
while (assetUnAssign.next())
{  
    gs.print(assetUnAssign.substate);
    x++;
    if (x > 8)
        break;
}
3 REPLIES 3

RaghavSh
Kilo Patron

Try below:

 

var assetUnAssign = new GlideRecord('alm_asset');
assetUnAssign.query();
var x = 0;
while (assetUnAssign.next())
{  
    gs.print(assetUnAssign.substatus);
    x++;
    if (x > 8)
        break;
}
 There has to be a <space> between new and GlideRecord() as above.
 
also there could be the possibility that 8 records this query is picking does not have substate.
 

Raghav
MVP 2023

Chavan AP
Kilo Sage

@ppicazo1 - 

Get the substatus choices from sys_choice table- 

var asset = new GlideRecord('sys_choice');
asset.addEncodedQuery('nameSTARTSWITHalm_asset^element=substatus^language=en');
asset.query();
while (asset.next()) {
    
    gs.print("state: " + asset.value);
}

then decide which substatus you want to update then use below script: 

 

var asset = new GlideRecord('alm_asset');
// Add conditions as needed, e.g.:
// asset.addQuery('state', 'in_use');
asset.setLimit(100); // Safety limit
asset.query();

var count = 0;
while (asset.next()) {
    asset.setValue('substatus', 'pending_repair');// choose required substatus
    asset.update();
    count++;
    gs.print('Updated: ' + asset.number);
}

gs.print('Total updated: ' + count);
Glad I could help! If this solved your issue, please mark it as Helpful and Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****

Chaitanya ILCR
Kilo Patron

Hi @ppicazo1 ,

 

Backend name of the SubState field is sbustatus

replace substate with substatus and run the script again

var assetUnAssign = new GlideRecord('alm_asset');
assetUnAssign.query();
var x = 0;
while (assetUnAssign.next())
{  
    gs.print(assetUnAssign.substatus);
    x++;
    if (x > 8)
        break;
}

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya