- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2018 07:47 AM
Hi All,
I am in the middle of creating a script include to be used as part of a reference qualifier to filter the available values in a reference field.
As part of my script I am using the following encoded query:
operational_status!=3^name!=CST (Client Specific Testing)^RLQUERYu_m2m_companies_configuratio.u_configuration_item,>=1,m2m^u_companyDYNAMICc1c7b721db43830085ce56d6dc9619ff^ENDRLQUERY
For most of our customers this works fine as the users are part of a single company. However some of our customers have multiple brands (companies). These users are made part of the parent company and the various brands are child companies related to this.
On the customer portal we ask them which brand they are reporting the issue for.
I was wondering if there is anyway to not use the companyDYNAMIC and instead use the value from the 'which brand' field within the encoded query?
Is this possible?
Thanks
sam
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2018 06:21 AM
What type of variable is current.variables.u_brands? Is it a single reference or multiple values? If single, maybe the following script will work. Notice I created an encodedQuery variable so its a little easier to read and I inserted the value of the brand variable into the query. Its also good practice to use an array to push values into versus creating a string of SysIDs. I modified that as well.
var My_Env = Class.create();
My_Env.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEnv : function(u_brands){
var brand = u_brands;
if(brand == ''){
return 'sys_idIN';
} else {
var answer = [];
var encodedQuery = "operational_status!=3^name!=CST (Client Specific Testing)^RLQUERYu_m2m_companies_configuratio.u_configuration_item,>=1,m2m^u_company
=" + brand + "^ENDRLQUERY";
var env = new GlideRecord('cmdb_ci_environment');
env.addEncodedQuery(encodedQuery);
env.query();
while(env.next()){
answer.push(env.sys_id.toString());
}
return 'sys_idIN' + answer.toString();
}
},
type: 'My_Env'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2018 05:55 AM
Sam,
Regarding your question about advantages of using an array vs concatenating strings, arrays are much easier to deal with. There are methods to remove entries, sort entries, insert entries at certain positions, etc while doing this in a string is difficult. Out of the box a utility script called ArrayUtil also exists to help:
Also it requires additional coding and remembering to remove the "extra" comma at the beginning/end of your string - your code had a substring(1) as an example:
'sys_idIN'+sysid.substring(1);
If you forget to remove the extra comma, problems can arise so that is why it is good practice to just declare an array variable and use the "push" function to insert items within it. Then you can simply return a comma separated list by using toString() or using the join(",") method.
Everyone codes differently and concatenating strings certainly works and some prefer it, I personally prefer arrays because IMHO its less risky because you don't have to remember to deal with the extra comma. I know I have seen this suggestion documented and covered by Chuck Tomasi and Kreg on TechNow but I cannot find the links.
I hope this helps!