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.

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

My Sample script is here
Client script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
       g_form.clearValue('approval_group2');
       return;
   }
   var ga = new GlideAjax('CheckApprovalGroup');
   ga.addParam('sysparm_name', 'getApprovalGroup');
   ga.addParam('sysparm_requested_for', newValue);
   ga.getXMLAnswer(function(response) {
       if (response == 'GROUP_Nuclear') {
           g_form.setValue('approval_group2', 'GROUP_Nuclear');
       } else {
           g_form.clearValue('approval_group2'); // Clear if condition not met
       }
   });
}


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 companySysId = userGR.getValue('u_snc_l_company'); // Reference field
           var costCenterSysId = userGR.getValue('cost_center'); // Reference field
           // Check company name
           if (companySysId) {
               var companyGR = new GlideRecord('u_company_org_list'); 
               if (companyGR.get(companySysId)) {
                   var companyName = companyGR.getValue('name');
                   if (companyName.includes('CANDU') || companyName.includes('Nuclear')) {
                       return 'GROUP_Nuclear';
                   }
               }
           }
           // Check cost center hierarchy path
           if (costCenterSysId) {
               var costCenterGR = new GlideRecord('cost_center'); 
               if (costCenterGR.get(costCenterSysId)) {
                   var hierarchyPath = costCenterGR.getValue('u_hierarchy_path'); 
                   if (hierarchyPath && hierarchyPath.includes('Nuclear')) {
                       return 'GROUP_Nuclear';
                   }
               }
           }
       }
       return ''; // Return empty if no match
   }
});

I got it @Bindhu1 

 

Just change one thing, instead of "SETTING THE VALUE WITH GROUP NUCLEAR, PQSS SYS_ID" 

 

I was not aware of sys_id hence passed that. 

 

g_form.setValue('approval_group2', 'GROUP_Nuclear'); - this line 

 

And also remove below 👇 lines - 

 

else {

           g_form.clearValue('approval_group2'); // Clear if condition not met

       }

 

Next add logs everywhere and let me know which one is not printing value, try after above @Bindhu1 Edits only. 

 

 

Chaitanya ILCR
Mega Patron

Hi @Bindhu1 

create a onchange client script on requested_for variable

 

CS

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
		g_form.clearValue('approval_group')
        return;
    }

    var ga = new GlideAjax('CheckNuclearConditions');
    gs.addParam('sysparm_name', 'checkConditionsClient')
    ga.addParam('sysparm_requested_for', newValue);
    ga.getXMLAnswer(function(answer) {
        if (answer === 'true') {
            g_form.setValue('approval_group', 'put SYSID OF the Group');
        }
    });
}

 

Client callable 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 companyContainsKeywords = user.u_snc_l_company.includes("CANDU") || user.u_snc_l_company.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

 

 

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

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