Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

schedule job script

levino
Giga Guru

Hi Team

please check if this scheduled job script is ok or if i am missing something?

var gr = new GlideRecord('sc_req_item');
gr.addQuery('state', 'Closed Complete');
gr.addQuery('cat_item', '89a103df8710da1016a443770cbb3523'); // Replace with your catalog item sys_id
gr.query();
while (gr.next()) {
    // Check if select_variable is 'developer' or 'designer'
    if (gr.variables.request_template == 'Developer' || gr.variables.request_template == 'Designer') {
        // Schedule a notification job to run 1 hour later
        var job = new GlideRecord('sys_trigger');
        job.initialize();
        job.name = 'Send Closed Request Notification';
        job.script = "sendClosedRequestNotification('" + gr.sys_id + "');";
        job.next_action = new GlideDateTime();
        job.next_action.addSeconds(3600); // 1 hour later
        job.insert();
    }
}

// Define the function to send the email
function sendClosedRequestNotification(requestItemSysId) {
    var gr = new GlideRecord('sc_req_item');
    if (gr.get(requestItemSysId)) {
        // Prepare email details
        var emailBody = "Your request has been closed.\n\n";
        emailBody += "Request Number: " + gr.number + "\n";
        emailBody += "Requested Item: " + gr.cat_item.getDisplayValue() + "\n";
        emailBody += "Requested For: " + gr.requested_for.getDisplayValue() + "\n";
        emailBody += "Description: " + gr.description + "\n";

        // Add requested item variables
        emailBody += "Requested Item Details:\n";
        emailBody += "Variable 1: " + gr.variables.request_template + "\n"; // Replace 'variable1' with the actual variable name
        emailBody += "Variable 2: " + gr.variables.description + "\n"; // Replace 'variable2' with the actual variable name
        // Add more variables as needed

        // Send Email
        var email = new GlideEmailOutbound();
        email.setSubject("Your Request is Closed");
        email.setBody(emailBody);
        email.setTo(gr.requested_for.email + ", second.recipient@example.com"); // Replace with the appropriate email addresses
        email.send();
    }
}

 

1 ACCEPTED SOLUTION

levino
Giga Guru

Thank You Ankur

Legend!  

 

btw

i am from the same city as u, but currently overseas

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@levino 

update this line as 3 means close complete. use that and share us the results

gr.addQuery('state', '3');
 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

levino
Giga Guru

Thank You Ankur

Legend!  

 

btw

i am from the same city as u, but currently overseas