How do I use gr.setValue();?

Anshul Duvey
Giga Contributor
 
8 REPLIES 8

Nivedita Patil
Mega Sage
Mega Sage

Hi @Anshul Duvey  ,

 

Please refer the below link.

https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/c_GlideRecordAPI#r_GlideR...

 

Mark my answer as accepted solution and helpful if helps you.

 

Thanks,

Nivedita Patil.

Harish Bainsla
Tera Sage
Tera Sage

// Assuming 'incident' is the table name
var gr = new GlideRecord('incident');

// Query for a specific record (you might want to add more conditions based on your use case)
gr.addQuery('number', 'INC001234');
gr.query();

// Check if the record was found
if (gr.next()) {
// Set the value of the 'short_description' field to 'Updated Short Description'
gr.setValue('short_description', 'Updated Short Description');

// Save the changes to the record
gr.update();
} else {
gs.info('Record not found');
}

Aakriti_Poudel
Tera Contributor

whats the difference between 

gr.setValue('short_description','Test short description');
//and

gr.short_description = 'Test short description';

Hi @Aakriti_Poudel 

you probably found your answer by now, but if it can be useful for anyone. 

 

Normally a script would do a direct assignment, for example now_GR.category = value. However, if in a script the element name is a variable, then you can use now_GR.setValue(elementName, value). When setting a value, ensure that the data type of the field matches the data type of the value you enter.

If the value parameter is null, the record isn't updated, and an error isn't thrown.

 

see : https://developer.servicenow.com/dev.do#!/reference/api/yokohama/server_legacy/c_GlideRecordAPI#r_Gl...