Want to write a fix script for removing value of a particular field in a form

Sayan
Mega Contributor

I want to write a fix script for a list of records in a table. There is a field in my form that has a particular type of value. I want to remove that value and make it empty,

1 ACCEPTED SOLUTION

var tab = new GlideRecord('your_table');
tab.addEncodedQuery('Your_encoded_query_here');
tab.query();
while(tab.next()){
tab.your_field_name = '';
tab.setWorkflow(false); //helps in prevent running of BR's while updating
tab.autoSysFields(false);
tab.deleteRecord();
}

Please mark my response as correct and helpful if it helped solved your question.
-Thanks

View solution in original post

6 REPLIES 6

Mohit Kaushik
Mega Sage
Mega Sage

Hi Sayan,

You can write the script something like this :

// suppose field is short description which you want to make empty
var rec = new GlideRecord('incident');
rec.addNotNullQuery('short_description'); //to fliter out records which has the values.
rec.setLimit(1); //remove this limit after testing one record whether scripts working fine or not.
rec.query();
while (rec.next()) {
rec.short_description = '';

rec.setWorkflow(false);
rec.update();
}

 

Refer this link to know more about fix scripts in servicenow:

https://docs.servicenow.com/bundle/orlando-application-development/page/build/applications/concept/c...

 

You can even use the above script in background script also.

 

Please mark this answer as correct and helpful it resolves the query and helpful alone if it lead you in right direction.

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Prateek kumar
Mega Sage

Try below:

var tab = new GlideRecord('your_table');
tab.addEncodedQuery('Your_encoded_query_here');
tab.query();
while(tab.next()){
tab.your_field_name = '';
tab.setWorkflow(false); //helps in prevent running of BR's while updating
tab.autoSysFields(false);
tab.update();
}

Please mark my response as correct and helpful if it helped solved your question.
-Thanks

What if I have to delete the whole record itself then what should be the code

var tab = new GlideRecord('your_table');
tab.addEncodedQuery('Your_encoded_query_here');
tab.query();
while(tab.next()){
tab.your_field_name = '';
tab.setWorkflow(false); //helps in prevent running of BR's while updating
tab.autoSysFields(false);
tab.deleteRecord();
}

Please mark my response as correct and helpful if it helped solved your question.
-Thanks