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, one followup question if I man:  You now have my conditiones in the condition field in the new UI Action.  However, for all my conditions, the 255 char limit on that field will not suffice.  How to get around that problem

@StacyLen 

if the 255 char limit exceeds then put that in script include function and call it from UI action condition.

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