Script Include and UI Action

StacyLen
Mega Guru

Newbie to Scripting here.

I want to use the SN OOTB "Revert to New" UI Action for Change.  However, the 255 characters allowed in Condition line is not enough.  So I am trying put all my conditions into a Script Include.  

I think all I need returned "from" the script is a true or false?

I need help writing this Script Include.  Below is my feeble and failed attempt to write the script.

 

Basically I want to check cur user to see if they are member of group with the listed sysid, then check the change types and change states and if they match any of the AND/OR conditions, return True.  Else, false

 

Help is most definitely appreciated

Stacy Len

 

//var revertChangeConditionsCheck{
//     try {
/var memBySysId = gs.getUser().isMemberOf('9d385017c611428711d22104ce95c371');
//gs.info(memBySysId);
// use group membership rather than Role.

//        if memBySysID &&
//             ((current.type == ChangeRequest.EMERGENCY && new ChangeRequest(current).isAuthorize()) ||
//                 (current.type == ChangeRequest.NORMAL && new ChangeRequest(current).isAssess()) ||
//                 (current.type == changeRequest.NORMAL && new ChangeRequest(current).isAuthorize())) {

//                 return true;

//                 else {
//                     return false;
//                 }
//             }

//      }
//  catch (error) {
//         gs.error("Error in Scripot Include revertChangeConditionCheck " + error);
//     }
// }

 

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@StacyLen 

use this script include

Then call it like this in UI action condition

new ConditionUtils().checkMyCondition(current)

var ConditionUtils = Class.create();
ConditionUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkMyCondition: function(current) {
        if ((current.type == global.ChangeRequest.EMERGENCY && new global.ChangeRequest(current).isAuthorize()) || (current.type == global.ChangeRequest.NORMAL && new global.ChangeRequest(current).isAssess()) && gs.getUser().isMemberOf('9d385017c611428711d22104ce95c371') && (current.type == ChangeRequest.EMERGENCY && new ChangeRequest(current).isAuthorize()) || (current.type == ChangeRequest.NORMAL && new ChangeRequest(current).isAssess()) || (current.type == changeRequest.NORMAL && new ChangeRequest(current).isAuthorize()))
            return true;
        else
            return false;
    },

    type: 'ConditionUtils'
});

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

View solution in original post

@StacyLen 

that UI action is server side so you will have to make it client side and use gsftSubmit()

I highlighted the changed below

1) you need to set action name

2) set Client = true

3) set Onclick = function name I gave

AnkurBawiskar_0-1754663861431.png

 

function revertToNew() {

    var confirm = confirm("Are you sure you want to revert this change to New?");
    if (confirm.toString() == 'true')
        gsftSubmit(null, g_form.getFormElement(), "revert_to_new_1");

}

if (typeof window == 'undefined')
    runServerSideCode();

function runServerSideCode() {
    var changeRequest = new ChangeRequest(current);
    if (!changeRequest.revertToNew())
        gs.addErrorMessage(gs.getMessage('State Model for {0} changes does not allow reverting change from {1} state', [changeRequest.getValue('type'), changeRequest.getDisplayValue('state')]));
    action.setRedirectURL(current);

}

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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@StacyLen 

use this script include

Then call it like this in UI action condition

new ConditionUtils().checkMyCondition(current)

var ConditionUtils = Class.create();
ConditionUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkMyCondition: function(current) {
        if ((current.type == global.ChangeRequest.EMERGENCY && new global.ChangeRequest(current).isAuthorize()) || (current.type == global.ChangeRequest.NORMAL && new global.ChangeRequest(current).isAssess()) && gs.getUser().isMemberOf('9d385017c611428711d22104ce95c371') && (current.type == ChangeRequest.EMERGENCY && new ChangeRequest(current).isAuthorize()) || (current.type == ChangeRequest.NORMAL && new ChangeRequest(current).isAssess()) || (current.type == changeRequest.NORMAL && new ChangeRequest(current).isAuthorize()))
            return true;
        else
            return false;
    },

    type: 'ConditionUtils'
});

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

Ankur, Bawiskar - YOU are the GOAT!!!  Works perfectly.  Now I need only one more thing.  I need to have a message popup when revert button (UI Action) is clicked, msg = "Are you sure you want to revert this change to New"

 

Thanks.  You are wonderful

Stacy Len

@StacyLen 

that UI action is server side so you will have to make it client side and use gsftSubmit()

I highlighted the changed below

1) you need to set action name

2) set Client = true

3) set Onclick = function name I gave

AnkurBawiskar_0-1754663861431.png

 

function revertToNew() {

    var confirm = confirm("Are you sure you want to revert this change to New?");
    if (confirm.toString() == 'true')
        gsftSubmit(null, g_form.getFormElement(), "revert_to_new_1");

}

if (typeof window == 'undefined')
    runServerSideCode();

function runServerSideCode() {
    var changeRequest = new ChangeRequest(current);
    if (!changeRequest.revertToNew())
        gs.addErrorMessage(gs.getMessage('State Model for {0} changes does not allow reverting change from {1} state', [changeRequest.getValue('type'), changeRequest.getDisplayValue('state')]));
    action.setRedirectURL(current);

}

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

awesome again!  Thank you so much