Log error if the value is not present

SK41
Giga Guru

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!

 

 

1 ACCEPTED SOLUTION

Deepak Shaerma
Kilo Sage

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 

View solution in original post

2 REPLIES 2

Deepak Shaerma
Kilo Sage

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 

Deepak Shaerma
Kilo Sage

Hi @SK41 

 Please Mark this Helpful also, If this Helps you in understanding.
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma