Call fix script from scheduled job

xiaix
Tera Guru

For some reason, google searching isn't coming up with any results...

How to call a Fix Script from a scheduled job.

I'm sure it's easy but something I've yet to do and would like someone to post an example.  Thanks!

(and please, if your reply is "you can do all the code right in your scheduled job and don't need a fix script"... no need to reply as I have a very specific reason this needs to be done this way  😛 )

1 ACCEPTED SOLUTION

xiaix
Tera Guru

Well, this was a bit easier than I anticipated:

 

find_real_file.png

View solution in original post

5 REPLIES 5

Jaspal Singh
Mega Patron
Mega Patron

Follow below steps.

1. Create scheduled job with required name. Suppose: YorFixScriptName

2. Create fix script with below code & once saved copy sys_id of record.

var job = new GlideRecord('sysauto_script');
job.get('name', 'YourFixScriptName');
SncTriggerSynchronizer.executeNow(job);

3. Create an entry in sys_update table with format as below.

sys_fix_script_sysidoffixscript.xml something like 

sys_fix_script_9aed87713b41030060a8695593efc483.xml

4. Scheduled job created in Step 1 needs to have below code.

var gr = new GlideRecord('sys_update');
gr.addQuery('name', 'NamefromStep3');
gr.query();
var ab=gr.next();
if(ab)
	{
		var abc=new canclritm();//canclritm suppose is script include name that holds actual logic
		abc.run(); //abc being function name
	}

5. Script include to hold actual logic. For instance for above canclritm without Client Callable being checked.

var canclritm = Class.create();
canclritm.prototype = {
    initialize: function() {},

    run: function() {
        var canclritm = new GlideRecord('sc_req_item');
        //Replace below with query required by right clicking the last parameter from query & then copying
        canclritm.addQuery('number=RITM0112469');
        canclritm.query();
        while (canclritm.next()) {
            canclritm.state = '4';
            canclritm.stage = 'Request Cancelled';
            var salu = 'Dear ' + canclritm.request.requested_for.getDisplayValue() + ',';
            var body = 'Your ' + canclritm.cat_item.getDisplayValue() + ' was not submitted correctly. Please raise the request against a relevant service again if it is still needed.';
            //gs.print(salu+'\n\n'+body);
            canclritm.comments = salu + '\n\n' + body;
            canclritm.update();
        }
    },
    type: 'canclritm'
};

Thank you for your reply.

xiaix
Tera Guru

Well, this was a bit easier than I anticipated:

 

find_real_file.png

Anil Shewale
Mega Guru

Hi

You can schedule both conditional and non-conditional scripts.

Procedure

  1. Navigate to System Definition > Scheduled Jobs.
  2. Click New.
  3. Select Automatically run a script of your choosing.
  4. On the form, fill in the fields.

Example:

The following is an example of a conditional script. This example runs the scheduled job only if there are active incidents older than 30 days.

// Only run this Scheduled Job if there are active Incidents over 30 days old
var ga = new GlideAggregate('incident');
ga.addAggregate('COUNT');
ga.addQuery('active', 'true');
ga.addQuery('sys_created_on', '<', gs.daysAgo(30));
ga.query();
ga.next();
ga.getAggregate('COUNT') !== '0'

refer the following thread for more.

 

https://docs.servicenow.com/bundle/orlando-platform-administration/page/administer/reference-pages/t...

https://community.servicenow.com/community?id=community_question&sys_id=2aca876ddb5cdbc01dcaf3231f96...

If it help mark helpful or correct 

Thanks and regards

Anil