UI Action script to dynamically update records according to multiple conditions.

Aki17
Kilo Guru

We would like to create UI Action button on the top right of Incident list page.
By clicking the button, it should check all the Incident records and judge if they match the conditions below and update each record according to the result.
The conditions are like below. Could someone please give me the sample UI Action script for this?


IF
"State" is "In Progress"
  AND "u_last_oncalldate" is (blank)
THEN
Set "u_oncall_state" = "NG"
Set "u_reason" = "XXX"

IF
"State" is "In Progress"
  AND "u_last_oncalldate"(Date/Time) is 'More than a week ago from now'
THEN
Set "u_oncall_state" = "NG"
Set "u_reason" = "YYY"

IF
"State" is "In Progress"
  AND 'The conditions above are NOT met'
THEN
Set "u_oncall_state" = "OK"

 

Best Regards,

Aki

1 ACCEPTED SOLUTION

Please use the updated script. I have tested this in my PDI and is working correctly for me now:

if (current.state == 2 && current.u_last_oncalldate == '') {
    updateRecord();
} else if (current.state == 2 && current.u_last_oncalldate != '') {
    checkFurther();
	updateRecordforElse();
}


function updateRecord() {
    current.u_oncall_state = 'Value you want to set';
    current.u_reason = 'Value you want to set';
    current.update();
}


function checkFurther() {
    var gr = new GlideRecord('incident');
    gr.addEncodedQuery('active=true^state=2^sys_created_onRELATIVELT@dayofweek@ahead@7^sys_created_onNOTONLast 7 days@javascript:gs.beginningOfLast7Days()@javascript:gs.endOfLast7Days()');
    gr.query();
    while (gr.next()) {
        gr.u_oncall_state = 'Value you want to set';
        gr.u_reason = 'Value you want to set';
        gr.update();
    }
}

function updateRecordforElse() {
     var gr = new GlideRecord('incident');
    gr.addEncodedQuery('active=true^state=2^sys_created_onONLast 7 days@javascript:gs.beginningOfLast7Days()@javascript:gs.endOfLast7Days()');
    gr.query();
    while (gr.next()) {
        gr.u_oncall_state = 'OK';
        gr.u_reason = 'Value you want to set';
        gr.update();
    }
}

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

View solution in original post

12 REPLIES 12

Hi shloke04,

Unfortunately, the issue has not been resolved...

Even though the test record has "u_last_oncalldate" = "2022-01-19 17:44:07" which is within the last 7 days AND "state" = "2", the result was "u_oncall_state" = 'NG' populated by "function checkFurther()".

The result should be "u_oncall_state" = 'OK' populated by "function updateRecordforElse()".

Is there any missing condition in the script for this?

Please use the updated script. I have tested this in my PDI and is working correctly for me now:

if (current.state == 2 && current.u_last_oncalldate == '') {
    updateRecord();
} else if (current.state == 2 && current.u_last_oncalldate != '') {
    checkFurther();
	updateRecordforElse();
}


function updateRecord() {
    current.u_oncall_state = 'Value you want to set';
    current.u_reason = 'Value you want to set';
    current.update();
}


function checkFurther() {
    var gr = new GlideRecord('incident');
    gr.addEncodedQuery('active=true^state=2^sys_created_onRELATIVELT@dayofweek@ahead@7^sys_created_onNOTONLast 7 days@javascript:gs.beginningOfLast7Days()@javascript:gs.endOfLast7Days()');
    gr.query();
    while (gr.next()) {
        gr.u_oncall_state = 'Value you want to set';
        gr.u_reason = 'Value you want to set';
        gr.update();
    }
}

function updateRecordforElse() {
     var gr = new GlideRecord('incident');
    gr.addEncodedQuery('active=true^state=2^sys_created_onONLast 7 days@javascript:gs.beginningOfLast7Days()@javascript:gs.endOfLast7Days()');
    gr.query();
    while (gr.next()) {
        gr.u_oncall_state = 'OK';
        gr.u_reason = 'Value you want to set';
        gr.update();
    }
}

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

Hi shloke04,

Amazing! It worked perfectly, thank you so much! I will Mark your answer as Correct.

By the way, I have one more question regarding another UI Action script.

If you could, please take a look at the following Community question I posted and give me your help.

https://community.servicenow.com/community?id=community_question&sys_id=36cfe87bdb890594aa66a9fb1396196f