Autopulate issue manager based off entity type
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 02:51 PM
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'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2024 02:58 PM
Hi @Runjay Patel ,
Entity type is not a field on the entity (profile) table it is a related list. Please see screenshot below:
How would I modify the code?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2024 09:58 PM
Hi @Nabilah ,
On that case you need to make ajax call to check.
Client script code
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');
}
});
}
}
Server side code
var CheckEntityTypeScriptInclude = Class.create();
CheckEntityTypeScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkEntityType: function() {
var profileSysId = this.getParameter('profileSysId');
var targetEntityType = '002b84281b755a10f9246204b24bcbcd';
var hasEntityType = false;
gs.log('Script include Called ');
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;
},
type: 'CheckEntityTypeScriptInclude'
};
Make sure "Client callable" ticketed.
Do check the log you supposed to get message "Script include Called" if not then create script include new and try and deactivate the old one.
-------------------------------------------------------------------------
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
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2024 11:44 PM
Hi @Runjay Patel, I have tried this but its still not working and the log message is not displayed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2024 12:00 AM
Hi @Nabilah ,
Thats the issue, your Script include is not getting called, hence you are not getting result from server. Once server get called it will work.
Check this for more info regarding Ajax call: https://servicenowwithrunjay.com/return-multiple-values-to-glideajax-from-script-include/
I have asked you to create new script include and make it client callable and then paste the code.
Only paste below code.
checkEntityType: function() {
var profileSysId = this.getParameter('profileSysId');
var targetEntityType = '002b84281b755a10f9246204b24bcbcd';
var hasEntityType = false;
gs.log('Script include Called ');
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;
},
Predefined code should be there, you just need to paste the in between.
-------------------------------------------------------------------------
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
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2024 12:08 AM
Hi @Runjay Patel , I have done exactly that and it is still not calling the script include.