- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2024 06:21 AM
Hi,
I have a requirement where I have a custom table (u_user_details) and it has "Location" as reference field. I have to log error message if the value that user sends, in the "Location" field is not present in the cmn_location table then it should not create the record in the custom table and log error with message " Invalid location". If the correct value user selects, then it should create the record and log info that the record is created successfully.
I have to check this before the record is created into the custom table (u_user_details).
How can I achieve this requirement? Kindly, suggest!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2024 06:29 AM
Hi @SK41
To meet this requirement you’d typically use a Business Rule that executes before the record is inserted into the database.
Step 1: Create a New Business Rule
1. Go to System Definition > Business Rules in the navigator.
2. Click New to create a new Business Rule.
3. Fill out the form with the appropriate details:
- Name: Give your Business Rule a name, e.g., “Validate Location”.
- Table: Select your custom table (u_user_details).
- When to run: Set to Before insert.
var locationId = current.getValue('location'); // Assume ‘location’ is the field name in your u_user_details table
if (!isValidLocation(locationId)) {
// If the location is not valid, log error and abort the insertion
gs.addErrorMessage('Invalid location');
current.setAbortAction(true);
} else {
// If the location is valid, allow the record to be created and log info message
gs.info('Record for user ' + current.getValue('user') + ' created successfully with valid location.');
}
})();
// Function to check if the given locationId exists in cmn_location table
function isValidLocation(locationId) {
var locationGR = new GlideRecord('cmn_location');
locationGR.addQuery('sys_id', locationId);
locationGR.query();
return locationGR.hasNext(); // Returns true if a record exists, otherwise false
}
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning
Thanks & Regards
Deepak Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2024 06:29 AM
Hi @SK41
To meet this requirement you’d typically use a Business Rule that executes before the record is inserted into the database.
Step 1: Create a New Business Rule
1. Go to System Definition > Business Rules in the navigator.
2. Click New to create a new Business Rule.
3. Fill out the form with the appropriate details:
- Name: Give your Business Rule a name, e.g., “Validate Location”.
- Table: Select your custom table (u_user_details).
- When to run: Set to Before insert.
var locationId = current.getValue('location'); // Assume ‘location’ is the field name in your u_user_details table
if (!isValidLocation(locationId)) {
// If the location is not valid, log error and abort the insertion
gs.addErrorMessage('Invalid location');
current.setAbortAction(true);
} else {
// If the location is valid, allow the record to be created and log info message
gs.info('Record for user ' + current.getValue('user') + ' created successfully with valid location.');
}
})();
// Function to check if the given locationId exists in cmn_location table
function isValidLocation(locationId) {
var locationGR = new GlideRecord('cmn_location');
locationGR.addQuery('sys_id', locationId);
locationGR.query();
return locationGR.hasNext(); // Returns true if a record exists, otherwise false
}
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning
Thanks & Regards
Deepak Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2024 02:18 AM
Hi @SK41
Please Mark this Helpful also, If this Helps you in understanding.
- Keep Learning
Thanks & Regards
Deepak Sharma