Dot walking list type field in scripting

SumanthMora
Mega Guru

Hi folks,

I have two list type fields on a form both are referenced from other tables.

find_real_file.png           find_real_file.png

I need to populate u_enterprise_app_definition.managed_by   on to the Custom manged by field on change or update of Enterprise app definition field.

I have written the before business rule but dot walking is not working for list type fields. Can anyone give me the solution.

eg: current.u_custom_manged_by = current.u_enterprise_app_definition.managed_by; (but this is not working as the field type is list)

Thanks

Sumanth

1 ACCEPTED SOLUTION

Code snippet you can try:


var managers = [];


if (!gs.nil(current.u_enterprise_app_definition)) {


    var enterpriseAppDefinition = new GlideRecord('u_cmdb_ci_appl_enterprise');


    enterpriseAppDefinition.addQuery('sys_id', 'IN', current.getValue('u_enterprise_app_definition'));


    enterpriseAppDefinition.query();


    while (enterpriseAppDefinition.next()) {


          managers.push(enterpriseAppDefinition.getValue('managed_by'));


    }



    // Remove duplicates


    var arrayUtil = new ArrayUtil();


    managers = arrayUtil.unique(managers);


}



current.u_custom_manged_by = managers.join(',');



Just make sure you execute BR also when the value of Enterprise App Definition is empty.


View solution in original post

14 REPLIES 14

Dominik Simunek
Tera Guru

Hi Sumanth,



List field type will give you comma separated sys_ids of referenced records. So you need to build a query to the referenced table using "sys_id" "IN" <list-of-sys_ids> to get all the referenced records, iterate through them and add the dot-walked value to the result.



var managers = [];


var enterpriseAppDefinition = new GlideRecord('name-of-reference-table');


enterpriseAppDefinition.addQuery('sys_id', 'IN', current.getValue('u_enterprise_app_definition'));


enterpriseAppDefinition.query();


while (enterpriseAppDefinition.next()) {


  managers.push(enterpriseAppDefinition.getValue('managed_by'));


}


current.u_custom_managed_by = managers.join(',');



I did not test the code at all so please test it a lot :-). Also you might want to improve the logic to make sure every user is involved only once as manager in case two enterprise applications have same manager.



Best regards,


Dominik


Thank you Dominik.


Hi Dominik,



Yes as you said in case two enterprise applications have same manager the Custom Managed By field is filling with two values(duplicates).



If enterprise applications value is removed the managed by value is not getting removed.



Your suggestion please?



Thanks


Sumanth




To make list of managers unique, use ArrayUtil's unique method documented here: https://docs.servicenow.com/bundle/kingston-application-development/page/app-store/dev_portal/API_re...



Regarding the removal of manager, if the code I share is within onBefore business rule that is executed any time the Enterprise App Definition value is changed, I guess the manager list should be updated as well. If this is not the case, please share your business rule definition and the code you are using at the end.



Best regards,


Dominik