Fix Script to update Lifecycle Stage on business applications

Glenn H
Tera Guru

We are running into an issue updating the life cycle stage field on the business application table.  The field is a reference to another table but it only has Name on that table.  When we run the below script, its showing the End of Life entry as a new entry into the table when it is actually an entry in the table life_cycle_stage.

 

Anyone run into this and have any thoughts?

 

var gr = new GlideRecord('cmdb_ci_business_app');
// 6 = decommission
// 3 = retired
gr.addEncodedQuery('install_status=3');
gr.setWorkflow(false);
gr.setLimit('10');
gr.query();
 
while(gr.next()) {
    gr.setValue('install_status', '3');
    // "end of life"
    gr.setValue('life_cycle_stage', 'End of Life');
    gr.update();
}
3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Glenn H 

try this

gr.setDisplayValue('life_cycle_stage', 'End of Life');

OR

gr.life_cycle_stage.setDisplayValue('End of Life');

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

Hi @Glenn H  ,

The issue is that the life_cycle_stage field is a reference field, so it expects a Sys ID (unique identifier) from the referenced table, not a text value. When you set 'End of Life' directly, ServiceNow interprets it as a new entry and creates it in the life_cycle_stage table.

To resolve this, you need to first query the Sys ID of the End of Life entry in the life_cycle_stage table and then use that Sys ID in the update. Here's the corrected script:

 

var gr = new GlideRecord('cmdb_ci_business_app');
// Query for business applications with install_status = retired (3)
gr.addEncodedQuery('install_status=3');
gr.setWorkflow(false);
gr.setLimit(10);
gr.query();

while (gr.next()) {
    var lifeCycleStage = new GlideRecord('life_cycle_stage');
    if (lifeCycleStage.get('name', 'End of Life')) { // Query for "End of Life" in the referenced table
        gr.setValue('life_cycle_stage', lifeCycleStage.sys_id); // Set the reference using the Sys ID
    }
    gr.update();
}

 

 

 

  • Fetch Sys ID: The script queries the life_cycle_stage table to find the Sys ID of the "End of Life" entry.
  • Set Sys ID: Use lifeCycleStage.sys_id to update the reference field instead of directly setting a text value.

 

Anand Kumar P
Giga Patron
Giga Patron

Hi @Glenn H ,

 

I have tried in my PDI its working with same script comment line  gr.setValue('install_status''3'); because you are getting retired status business app in encoded query no need setting again install_status to 3.

 

When i update life_cycle_stage field with End of Life value ,  below script is updating with the OOB End of Life value  sys_id 6cc34291b7620010ee0d3177ee11a9f8 not creating any new entries in life_cycle_stage table.

var gr = new GlideRecord('cmdb_ci_business_app');
gr.addEncodedQuery('install_status=3');
gr.setWorkflow(false);
gr.setLimit('1');
gr.query();
while(gr.next()) {
    gr.setValue('life_cycle_stage', 'End of Life');
    gr.update();
}

 

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand