Best way to change the state field if any update on the record

Chaiatnya
Tera Contributor

Hi, I've a custom table and with nearly 20 fields. There is a "state" field with values 1 to 5. when ever any field changes on the form except "State" field and "description(type: string field)"field the state should set the value to 1.

This can be accomplished by business rule "update" action and adding all the fields in condition in "when to run".

But is there any other way to accomplish this, because in future if some one add new field and if they have not updated the field in the business rule the functionality will not work.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can easily handle this using this approach

1) create a system property which would hold the field names such as state,description -> these are the fields for which BR should not run

2) in the script section you can determine which fields got changed; store this into an array

3) then check if the changed fields contains any of the system property value

a) if yes then your logic

b) if not then do nothing

Refer below link on how to get the changed fields in BR

Checking for Modified or Changed Fields in Script

Regards
Ankur

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

View solution in original post

4 REPLIES 4

Pranav Bhagat
Kilo Sage

Its better to create a state handler script include and add the logic there .You can check the OOTB state handlers for incident and change.

 

Go through this state model document

 https://docs.servicenow.com/bundle/paris-platform-administration/page/administer/state-model/concept/state-model.html

 

https://docs.servicenow.com/bundle/london-it-service-management/page/product/change-management/task/t_UpdateStateHandlerScriptInclude.html

Regards

Pranav

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can easily handle this using this approach

1) create a system property which would hold the field names such as state,description -> these are the fields for which BR should not run

2) in the script section you can determine which fields got changed; store this into an array

3) then check if the changed fields contains any of the system property value

a) if yes then your logic

b) if not then do nothing

Refer below link on how to get the changed fields in BR

Checking for Modified or Changed Fields in Script

Regards
Ankur

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

manjusha
Kilo Guru

Hi,

You can use state model functionality in servicenow to above functionality

Thanks,

Manjusha Bangale

AkshatRastogi
Mega Guru
Use below function to get changed fields on server side, add this in the business rule, 
this will work for scoped apps as well:

var changed _fields = getChangedFieldNames(current); function getChangedFieldNames(gr) { var result = []; var elements = gr.getElements(); var size = elements.length; for (var i = 0; i < size; i++) { var ge = elements[i]; if (ge.changes()) { result.push(ge.getName()); } } return result; }