Script only works in global scope

Winston
Tera Guru

I created a Business Rule that triggers when a request item is closed. It pulls asset records from a MRVS with in that request item and creates a record on a custom table with the information. 

 

It works in the global scope but not the scope I created for this process. Should I just switch this project to global or is there something I can change in my script to make it work in my scope?

 

//Pulling the Date from the Date/Time field.
var closeDateTime = new GlideDateTime(current.closed_at);
var closeDate = closeDateTime.getLocalDate();

//MRSV from catalog form with asset information
var assetArry = current.variables.transfering_assets;
var assetCnt = current.variables.transfering_assets.getRowCount();

//loop that goes through each asset in the MRSV
for (i = 0; i < assetCnt; i++) {
    var gr = new GlideRecord('alm_asset');
    gr.addQuery('sys_id', assetArry[i].sys_id);
    gr.query();

    if (gr.next()) {
        //Creates the Record on the export staging table.
        var stagingRecord = new GlideRecord('x_fdohs_export_sta_export_staging');
        stagingRecord.initialize();
        stagingRecord.record_code = 'D';
        stagingRecord.interface_type = 'ret';
        stagingRecord.business_unit = '76000';
        stagingRecord.asset_id = gr.asset_tag;
        stagingRecord.transaction_date = closeDate;
        stagingRecord.accounting_date = closeDate;
        stagingRecord.insert();
    }
}
7 REPLIES 7

OlaN
Giga Sage
Giga Sage

Hi,

I'm guessing your Catalog item/Business rule is running in the global scope ?
Check the table permissions on your custom table/custom app, does it allow for other scopes to write to it ?
If so, check the cross-scope-permissions to allow for writing by the Business rule.

Ankur Bawiskar
Tera Patron
Tera Patron

@Winston 

your business rule should be in the scope of the Catalog item, I believe it's Global scope.

From Global scope you should be allowed to create record to your custom table

Ensure "Can Create" checkbox is True on your custom table under Application Access

Also update script as this and hopefully it should work

Parse the JSON string you get by converting it to object

try {
    //Pulling the Date from the Date/Time field.
    var closeDateTime = new GlideDateTime(current.closed_at);
    var closeDate = closeDateTime.getLocalDate();

    //MRSV from catalog form with asset information
    var assetArry = current.variables.transfering_assets.toString();

    var parsedData = JSON.parse(assetArry); // convert to object

    //loop that goes through each asset in the MRSV
    for (i = 0; i < parsedData.length; i++) {
        var gr = new GlideRecord('alm_asset');
        gr.addQuery('sys_id', parsedData[i].sys_id);
        gr.query();

        if (gr.next()) {
            //Creates the Record on the export staging table.
            var stagingRecord = new GlideRecord('x_fdohs_export_sta_export_staging');
            stagingRecord.initialize();
            stagingRecord.record_code = 'D';
            stagingRecord.interface_type = 'ret';
            stagingRecord.business_unit = '76000';
            stagingRecord.asset_id = gr.asset_tag;
            stagingRecord.transaction_date = closeDate;
            stagingRecord.accounting_date = closeDate;
            stagingRecord.insert();
        }
    }
} catch (exe) {
    gs.error("Exception occured in business rule 'business_rule_name'. Exception details:" + exe.message);
}

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

@Winston 

Hope you are doing good.

Did my reply answer your question?

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