update state field by script

Shabbir1
Tera Contributor

Hi team

 

On RITM form we have UI action button after clicking that approval will be trigger script is working fine. But if approver approves the RITM i have to change the state to assigned ..how can we achieve this without workflow. If approver approves that particular RITM i have to change state can anyone help me in the script .I am using below script for approval triggering in UI action

 

var approval_gr = new GlideRecord('sysapproval_approver');
approval_gr.initialize();
approval_gr.state = 'requested';
approval_gr.approver = current.requested_for.manager; //Replace with approver from current record
approval_gr.sysapproval = current.sys_id; //Current is record that requires approval
approval_gr.insert();
4 REPLIES 4

Slava Savitsky
Giga Sage

You could do it with a business rule on [sysapproval_approver] table that would:

  1. run when the state of the approval record changed to Approved
  2. get the RITM record to which the approval record belongs (based on the value of sysapproval field)
  3. change the state of the RITM record as needed

But what is the reason for not using a workflow?

 HI slava,

 

Because after submitting the catalog item there is no approval customer requirement is in case if they want any approval they will click on UI action button so that we are raising approval manually. Can anyone share the script it would be helpful for me thanks.

In my opinion, this is not a valid reason for not using a workflow. You could have a workflow that would wait for the customer to trigger the approval. The appropriate solution depends on what the entire process looks like.

melwintnb
ServiceNow Employee
ServiceNow Employee

Hi @Shabbir1 ,

You can create after business rule in "sysapproval_approver" table and set condition as state changes and changes to approved.
And go with below script to update the state.

var ritm = new GlideRecord('sc_req_item');
 if(ritm.get(current.getValue('document_id')))
 {
 ritm.state = ‘approved’; // Required the backend value
ritm.update();
}


If you wants the this BR to run only for specific catalog item, then you can define condition in BR filter condition.