Check 2 fields from 2 different table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2024 10:19 AM
Hello Community,
I have a situation in users table. I want to add a groups to user by looking at employee type in users table and check user Company ID is available in departmnet table. Right now we are using below query.
Taking employee type from users table and listed (hard coded)all company ID's in sys_property.
"New requirement":
1. Need to take employee type from users table and take company ID ( exit functionality)
2. Check Comapny ID is available in departmnet table. (Right now checking company ID from sys_property)
Below is the query we are using.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2024 09:10 PM
Hi @Kanna12 ,
you can try something like below
Business rule type make it has before update/insert
then add the Employee type changes
if (current.u_employee_type == 'employee' && current.department) { // check if the employee type is employee and has department information
var departmentGr = new GlideRecord("cmn_department"); //query to department table
departmentGr.addEncodedQuery('company' + current.company + '^sys_id' + current.department);// pass user department and company value
departmentGr.query();
if (departmentGr.next()) { //if present then check if the user is member of group or not
var empRec1 = new GlideRecord('sys_user_grmember');
empRec1.addQuery('user', current.sys_id);
empRec1.addQuery('group', '4691af77c316d610c7311412b4013164'); // sys_id of the group " Employee"
empRec1.query();
if (!empRec1.next()) {
empRec1.initialize();
empRec1.user = current.sys_id;
empRec1.group = '4691af77c316d610c7311412b4013164'; // sys_id of the group Employee"
empRec1.insert();
}
}
}
If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
Thanks,
BK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2024 09:22 PM
checking fields from two different tables can be done using GlideRecord queries in a script. To do this, you can query both tables separately and retrieve the values of the fields you're interested in. For example, if you want to check the priority field from the Incident table and the state field from the Change Request table, you can create a script where you first use GlideRecord to query the Incident table, retrieve the priority value, and then query the Change Request table for the state field. You can also query tables with related records. For instance, if the Incident table has a reference field pointing to the Change Request table, you can retrieve the reference field value, and then query the related Change Request table to check its state field. This approach allows you to efficiently check and compare data across different tables using the GlideRecord API
var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('active', true);
incidentGR.query();
if (incidentGR.next()) {
var incidentPriority = incidentGR.priority;
gs.info('Incident Priority: ' + incidentPriority);
} else {
gs.info('No active incidents found.');
}
var changeRequestGR = new GlideRecord('change_request');
changeRequestGR.addQuery('active', true);
changeRequestGR.query();
if (changeRequestGR.next()) {
var changeState = changeRequestGR.state;
gs.info('Change Request State: ' + changeState);
} else {
gs.info('No active change requests found.');
}