Get value dynamically of a vendor field from Group in Client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-02-2024 08:36 AM
Hi Team
I have a requirement. On Service operation workspace change request, requested by and assign to field cannot be same when the Workgroup vendor is AV Technology.
What I have:
For that I wrote a On-submit client script and Script Include (Client callable). In Client script for fetching vendor details (Reference field), I used dot walk to vendor field and used sys id of particular vendor in 'If' condition. Using sys id, its perfectly working when those two field are same on saving the form.
My question is:
As I know, using sys id is not a best practice in ServiceNow, is there any other way to fetch the Vendor field value dynamically?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-02-2024 08:14 PM
Can u share ur script please
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2024 07:18 AM
Thanks for your response, Mani. Please review the script and let me know if find any mistake.
Here is the script: -
script include: -
name- CheckAssignmentGroupVendor
Client callable- Checked
var CheckAssignmentGroupVendor = class.create ();
CheckAssignmentGroupVendor.prototype = object.extendsObject ( AbstractAjaxProcessor, {
initialize: function () {},
getVendorForGroup: function (assignmentGroupSysId) {
var vendor = ' ';
var gr = new GlideRecord ("sys_user_group");
if (gr.get (assignmentGroupSysId) ) {
vendor = gr.vendor.name;
}
return vendor;
},
process: function () {
var assignmentGroupSysId = this.getParameter ("sys_id");
var vendor = this.getVendorForGroup (assignmentGroupSysId);
return vendor;
},
type: 'CheckAssignmentGroupVendor'
}),
Client Script: -
Name; Check assignment group vendor
Table; Change Request
Ui Type: all
Type: onSubmit
View: sow (Service operation workspace)
function onSubmit () {
var requestor = g_form.getValue ("requested_by");
var assignedto = g_form.getValue ("assigned_to");
var group = g_form.getValue ('assignment_group');
if (requestor === assignedto) {
var ga = new GlideAjax ('CheckAssignmentGroupVendor');
ga.addParam ('sys_id', group);
ga.getXMLAnswer (function (response) {
var vendor = response.responseXML.documentElement.getAttribute ("answer");
if (vendor == "sys_id of the vendor") {
alert ("Assigned to and Requested by cannot be same");
} else {
g_form.submit (true);
}
});
}
}
Thanks, and Regards
Tarini Prasad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2024 07:59 AM
Two changes required here
First in GlideAjax call
add this ->ga.addParam('sysparm_name','process'); // Process is function name which u used in script include
Next change is ->
If("vendor=="vendor groupname") .
You are returning vendor group name from script include...so use it instead of sy_id
Rest everything looks good...this is dynamic logic only
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2024 09:12 AM
Now its restrict to save. But the error message is not populated. Still is there any mistake in alert message?