Background script to update date field

ceraulo
Mega Guru

Hello!

I need to update the retirement date of assets with a specific date using background script. This is my script but it does not work.

var gr = new GlideRecord('clm_m2m_contract_asset');
gr.addEncodedQuery('<insert query>');
gr.query();
while (gr.next()) {
   gr.retirement_date = '2022-01-30';
   gr.setWorkflow(false);
   gr.update();
}

Please help!

Thanks.

5 REPLIES 5

Vishal Savajia1
Kilo Sage

Try in this way

var gr = new GlideRecord('change_request');
gr.addQuery('number','CHG123456');
gr.query();
if(gr.next())
{
gr.start_date = new GlideDateTime("2020-08-27 13:30:00");
gr.end_date = new GlideDateTime("2020-08-28 00:30:00");
gr.u_outage_start_date = new GlideDateTime("2020-08-27 13:30:00");
gr.u_outage_end_date = GlideDateTime("2020-08-28 00:30:00");
gr.u_closed_by_date = GlideDateTime("2020-09-04 14:30:00");
gr.setWorkflow(false);
gr.update();
gs.print(gr.number);
}

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So what date it set?

Are you using correct query and got correct count of records?

Is the field name correct?

var gr = new GlideRecord('clm_m2m_contract_asset');
gr.addEncodedQuery('<insert query>');
gr.query();
gs.info('Row Count' + gr.getRowCount());
while (gr.next()) {
    gr.retirement_date = '2022-01-30';
    gr.setWorkflow(false);
    gr.update();
}

Regards
Ankur

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

@ceraulo 

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

 

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

@ceraulo 

Hope you are doing good.

Did my reply answer your question?

Would you mind marking the best matching answer as correct and helpful, to close this thread?

Thanks!
Ankur

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