setValue of a field is not working in async insert/update Business rule

Renu9
Tera Contributor

Hi All,

I am writing an async Insert/Update business rule to update a field value(Ex: TestA) on RITM based on RITM's Approval field value.

If Approval field value is either approved/rejected, then only I need to populate some value in  'TestA' 

There are few RITM's with approver and few with no-approvers

Async Insert/Update

current.setValue('TestA', time);

(or)
        current.TestA = time;

Both the above methods are not working in async BR, Please guide

 

10 REPLIES 10

shloke04
Kilo Patron

Hi,

Couple of things I would suggest here to look at:

1. Did you put a condition in your BR so that it gets executed when your Approval field on RITM is updated?

if yes, can you check if Approval field is getting updated or not?

2. Also, you will have to use current.update() in your Async BR which you have written.

Another suggestion which I have seen couple of times happening is Approval field not getting set properly so will suggest to follow the below approach:

1. Write an After Update Business Rule on Approval Table and use the condition and script as mentioned below:

BR Details:

1. Table: Approval (sysapproval_approver)

2. When: After Update

3. Condition : State Changes to Approved OR State Changes to Rejected

4. Script:

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var getRITM = current.sysapproval;

    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('sys_id', getRITM);
    gr.query();
    if (gr.next()) {
        gr.FieldName = 'Value which you want to set';
        gr.update();
    }

})(current, previous);

find_real_file.png

These are all suggestions, without looking at what you have done is difficult to help you with exact solution. Can you share your screenshot of the BR if above suggestion does not work.

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Renu9
Tera Contributor

Hi,

I tried using as said above by keeping the BR on approval table , but concern here is if there is no approvals for few RITM's then this BR is not even getting triggered as the record will not be available in Approval table.

If there is having few approvers, then there is no issue , but if is with 0 approvers, then the above is not working

(function executeRule(current, previous /*null when async*/ ) {

var app= current.approval;

gs.log("RITM value is  "+app);   

var start = new GlideDateTime(current.opened_at); //opened time
    gs.log("RITM opened time " +start);
    var end = new GlideDateTime(current.approval_set); //approved time
    var dc = new DurationCalculator();
    var sch = gs.getProperty('BusinessHoursSchedule');
    dc.setSchedule(sch); //Business Hours 8 AM - 5 PM (Mon to Fri)
    var time = dc.calcScheduleDuration(start, end); // time would be in seconds
    var durationMS = time * 1000;
    var result = new GlideDuration(durationMS);
    var duration = result.getDisplayValue();
   current.setValue('u_approval_duration', duration);
        current.u_approval_duration = duration;
    gs.log("RITM Approval duration " +u_approval_duration);
})(current, previous);

If there are any approvals for the RITM, then the script is working fine

If there are no approvals for RITM, then automatically the RITM approval field value gets displays as 'Approved'. But it is showing as 'Requested' only, hence my script is not working, Kindly guide me here

 

The bolded line log is showing as 'Requested' instead of 'Approved' for no approvers RITM

Hi you need to use current.update() in after Business rule to update the record because youre trying to update the record after record insertion in datebase hence current.update () is required here

If you dont want to use current.update(). You need to change your BR to before update. 

Regards
Harish

Hi,

Your initial question says below:

"If Approval field value is either approved/rejected, then only I need to populate some value in  'TestA' "

So will there be a scenario where there will be no approvals? Because what I understood from your question was the field need to be set only when Approval is marked as Approved or Rejected.

Please confirm!!

Regards,

Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Renu9
Tera Contributor

Yes, there are scenarios where RITM with No approvals.

If there is no approval, then by default, the Approval field value on RITM is showing as Approved.

But when I keep a log and check, the value is coming up as Requested.

So, BR is not getting triggered if I give condition as Approval is Approved/Rejected