Need help on scheduled Job

Shahida07
Tera Contributor

I need to create scheduled job like first need to check if empid is 7 digited number is valid or not if it is 7 digited empid then I need to clear the UserID field as empty can someone help me on this?

 

 

Thanks in advance

1 REPLY 1

Marcos Kassak
Kilo Sage
Kilo Sage

Hi @Shahida07,

 

If you provide more information like table, field name, and which actions you need to take it might get easier for everyone to help you.

 

Steps to Create Scheduled Job:


1. Navigate to System Definition > Scheduled Jobs in the Application Navigator.
2. Click New to create a new Scheduled Job.
3. Fill in the necessary fields, such as Name and Run.
4. In the Script field, you can write a JavaScript that performs the required task.


Example Script:


Here’s a basic example:

 

// Initialize a GlideRecord object for your table.
// Replace 'your_table_name' with the actual name of your table.
var gr = new GlideRecord('your_table_name');

// Query the table to retrieve all records.
// You can add specific conditions if you want to filter the records.
gr.query();

// Loop through each record retrieved from the table.
while (gr.next()) {
    // Log the current record's empid for debugging purposes (optional).
    gs.info('Processing record with empid: ' + gr.empid);
    
    // Check if the 'empid' field is a 7-digit number.
    // Replace 'empid' with the actual field name you want to check.
    if (/^[0-9]{7}$/.test(gr.empid)) {
        // If 'empid' is a 7-digit number, log this information (optional).
        gs.info('Empid ' + gr.empid + ' is a 7-digit number. Clearing user_id.');
        
        // Clear the 'user_id' field.
        // Replace 'user_id' with the actual field name of your UserID in the table.
        gr.user_id = '';
        
        // Update the record to save the changes.
        gr.update();
    } else {
        // If 'empid' is not a 7-digit number, log this information (optional).
        gs.info('Empid ' + gr.empid + ' is not a 7-digit number. Doing nothing.');
    }
}

// Log the completion of the script (optional).
gs.info('Script execution completed.');

 

Notes:


1. Replace your_table_name, empid, and user_id with the actual names of your table and fields.
2. This script logs messages to the system log, which can be helpful for debugging. You can remove the gs.info lines if you prefer not to log these messages.
3. Please test this script in a sub-production instance before running it in a production environment to ensure it behaves as expected and to avoid unintended data modifications.

 

If you found my answer helpful or correct in any way, please don't forget to mark it to help future readers! 👍

 

--

 

Kind regards,


Marcos Kassak
Solution Consultant  🎯