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.

Autopulate issue manager based off entity type

Nabilah
Tera Contributor

Hi I have a requirement on the issue table (sn_grc_issue) to autopopulate the issue_manager field to user X if issue_source= indicator failure and the entity referenced in the profile field has entity type (sn_grc_m2m_profile_profile_type)  Y. 

 

On the entity (profile) table the entity types are a related list. I attempted to an on load client script calling a script include but it is not working for me. How would I go about achieving this?

 

Client Script:

 

 

 

function onLoad() {
    // Check if the issue_source field matches the specified value
    if (g_form.getValue('issue_source') === 'd8000889730323003aebda013ef6a72a') {

     
        g_form.getReference('profile', function(profile) {
            if (profile) {
               
                var ga = new GlideAjax('CheckEntityTypeScriptInclude');
                ga.addParam('sysparm_name', 'checkEntityType');
                ga.addParam('profileSysId', profile.sys_id);  
                ga.getXMLAnswer(function(response) {
                    var hasEntityType = response.responseXML.documentElement.getAttribute("answer") === "true";
					alert(hasEntityType);

                    
                    if (hasEntityType) {
                        g_form.setValue('issue_manager', 'X');
                    } else {
                        alert("Profile does not contain the required entity type.");
                    }
                });
            } else {
                alert('Profile is null or undefined');
            }
        });
    }
}

 

 

 

Script Include:

 

 

var CheckEntityTypeScriptInclude = Class.create();
CheckEntityTypeScriptInclude.prototype = {
    initialize: function() {},

  
    checkEntityType: function() {
        var profileSysId = this.getParameter('profileSysId');
        var targetEntityType = '002b84281b755a10f9246204b24bcbcd';
        var hasEntityType = false;

        var gr = new GlideRecord('sn_grc_m2m_profile_profile_type');
        gr.addQuery('profile', profileSysId);           
        gr.addQuery('profile_type', targetEntityType);  
        gr.query();

        if (gr.next()) {
            hasEntityType = true;
        }

        return hasEntityType.toString(); 
    },

    type: 'CheckEntityTypeScriptInclude'
};

 

 
10 REPLIES 10

Nabilah
Tera Contributor

Can anyone guide me to the right direction please?

Runjay Patel
Giga Sage

Hi @Nabilah ,

 

If i understand you correctly the you have two filed on sn_grc_issue table, 1st one is issue_manager reference to user table and 2nd have profile reference to sn_grc_m2m_profile_profile_type table.

 

Now you want to set X to issue_manager 

if issue_source= indicator failure and profile field has entity type

else

you want to set Y to issue_manager 

 

And you want it on onload right?

Is my understanding is correct?

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

 

Hi @Runjay Patel @yes that is correct. I want to set the issue_manager field to user X if the following conditions are true:

 

1. issue source = indicator failure 

2. entity has entity type Y 

Hi @Nabilah ,

 

You can write onload client script and use below code.

function onLoad() {
    //Type appropriate comment here, and begin script below

    if (g_form.getValue('issue_source') == 'd8000889730323003aebda013ef6a72a') {
// d8000889730323003aebda013ef6a72a - replace with your actual issue source sys id
        var flag = g_form.getReference('profile', userLookup);
        if (flag)
		g_form.setValue('issue_manager', 'X');

    }


}

function userLookup(userref) {

    var entityType = userref.u_entity_type; // replace u_entity_type with your entity type filed which present on profile reference table.
    if (userLookup == 'Y')
        return true;
    else
        return false;
}

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------