Background Job Configuration

Sneh
Tera Contributor

Hello All,

I have created a new background job configuration in sn_sec_cmn_background_job_configuration in this table using the logic written in other backgroup job config(i.e OOB) 

Can anyone suggest me how I run this background job as I'm not able find any appropriate doc in servicenow for this.

Aim is to use background job rather than Scheduled Script. 

5 REPLIES 5

KetilE
Tera Contributor

Hi!

There is a Script Include called 'BackgroundJobUtils' which has a method 'createBackgroundJob'. You can call this method in a script and thereby put a background job in queue. After that you can call the 'startJob' method in the same script include and pass the job and it will start. See Edit below. 


createBackgroundJob() method:

 

 

/*
     * Creates a background job
     * table - name of the table which triggered the creation of background job
     * jobType - action which triggered the background job creation
     * params - parameters needed for the job to run
     * documentId - sys_id of the triggering record
     * Returns the sys_id of the created job
     */
    createBackgroundJob: function(table, jobType, params, documentId, parentJob, jobConfigId, userDomain, jobRun) {
        var job = {
            link: "",
            number: ""
        };
        try {
            var gr = new GlideRecord("sn_sec_cmn_background_job");
            gr.initialize();
            gr.table = table;
            gr.job_type = jobType;
            gr.job_configuration = this.getJobConfig(jobConfigId);
            gr.state = this.QUEUED;
            if (params)
                gr.parameters = params;
            if (documentId)
                gr.document_id = documentId;
            if (parentJob)
                gr.parent_job = parentJob.getUniqueValue();
            if (jobRun)
                gr.background_job_run = jobRun;
            if (!gs.nil(userDomain))
                gr.sys_domain = userDomain;
            var sys_id = gr.insert();
            gr = new GlideRecord("sn_sec_cmn_background_job");
            gr.get(sys_id);
            job.link = gr.getLink(false);
            job.number = gr.getValue("number");
        } catch (e) {
            gs.error("Error occurred while creating Background Job for - " + jobType + " Error was : " + e + '\n' + e.stack);
        }
        return job;
    }

 

 

EDIT
After some additional testing, it turned out that a user (SecCommon.System) was missing in our dev instance after cloning. This user is tasked with running scheduled jobs which trigger the background jobs, and since it was missing in out dev instance the background jobs were not triggering. 

So the below method is not required to trigger the background job, simply create the job and it will automatically run. 

 

startJob() method:

 

 

/* sets the job status to running and started_at field
     */
    startJob: function(job, subState, stateDescription, notes) {
        try {
            job.setValue("state", this.IN_PROGRESS);
            job.setValue("started_at", new GlideDateTime());
            if (subState)
                job.setValue("substate", subState);
            if (stateDescription)
                job.setValue("state_description", stateDescription);
            if (notes)
                job.setValue("notes", notes);
            job.update();

        } catch (e) {
            gs.error("Error occurred while starting Background Job for - " + job.getValue("number") + " Error was : " + e + '\n' + e.stack);
        }
    }

 

 

Link to script include:

https://<YOUR-INSTANCE>.service-now.com/sys_script_include.do?sys_id=2b5a3d5dc77333007393ec22c7c26046