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

OlaN
Giga Sage
Giga Sage

Hi,

You could make some subflows for this purpose.

The subflow would query incidents according to your requirement, then loop through found records, and set values as needed.
After you made the subflow, and tested it to be working, make a code snippet from it, and use that code to call from an UI action.

Hi OlaN,

Thank you for your reply, but could you please advise me on how to define the conditions in Flow Designer?

Hi, that should not be a problem.

Adding conditions is very easy.

See example:

find_real_file.png

shloke04
Kilo Patron

Hi,

Better way for this would be to handle this in a before Update Business Rule on your Incident table.

But still if you want to go with a UI Action, you can use below:

if (current.state == 2 && current.u_last_oncalldate == '') {
    updateRecord();
} else if (current.state == 2 && current.u_last_oncalldate != '') {
    checkFurther();
} else {
    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^u_last_oncalldateRELATIVEGT@dayofweek@ahead@7');
    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() {
    current.u_oncall_state = 'OK';
    current.update();
}

find_real_file.png

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