How do I add to work notes with the comments , when the Sate is changed

NAIDILE S
Tera Contributor

Hi All

 

I have a Table called "vulnerable Item". In this table we have a related list "Requested Approvals" 
when this request is approved ( the sate is deferred), in the work notes, i need to populate this message "Request for deferral has been approved until [UNTIL DATE]." this until date should also be displayed .
how can i do this , can anyone help me.


Thanks,
Naidile 

4 REPLIES 4

Rahul Kumar17
Tera Guru

Hi Naidile,

 

To achieve this, you can create a Business Rule that triggers when a Requested Approval record is updated and its state is changed to "deferred". Here are the steps you can follow:

  1. Go to the "Requested Approval" table and open a record for editing.

  2. In the header, click the "Business Rules" related link.

  3. Click the "New" button to create a new Business Rule.

  4. Enter a name for the Business Rule, such as "Add work note on approval deferral".

  5. Select "Requested Approval" as the Table.

  6. Set the "When to run" field to "After".

  7. Set the "Advanced" field to "true".

  8. In the "Advanced" section, enter the following script:

 

(function executeRule(current, previous /*null when async*/) {
   if (current.state == 3 && previous.state != 3) { // State 3 is "deferred"
      var itemGr = new GlideRecord('vulnerable_item');
      itemGr.get(current.vulnerable_item.sys_id);
      var workNotes = "Request for deferral has been approved until " + current.u_until_date;
      itemGr.work_notes = workNotes;
      itemGr.update();
   }
})(current, previous);

 

In this script, we first check if the current state is "deferred" and the previous state was not "deferred". This ensures that the script only runs when the state is changed to "deferred".

Then, we create a GlideRecord object for the "vulnerable_item" table and get the record associated with the Requested Approval record. We then construct the work notes message using the "u_until_date" field from the Requested Approval record.

Finally, we set the "work_notes" field on the "vulnerable_item" record to the constructed message and update the record.

  1. Save the Business Rule.

With this Business Rule in place, whenever a Requested Approval record is updated and its state is changed to "deferred", a work note will be added to the associated "vulnerable_item" record with the message "Request for deferral has been approved until [UNTIL DATE]." where [UNTIL DATE] is the value of the "u_until_date" field on the Requested Approval record.

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

Thanks Rahul , Ill try with this information.

Hi Rahul

i tried this script , its not working for me , could you help me with this.
i have written for after BR (update and insert)

NAIDILES_0-1683118385564.png

itemGr.get(current.vulnerable_item.sys_id);

here current.vulnerable_item(is this the table name )?


Ankur Bawiskar
Tera Patron
Tera Patron

@NAIDILE S 

How are you identifying the Request is approved? is there a state field on Vulnerable item table?

from where the date has to be fetched? is that date field on vulnerable item table

If yes then you should use Before update BR on Vulnerable Item table

Ensure you give valid conditions for BR to trigger

Use this script

current.work_notes = 'Request for deferral has been approved until ' + current.ignore_date;

If my response helped please mark it correct and close the thread so that it benefits future readers.

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