Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Help with removing values from array

sndev1099
Giga Expert

Hi, I need some help with setting a field from an array. My below business rule runs on an m2m table and updates the OIM entitlements field when any values are added to the related list table. However, I need some script for the if current.operation() == delete part. Can anyone help? 

Here is an example, I have added 6 entitlements and it's updated the OIM entitlements field with the sys_id's. However, if they are removed, I need to update OIM entitlements field with new values (possibly push new values to an array?)

find_real_file.png

 

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


    var m2moim = new GlideRecord('x_lbg_insider_risk_insider_access_lookup');
    m2moim.addQuery('sys_id', current.ir_access_categorisation);
    m2moim.query();

    if (current.operation() == 'insert') {


        while (m2moim.next()) {

            var entList = [];
            var m2Gr = new GlideRecord('x_lbg_insider_risk_m2m_catalog_acce_ir_access_ca');
            m2Gr.addQuery('ir_access_categorisation.sys_id', m2moim.sys_id);
            m2Gr.query();

        }

        while (m2Gr.next()) {

            entList.push(m2Gr.getValue('sys_id'));
            m2moim.u_oim_entitlements = entList.join(',');
            m2moim.updateMultiple();
		

        }
    } else if (current.operation() == 'delete') {
		
	
		
		}
	
		
})(current, previous);
1 ACCEPTED SOLUTION

Baala T
Mega Guru

Hi,

I just modified the code, just give a try,

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

    var m2moim = new GlideRecord('x_lbg_insider_risk_insider_access_lookup');
    m2moim.addQuery('sys_id', current.ir_access_categorisation);
    m2moim.query();

    if (current.operation() == 'insert') {
		var entList = [];

        while (m2moim.next()) {

            var m2Gr = new GlideRecord('x_lbg_insider_risk_m2m_catalog_acce_ir_access_ca');
            m2Gr.addQuery('ir_access_categorisation.sys_id', m2moim.sys_id);
            m2Gr.query();

			while (m2Gr.next()) {
				entList.push(m2Gr.getValue('sys_id'));
			}

            m2moim.u_oim_entitlements = entList.join(',');
            m2moim.updateMultiple();
        }
    } else if (current.operation() == 'delete') {

		var entList1 = [];
		
        while (m2moim.next()) {
           
            var m2Gr1 = new GlideRecord('x_lbg_insider_risk_m2m_catalog_acce_ir_access_ca');
            m2Gr1.addQuery('ir_access_categorisation.sys_id', m2moim.sys_id);
            m2Gr1.query();

			while (m2Gr1.next()) {
				entList1.push(m2Gr1.getValue('sys_id'));
			}
			
            m2moim.u_oim_entitlements = entList1.join(',');
            m2moim.updateMultiple();
		}
    }
})(current, previous);

 

Regards,
Bala T

View solution in original post

5 REPLIES 5

Bala, thank you so much. I have been stuck on this for days!