Manage ServiceNow ITIL License Maximum Limit by using Flow Designer

Rajesh Bandila
Tera Contributor

 

 

Hi Everyone,

 

I have created the catalog item(with Flow Designer) to submitting the user to get ITIL access in ServiceNow. I have an requirement now, We have a total of limit 600 ITIL licenses.
If a user submits a request for an ITIL license and the current count is below 600, the request should be processed as usual. If the count has reached 600, the request state should be set to “Pending”. Once a license becomes available, the system should automatically process the pending request.

How to achieve this by using Flow Designer? what "action" I need place in the flow? Moreover, How to process the "pending" request's once the license is available?


script:

 

(function execute(inputs, outputs) {
    var user = new GlideRecord('sys_user');
    user.addActiveQuery();
    user.query();
    var count = 0;
    while (user.next()) {
        var role = new GlideRecord('sys_user_has_role');
        role.addQuery('user', user.sys_id);
        role.addQuery('role.name', 'itil');
        role.query();
        if (role.hasNext()) {
            count++;
        }
    }
    gs.print(count);

    if (count >= 600) {
     
        var request = new GlideRecord('your_request_table');
        if (request.get(inputs.request_sys_id)) {
            request.state = 'Pending';
            request.update();
        }
    } else {
        
        }
    }
})(inputs, outputs);

 

 

Thanks,

Rajesh

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@Rajesh Bandila You can build a custom action and put your source code in it. Use this custom condition in your flow to mark the request pending if the licenses are not available.

 

For pending request, you can use a combination of a Script Action and an Event. The event will trigger as soon as a license becomes available. Script Action will pick up those pending request and will fulfil them once the event is triggered.

 

Hope this helps.

Community Alums
Not applicable

Hey @Rajesh Bandila 

 

Your query can be more efficient. Here is the script you can use in your custom action has Sandeep has noted:

 

function canGiveItil(threshold) {
    var count = new GlideAggregate('sys_user_has_role');
    count.addEncodedQuery('user.active=true^role=282bf1fac6112285017366cb5f867469'); // ITIL role
    count.addAggregate('COUNT');
    count.query();

    if (count.next()) {
        var total = parseInt(count.getAggregate('COUNT'));

        if (total < threshold) {
            return true;
        }
    }
    return false;
}

 

As you can see here, I am accepting the threshold (600 licenses) as a parameter so you could easily change it later. I would recommend making this an input to your custom action. Additionally, you may look at using a decision table where you can call different flow actions based on the type of license you want to assign.

 

Hope this helps!

~Nick

Hi @Community Alums 

 

Thank you for your response. The script you provided works correctly to set the request to a “pending” state when the ITIL license count is unavailable. However, the challenge lies in automatically processing the “pending” request once a license becomes available. Could you please let me know, how to achieve this with in the Flow Designer?

 

Thanks,

Rajesh

 

Thanks,

Rajesh

Community Alums
Not applicable

Hey @Rajesh Bandila ,

 

I don't know if it would make sense to do this in Flow Designer actually as you probably want to process the requests as first-in-first-out (FIFO). With flow designer, you could put a Wait condition in one form or another, but the challenge would be you wouldn't have control of the order in which each flow context would execute in.

 

If I were trying to do this automated, I would write a scheduled job that runs once a day, or on whatever interval you require, that queries for RITM's where Catalog Item=<your catalog item>, and the Workflow Status=<Pending License> (or whatever you name it). With this, you could also add an Order By condition to order by the date they were created, or any other parameters for your requirement and process them in order. Here is the high level sudo-code for the script:

 

// get how many licesnes I have available

// if licenses available > 0, continue on
     // loop pending RITMs for a license
         // if licenses available > 0, continue on, else break out of the loop
              // assign the license to the user in this RITM
              // close the RITM, or maybe, close the specific task on this RITM, maybe you have one labeled "System Automation: Assign license once available" assigned to the System Administrator
              // decrement licenses available by one
         // end if
     // end loop
// end if

 

Hope this helps!

~Nick