Scheduled Import Not working when triggering from BR

shrikant Kahar1
Tera Contributor

Hello Team,

 

I'm facing issue while scheduled import triggering from BR in Global Application.

BR Table : sc_req_item

BR Condition: After Insert

Please find the Business Rule Script Below

 

(function executeRule(current, previous /*null when async*/ ) {
    var attachment = new GlideSysAttachment();
    var attached = attachment.copy('sc_req_item', current.sys_id, 'sys_data_source', '430167151bbca61064e5ed73b24bcb27'); //datasource sysid
    if (attached) {
        gs.info('Attachment copied from: ' + current.number);
        var grImp = new GlideRecord("scheduled_import_set");
        if (grImp.get('f12f0225c3f86a1016f64bcd2b013174')) {
            gs.info('import loaded');
            //schedule data import sysid
            gs.executeNow(grImp);
        }
    }
})(current, previous);
 
Thanks in advanced
Shrikant
11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

@shrikant Kahar1 

how are you debugging?

This log came  -> gs.info('import loaded');

Also the script might have executed but the fetching of data might have failed -> did you check this?
what happens if you manually click Execute now on that scheduled import?

Are you copying file to the data source associated to that scheduled import and then triggering?

If yes then give some sleep of 5-10 seconds, may be the scheduled import when triggered didn't find the file till then

(function executeRule(current, previous /*null when async*/ ) {
    var attachment = new GlideSysAttachment();
    var attached = attachment.copy('sc_req_item', current.sys_id, 'sys_data_source', '430167151bbca61064e5ed73b24bcb27'); //datasource sysid
    if (attached) {
        gs.info('Attachment copied from: ' + current.number);
        var grImp = new GlideRecord("scheduled_import_set");
        if (grImp.get('f12f0225c3f86a1016f64bcd2b013174')) {
            gs.info('import loaded');
            //schedule data import sysid

			// added 10 seconds sleep
            var secondsValue = 10;
            var seconds = parseInt(secondsValue, 10) * 1000;
            var start = parseInt(new Date().getTime()) + seconds;
            while (start > parseInt(new Date().getTime())) {
                // do nothing
            }

            gs.executeNow(grImp);
        }
    }
})(current, previous);

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

Hello @Ankur Bawiskar ,

Thanks for the Solution

This log came  -> gs.info('import loaded'); --> Yes Its Coming 

Also the script might have executed but the fetching of data might have failed -> Script is working fine for the Update Operation Only for insert we are facing issue.

what happens if you manually click Execute now on that scheduled import? -> Its working Fine

Are you copying file to the data source associated to that scheduled import and then triggering? : YES

 

I tried your script as well but it's not working.

 

Thanks

Shrikant

 

 

 

@shrikant Kahar1 

Since you said the BR script and data is getting loaded fine when record is updated, so there's no issue with your script.

BR is running on which table and what's your actual requirement?

I still believe it's issue with the timing i.e. the file is not getting copied by the time this line triggers

            gs.executeNow(grImp);

Also I believe you should 1st remove the earlier file attached to that data source just before copying a fresh one

like this

(function executeRule(current, previous /*null when async*/ ) {

    // delete all earlier files from data source
    var dataSourceRec = new GlideRecord('sys_data_source');
    if (dataSourceRec.get('430167151bbca61064e5ed73b24bcb27')) {
        var attach = new GlideSysAttachment();
        attach.deleteAll(dataSourceRec);
    }

    var attachment = new GlideSysAttachment();
    var attached = attachment.copy('sc_req_item', current.sys_id, 'sys_data_source', '430167151bbca61064e5ed73b24bcb27'); //datasource sysid
    if (attached) {
        gs.info('Attachment copied from: ' + current.number);

        // added 10 seconds sleep
        var secondsValue = 10;
        var seconds = parseInt(secondsValue, 10) * 1000;
        var start = parseInt(new Date().getTime()) + seconds;
        while (start > parseInt(new Date().getTime())) {
            // do nothing
        }

        var grImp = new GlideRecord("scheduled_import_set");
        if (grImp.get('f12f0225c3f86a1016f64bcd2b013174')) {
            gs.info('import loaded');
            //schedule data import sysid
            gs.executeNow(grImp);
        }
    }
})(current, previous);

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

@shrikant Kahar1 

Any update to this?

 

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