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

Did you try the solution proposed by me above?

Regards

Shloke

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

Regards,
Shloke

Hi shloke04,

Thank you so much for the script!

It worked for the first condition, but didn't for the second condition. *The following part:

Could you please check if there is some problem with the query?

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"

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();

Could you please check if there is some problem with the query?

Have updated the UI Action code. Please use the same 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('sys_created_onRELATIVELT@dayofweek@ahead@7^state=2');
    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();
}

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,

Thank you for the script. The following script worked for "IF" and "IF ELSE" conditions.

However, looks like "ELSE" pattern (the last one) did not work...

Specifically, 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()", so could you revise the script, please?

find_real_file.png

 

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('u_last_oncalldateRELATIVELT@dayofweek@ahead@7^state=2');
    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();
}

okay. Can you try the below modified script and let me know if you are still facing an issue here:

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^sys_created_onRELATIVELT@dayofweek@ago@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();
}

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