Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Automate User allocations in Software entitlements using groups

MercBuilding
Tera Guru

Hi Team,

   Iam trying to assign users from groups to few entitlements. I have written a script to do it but this is allowing only one entitlement, i want to allocate users from same group to ent1 and ent2 which have same display name. Its not working for multiple entitlements with same display name and even i have entitlements with enterprise contracts its not allowing me to over allocate the rights (allcoate more than purchased rights). Below is the script, please let me know how to achieve this and where i need to modify to achieve this.

createUserALlocations();


function createUserALlocations() {
    var mappings = [
        { groupName: 'M365 Visio Plan', entitlementName: 'Microsoft Visual Studio 2017 Enterprise' },
       
        { groupName: 'Visual Studio Ent', entitlementName: 'Microsoft Visual Studio with Github Enterprise' },
        { groupName: 'Visual Studio Pro', entitlementName: 'Microsoft Visual Studio with Github Professional' },
        { groupName: 'Adobe Acrobat', entitlementName: 'Adobe Systems Acrobat DC Pro' }
    ];
   
    for (var i = 0; i < mappings.length; i++) {
        var groupGR = new GlideRecord('sys_user_group');
        groupGR.addQuery('name', mappings[i].groupName);
        groupGR.query();
        if (groupGR.next()) {
           
                var userGR = new GlideRecord('sys_user_grmember');
                userGR.addQuery('group', groupGR.sys_id);
                userGR.query();
                while (userGR.next()) {
                    var ent_sysid;
                    var allocations_available;
                    var total_ent=[];
                    gs.info("number of users "+groupGR.getRowCount());
                    var k=groupGR.getRowCount();
                    var entitlementGR = new GlideRecord('alm_license'); // Replace with your entitlement table name
            entitlementGR.addQuery('display_name', mappings[i].entitlementName);
            entitlementGR.query();
            while (entitlementGR.next()) {
                var entCount=entitlementGR.getRowCount();
                total_ent.push(entitlementGR.sys_id,entitlementGR.allocations_available);
            }
                    var allocationCheck = new GlideRecord('alm_entitlement_user'); // Replace with your allocation table
                    allocationCheck.addQuery('assigned_to', userGR.user.sys_id);
                   // allocationCheck.addQuery('licensed_by', entitlementGR.sys_id);
                   allocationCheck.addEncodedQuery('licensed_by.display_nameSTARTSWITH'+mappings[i].entitlementName);
                    allocationCheck.query();
                    if (!allocationCheck.hasNext()) {
                        var allocationGR = new GlideRecord('alm_entitlement_user');
                        allocationGR.initialize();
                        allocationGR.assigned_to = userGR.sys_id;
                        allocationGR.licensed_by = entitlementGR.sys_id;
                        allocationGR.insert();
                    }
                }
            } //else {
                //gs.error('Entitlement not found: ' + mappings[i].entitlementName);
            //}
        } else {
            gs.error('Group not found: ' + mappings[i].groupName);
        }
    }
}
 
Thanks!
1 REPLY 1

dreinhardt
Kilo Patron
Kilo Patron

Hi @MercBuilding ,

 

Upgrade zu Zurich and use OOTB solution for this - https://www.servicenow.com/docs/bundle/zurich-it-asset-management/page/product/software-asset-manage...

 

Best, Dennis

Should my response prove helpful, please consider marking it as the Accepted Solution/Helpful to assist closing this thread.