Reference Qualifier Advanced Script Include

Ryan2
Kilo Explorer

Hello, I am working on an advanced reference qualifier and need some help getting pointed in the right direction.

I am applying this to the "Affected CI" field under Incident. I have multiple companies in my instance and only want to show the CIs that meet 3 criteria

1. Must not be in a "Retired" state (state 7).

2. Must be for the "Company" populated on the form OR be one of the generic CIs I have created that have "generic" in the name of the CI.

.I tested putting in conditions in the advanced line, but could not get it working. Not sure if this can be fixed to work, or if i need a script include. I was able to get the first two conditions working, but not with the OR statement.

javascript:'install_status!=7^' + 'company='+current.company^OR' name='+name.tolowerCase().indexOf('generic') > -1;

If i go for a script include, do i need to pass back the actual CIs, or can i just return true or false?

This is my first hacked together script include, I am sure it needs a lot of work. Going to keep banging away at this, but it will be very helpful to understand this for future needs. Thanks

function u_CompanyAffectedCI(current) {

   

    var a = current.company;

    var b = current.cmdb_ci.name;

    var c = current.cmdb_ci.install_status;

    var d = current.cmdb_ci.company;

   

if (d==a){

if (b.toLowerCase.indexOf('generic') || c!=7){

return TRUE;

}

}

else {

return FALSE;

}

return FALSE;

}

1 ACCEPTED SOLUTION

Jon Barnes
Kilo Sage

Actually, I don't think you really need a script include for this. Your query string is almost there from what I can tell. try this:



This is an example where you want the name to start with generic:


javascript: 'install_status!=7^company=' + current.company + '^ORnameSTARTSWITHgeneric'



This is an example where you just want it to contain generic anywhere:


javascript: 'install_status!=7^company=' + current.company + '^ORnameLIKEgeneric'


View solution in original post

4 REPLIES 4

Jon Barnes
Kilo Sage

Actually, I don't think you really need a script include for this. Your query string is almost there from what I can tell. try this:



This is an example where you want the name to start with generic:


javascript: 'install_status!=7^company=' + current.company + '^ORnameSTARTSWITHgeneric'



This is an example where you just want it to contain generic anywhere:


javascript: 'install_status!=7^company=' + current.company + '^ORnameLIKEgeneric'


Thanks! That worked perfectly.



2 questions if you have a moment



1. On the query string, is there somewhere that defines the logic of the order the conditions are evaluated for the operators?   In the above string we have Condition1 AND Condition2 OR Condition3. Wondering how it groups them together by operator.



2. For a script include for a ref qual, is it a true or false answer the script should return, or should it return the list of options that can be selected?


1. I don't know if the encoded query strings are documented anywhere, but essentially, any ^OR is bundled with the nearest previous ^. The best way to play around with this is to create the filters you want in any list in servicenow, then you can right-click the filter and click Copy Query. This will give you the encoded query so you can see how SNOW encoded your filter.



2. There are a couple of ways to do this, but generally the script include just builds and returns the encoded query string (e.g. return 'install_status!=7'). You can return an array of sys_ids and join them together in your advanced qualification entry like this javascript: 'sys_idIN' + myScript.getIDs().join(','), but this is not as ideal because it could create a very long query string of sys_ids. There are no circumstances I can think of where you would return a true/false from this type of script.


Thanks Jon!