Filtering out options to avoid Duplicates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 09:18 AM
Hello Everybody, I seem to have run into an issue I cant quite fix. I have Service Catalog with 3 variables
1. business_application (Reference to business application)
2. existing_relationships (List Collector referencing cmdb_rel_ci)
> Reference Qualifier: javascript:'parent.sys_class_name=cmdb_ci_business_capability^child=' + current.variables.business_application;
3. new_relationships (List collector referencing cmdb_ci_business_capability)
>Reference Qualifier: javascript:'hierarchy_level=2^child!=' + current.variables.business_application;
Basically to delete and create Relationships between business applications.
However I need to add prevention and I have tried to do this with a Script Include but I just can't quite make it work properly.
I'm trying an OnChange Client Catalog Script along with a Script Include. what I need is to prevent duplicate when users select any "new_relationships" and the options shown already on "existing_relationship"
This is my code so far:
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var targetRecord = g_form.getValue("business_application");
var ga = new GlideAjax('CheckExistingRel');
ga.addParam('sysparm_name', 'checkRelationship');
ga.addParam('sysparm_sysID', g_form.getValue("business_application"));
ga.addParam('sysparm_parentID', g_form.getValue("existing_relationships"));
ga.getXMLAnswer(callbackFunction);
function callbackFunction(answer){
if(answer){
var returnedData = JSON.parse(answer);
g_form.addErrorMessage('A relationship with '+ returnedData.name + ' already exists please verify your changes and submit them when ready');
g_form.clearValue('new_relationships');
}
}
}
And this is My Script Include:
var CheckExistingRel = Class.create();
CheckExistingRel.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkRelationship: function()
{
var targetRecord = this.getParameter('sysparm_sysID');
var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('child', targetRecord);
//gr.addQuery('parent', this.getParameter("sysparm_parentID"));
gr.query();
if(gr.next())
{
var results = { "name":gr.getValue("name")};
gs.addErrorMessage("If was true");
return JSON.stringify(results);
}else
{
}
}
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 11:26 PM
Hi. I am not going deep into the glide ajax syntax here, you can check yourself how to unpack the result and check what is inside.
What I would change in the script include:
- define the result object outside the if with a property (for example duplicateExists: false)
- if the IF is true - set the result.duplicateExests value to true, set result.duplicateName: the name of the duplicate
- if the IF is false, return the result object
On the client side - check if the result.duplicateExists is true - if yes, go ahead and display the error message + clear the field value. if no, just let it go.
With your current code you return no result in the False case.
I have no time to write the code on your behalf, hope this guidance helps.
- set
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024