How to display only records whose Contract field matches the logged-in user
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2022 04:22 AM
Hi Community,
I'm having a hard time setting variable in the catalog of the customer service portal.
I'm trying to display only the servers that match the Contract associated with the logged-in user.
Below are the current variable settings.
Application: Customer Service
Type: List Collector
Type Specifications
List table: Server [cmdb_ci_server]
Reference qualifier: (Where I am asking)
If it is a company, it could be realized by describing the following in the Reference qualifier, but it seems that the Contract cannot be made so easily.
javascript:"company=" + gs.getUser().getCompanyID();
I have little knowledge of scripts and have just started developing ServiceNow.
Please give me an approach to this challenge.
Thanks in advance.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2022 04:58 AM
Hi There,
I can help you with your reference qualifier, but for that I would require to know, which filed on the Server table is referring to the the user record or contract record. Once we have a relation then we can set the qualifier to display required record.
Please mark this as correct and helpful if it resolved the query or lead you in right direction.
Thanks,
Mohit Kaushik
Community Rising Star 2022
Mohit Kaushik
ServiceNow MVP (2023-2025)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2022 07:20 PM
Hello,
Thank you for the reply.
There is a Contract in the Related Lists.
There is also a Contract in the Related Lists of User (Contact). Server and User are not directly related.
Is this configuration insufficient? Do I need to add a Contract field instead of Related Lists?
Thank you in advance for your kind answers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2022 01:44 AM
Hi There,
So as per my understanding you are having a relationship between Server and Contract table via a filed called configuration item on Contract table.
Similarly you must be either having a user field or vendor field on contract table to have a relation between users and contracts.
So I have considered that you have vendor field on contract table, which is equivalent to Company field on User table. If my approach is correct then below script should be able to give you the answer. You will need to create a script include as shown below and then use it in Reference qualifier.
getServerAssociatedtoContract: function(){
//to get the company of logged in user
var user = new GlideRecord('sys_user');
user.get(gs.getUserID());
var comp = user.company;
//gs.info(comp);
//to get the contracts associated with the vendor (company of user)
var ci = [];
var contract = new GlideRecord('ast_contract');
contract.addQuery('vendor',comp);
contract.query();
while(contract.next()){
ci.push(contract.u_configuration_item); //configuration item mapped with each contract.
}
return 'sys_idIN'+ci.toString(); //query to filter server table
}
Reference qualifier can be:
javascript: new ScriptIncludeName().getServerAssociatedtoContract();
But if you have a User field present on the Contract table, then the script include can be more easy I believe. You will have to do below changes in it.
getServerAssociatedtoContract: function(){
//to get the contracts associated with the User
var ci = [];
var contract = new GlideRecord('ast_contract');
contract.addQuery('u_user',gs.getUserID());
contract.query();
while(contract.next()){
ci.push(contract.u_configuration_item); //configuration item mapped with each contract.
}
return 'sys_idIN'+ci.toString(); //query to filter server table
}
Please mark this as correct and helpful if it resolved the query or lead you in right direction.
Thanks,
Mohit Kaushik
Community Rising Star 2022
Mohit Kaushik
ServiceNow MVP (2023-2025)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2022 05:20 AM
Hi,
Thank you for your polite reply. I registered the following script based on your hint, but unfortunately it didn't work in my environment.
var getServerAssociatedtoContract = Class.create();
getServerAssociatedtoContract.prototype = {
initialize: function() {},
getServerAssociatedtoContract: function() {
//to get the contracts associated with the User
var ci = [];
var contract = new GlideRecord('ast_contract');
contract.addQuery('u_contacts', gs.getUserID());
contract.query();
while (contract.next()) {
ci.push(contract.u_cis); //configuration item mapped with each contract.
}
return 'sys_idIN' + ci.toString(); //query to filter server table
},
type: 'getServerAssociatedtoContract'
};
I don't use the vendor field. I have added CIs (u_cis) and Users (u_contacts) to the fields in the Contract table.
In the above example, I'd like to display only Test RHEL Server 001 when logging in as User01 or User03, but now it looks like all the CIs are visible.
If your time allows, I would be grateful if you could give me some advice on current situation.
Thank you for your kindness.