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

Hi Dominik,



PF the screenshot below.



find_real_file.png


find_real_file.png



Thanks


Sumanth


You need to remove condition is not empty. As otherwise in case you clear the field, you want to clear also the second field. You can also improve the Business Rule that if the first field is empty, you will directly set the second field to empty value without doing GlideRecord query to improve performance.


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.


Thanks a lot Dominic. It's working as expected.


dooma



Hi Dominik,



Further to the above query,I had one more requirement on the above. This is more about async.



If suppose when managed_by changes in the u_cmdb_appl_enterprise table , the same should update in u_custom_managed_by field on cmdb_ci_service table.



Can you please give me your inputs on this?



Thanks in advance.



Sumanth