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

Abhijit4
Mega Sage

Hi @Winston 

 

I don't recommend to change scope to Global just because your one script is not working.

 

It could be due to the fact some information you are not able to access or not able to create record from scope. To do so, you can either manage it with cross scope privilege records or providing table's permission so that you can access those table from your new scope.

 

To further understand root cause of your problem, I suggest you to put your code in try catch block and see what exception it throws. Here is updated script for you with try catch block:

 

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;
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();
    }
}
}
catch(exe){
gs.error("Exception occured in business rule 'business_rule_name'. Exception details:"+exe.message);
}

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Thank you for the response, it's saying my array is undefined, but when I write the array variable to the logs it looks like it's hitting that information without issue.

 

No, actual error is, it says "cannot read sys_id from undefined.

 

Abhijit4_0-1747319559361.png

 

Below getRowCount() may not work in server side script, I guess it works only on client side. 

 

current.variables.transfering_assets.getRowCount();

 

So, you may need to use, something like below to get actual length of MRVS data and then execute further script:

var assetArry = JSON.parse(current.variables.transfering_assets);
var assetCnt = assetArry.length;

 

I suggest you to put some logs to see if you are getting record count correctly and further you are able to access sys_id.

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

I took a lot of my logs out before I posted it here, but the .getRowCount(); seems to be working fine in the scoped script and the script itself works fine in the global scope just not in my scope. I'll test again to confirm.

 

I'm gonna try looking into the cross scope permissions and see if that helps.

 

Thanks again for your help on this.