org.mozilla.javascript.NativeArray populating instead of array values

SNAdmin47
Kilo Sage

I'm trying to use this before business rule on the sys_user table to populate a field on the core_company table when a user is saved and is a company change approver (i.e., 'RFC Approver' field is true ). The script is looking up any other users for the same company and is also a change approver, and then populating the 'u_company_change_approvers' field on the company form the users belong to. This list of users is needed for inclusion on a notification to fire when certain change events are triggered. 

 

This would be fine, and the logs that are returned are all positive.... the company sys_id is correct, the users sys_ids returned are correct, and the array value is correct (i.e., 2 sys_ids, each belonging to the users returned as expected) but when the script populates the field on the company form it's persistently populating it with the value 'org.mozilla.javascript.NativeArray@118ceee'

 

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

    var companyValue = current.company;
    gs.log('The company value is: ' + companyValue);

    var companyApproversList = [];

    var companyApprovers = new GlideRecord('sys_user');
    companyApprovers.addQuery('company', companyValue);
    companyApprovers.addQuery('active', true);
    companyApprovers.addQuery('u_rfc_approver_', true);
    companyApprovers.query();
    while (companyApprovers.next()) {
        gs.log('The user returned is :' + companyApprovers.getDisplayValue());
        companyApproversList.push(companyApprovers.getUniqueValue().toString());
    }

    var parentCompany = new GlideRecord('core_company');
    parentCompany.addQuery('sys_id', companyValue);
	parentCompany.query();
    if (parentCompany.next()) {
        parentCompany.u_company_change_approvers = companyApproversList;
        parentCompany.update();
		gs.log('parentCompany value is: ' + parentCompany.getDisplayValue());
    }

    gs.log('The companyApproversList is: ' + companyApproversList);

})(current, previous);
  • I initially thought that it was the script but the logs I'm getting back are consistently correct.
  • I spent a while trying to track down the user for that value until I realised that it doesn't exist at all. 
  • I've tried adding toString (as per above) and a variety of different methods but it's still persistently refusing to populate the field with the array value and insists on using the Mozilla content. 

Does anybody know why this is happening, and more importantly, how I can configure the script to work correctly? Many thanks in advance for any assistance you can offer. 🙂

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

On your custom field named u_company_change_approvers on the core_company table, what is the field type and reference table?  Sounds like it should be a list on sys_user, so assuming that's true you can try some alternate methods changing these two lines:

companyApproversList.push(companyApprovers.sys_id.toString());

parentCompany.u_company_change_approvers = companyApproversList.join(',');

  

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

On your custom field named u_company_change_approvers on the core_company table, what is the field type and reference table?  Sounds like it should be a list on sys_user, so assuming that's true you can try some alternate methods changing these two lines:

companyApproversList.push(companyApprovers.sys_id.toString());

parentCompany.u_company_change_approvers = companyApproversList.join(',');

  

SNAdmin47
Kilo Sage

Thank you @Brad Bowman  , that worked a treat!! I'm very grateful for your assistance there, I was scratching my head and you've saved me hours of frustration, thank you! You're quite right, it's a list field referencing the sys_user table... apologies, I should have been clearer on that. 

 

Are you able to explain why this was happening? I'm very curious where the 'org.mozilla.javascript' string was coming from, especially since I'm using Chrome and not Firefox as a browser. It's not essential, but it'd be nice to know and understand, I'm just very curious. Regardless, thank you once again. 😁

 

I did find that the field wasn't updating every time a user was saved so added a line to clear the field contents and then re-add the query results. I also tested and the .toString was now superfluous so removed that. Just in case anybody is ever hitting the same issue and comes across this post, this is the final script that seems to be working fine:

 

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

    var companyValue = current.company;
    gs.log('The company value is: ' + companyValue);

    var companyApproversList = [];

    var companyApprovers = new GlideRecord('sys_user');
    companyApprovers.addQuery('company', companyValue);
    companyApprovers.addQuery('active', true);
    companyApprovers.addQuery('u_rfc_approver_', true);
    companyApprovers.query();
    while (companyApprovers.next()) {
        gs.log('The user returned is :' + companyApprovers.getDisplayValue());
        companyApproversList.push(companyApprovers.getUniqueValue());
    }

    var parentCompany = new GlideRecord('core_company');
    parentCompany.addQuery('sys_id', companyValue);
	parentCompany.query();
    if (parentCompany.next()) {
		parentCompany.u_company_change_approvers = "";
        parentCompany.u_company_change_approvers = companyApproversList.join(',');
        parentCompany.update();
		gs.log('parentCompany value is: ' + parentCompany.getDisplayValue());
    }

    gs.log('The companyApproversList is: ' + companyApproversList);

})(current, previous);