How to update a target field without coalesce in transform map

Dharma5
Tera Contributor

Hi 

 

I want to update an active field in target table if business area is XYZ, I tried the code below but it is inserting a new record instead of updating existing record

 

var resGrp1 = new GlideRecord('restricted_groups');
resGrp1.addQuery('group_id', source.u_group_number);
resGrp1.addQuery('business_area', XYZ);
resGrp1.addQuery('active', false);
resGrp1.query();
if(resGrp1.next()){
resGrp1.active = true;
resGrp1.update();

 

can anyone help me on this, Thanks in advance.

5 REPLIES 5

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Dharma5 

 

You can create a field mapping for Active field with Source Script as below:

 

AnubhavRitolia_0-1665939812754.png

Find the same code below:

 var resGrp1 = new GlideRecord('restricted_groups');
            resGrp1.addQuery('group_id', source.u_group_number);
            resGrp1.addQuery('business_area', 'XYZ');
            resGrp1.addQuery('active', false);
            resGrp1.query();
            if (resGrp1.next()) {
                return true; 
			} else {
				return false;
			}

 

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Hi Anubhav,

 

Thanks for the reply.

 

But we cannot use field map script here for active field. Because we’re not mapping active field, we want to update it through some validations.

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Dharma5 

 

Than you can write below code in Transform Script as onAfter:

 

var resGrp1 = new GlideRecord('restricted_groups');
resGrp1.addQuery('group_id', source.u_group_number);
 resGrp1.addQuery('business_area', 'XYZ');
resGrp1.addQuery('active', false);
resGrp1.query();
if (resGrp1.next()) {
target.active = true;
} else {
target.active = false;
}
target.update();

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Hi @AnubhavRitolia 

 

I tried writing that script in onAfter, it is making old record in target table to true but it was again creating a new record and we need to avoid that(creating new record in target table).