Populate the group based on requested for user details

Bindhu1
Tera Contributor

I have below two variables: need help in script to achieve this requirement. 
1. requested_for: referring to sys_user 
2. approval_group: referring to sys_user_group

Populate the Group as 'GROUP_Nuclear' only when 

  • Requested For.u_snc_l_company contains "CANDU" or "Nuclear"
    Bindhu1_0-1743531203158.png

     

  • or Requested For.cost_center.u_hierarchy_path path contains "Nuclear"
    Bindhu1_1-1743531240783.png->  Bindhu1_2-1743531274190.png

     

     

2 ACCEPTED SOLUTIONS

Hi @Bindhu1 ,

my bad I didn't notice u_snc_l_company as a reference field 

is it referencing to core_company table if the below script doesn't work as stated in the line number 7 of below code dot walk further to field where nuclear or Candu exist

update the script include

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

    checkConditions: function(requestedFor) {
        var user = new GlideRecord('sys_user');
        if (user.get(requestedFor)) {
            var companyName = user.u_snc_l_companyName.getDisplayValue(); /*if gtDisplayValue() doesn't work try dot walkin to name or the field where Nuclear or CANDU you are trying to find*/

            var companyContainsKeywords = companyName.includes("CANDU") || companyName.includes("Nuclear");
            var costCenterContainsKeywords = user.cost_center.u_hierarchy_path.includes("Nuclear");

            return companyContainsKeywords || costCenterContainsKeywords;
        }
        return false;
    },

    // This function is client-callable
    checkConditionsClient: function() {
        var requestedFor = this.getParameter('sysparm_requested_for');
        var result = this.checkConditions(requestedFor);
        return result.toString();
    },

    type: 'CheckNuclearConditions'
});

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

 

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Bindhu1 

Did you add logs in script include?

Did you add alert in client script?

Your Script Include should be client callable

Something like this, I assume u_snc_l_company is referring to core_company table

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

    getGroup: function() {
        var user = this.getParameter('sysparm_user');
        var gr = new GlideRecord("sys_user");
        gr.addQuery("sys_id", user);
        // give the field on that table which holds this value, I gave name field
        gr.addQuery('u_snc_l_company.nameLIKECANDU,Nuclear').addOrCondition('cost_center.u_hierarchy_pathLIKENuclear');
        gr.query();
        if (gr.hasNext()) {
            return 'nuclearGroupSysId';
        }
        return '';

    },

    type: 'UserCheck'
});

onChange client script: On Requested For field on form

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

    if (newValue == '')
        g_form.clearValue('approval_groupField');

    var ga = new GlideAjax('UserCheck');
    ga.addParam('sysparm_name', 'getGroup')
    ga.addParam('sysparm_user', newValue);
    ga.getXMLAnswer(function(answer) {
        if (answer.toString() != '') {
            g_form.setValue('approval_groupField', answer.toString());
        }
    });
}

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

13 REPLIES 13

Shivalika
Mega Sage

Hello @Bindhu1 

 

You will need an on change catalog client script on requested for field with corresponding script include - 

 

Below 👇 on change 

 

(function executeRule(current, gForm, gSNC) {

    // Get the requested_for sys_id

    var requestedFor = gForm.getValue('requested_for');

    

    if (requestedFor) {

        // Call the Script Include to check if criteria match

        var groupName = new GlideAjax('CheckApprovalGroup');

        groupName.addParam('sysparm_name', 'getApprovalGroup');

        groupName.addParam('sysparm_requested_for', requestedFor);

        groupName.getXMLAnswer(function(response) {

            if (response == 'GROUP_Nuclear') {

                gForm.setValue('approval_group', 'GROUP_Nuclear');

            } else {

                gForm.clearValue('approval_group'); // Clear if condition not met

            }

        });

    } else {

        gForm.clearValue('approval_group'); // Clear if no requested for selected

    }

})(current, gForm, gSNC);

 

Below 👇. Script include 

 

var CheckApprovalGroup = Class.create();

CheckApprovalGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    

    getApprovalGroup: function() {

        var requestedForSysId = this.getParameter('sysparm_requested_for');

        

        if (!requestedForSysId) {

            return '';

        }

        

        var userGR = new GlideRecord('sys_user');

        if (userGR.get(requestedForSysId)) {

            var companyName = userGR.getValue('u_snc_l_company'); // Assuming this is a string field

            var costCenterSysId = userGR.getValue('cost_center'); // Reference to cost_center

            

            // Check company name for required keywords

            if (companyName && (companyName.includes('CANDU') || companyName.includes('Nuclear'))) {

                return 'GROUP_Nuclear';

            }

            

            // Check cost center hierarchy path if cost_center exists

            if (costCenterSysId) {

                var costCenterGR = new GlideRecord('cost_center');

                if (costCenterGR.get(costCenterSysId)) {

                    var hierarchyPath = costCenterGR.getValue('u_hierarchy_path'); // Assuming this is a string

                    if (hierarchyPath && hierarchyPath.includes('Nuclear')) {

                        return 'GROUP_Nuclear';

                    }

                }

            }

        }

        

        return ''; // Return empty if no match

    }

});

 

Please fit it to correct names on your UI. Must make the UI type as ALL in the client script and check the "client callable" checkbox in the script include. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

 

Hi Shivalika, thanks for the script , but this is not setting approval_group 
or Can we break down the script like below please?
if Requested For.u_snc_l_company [reference field] contains "CANDU" or "Nuclear" 
set the approval group to 'Nuclear'

@Bindhu1 

 

Did you copy this exact same script ? Also did you change the names of fields ? 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Yes, I Did copy the exact script and did change the names of fields.