Transform script to edit source field based on target field

Oliver Anderson
Kilo Sage

Hi, my scripting isn't that great so forgive me.

 

I have an import set table that looks like this:

u_department

u_rc

Accounting130
HR

202

Serviceville

101

 

I created a transform map that maps u_department and u_rc to name and id on the cmn_department table respectively. My cmn_department table looks like this:

name

id

Accounting 
HR202
Serviceville - Branch 20

 

 

Some departments in cmn_department are already complete, some are missing the ID, and some have " - Branch #"

appended to the name. 

 

I'm trying to create an OnBefore Transform Script that will change source.u_department to target.name if target.name contains source.u_department. I would also like to skip the rows that completely match, and update the id of rows in cmn_department that are empty.

 

This is what I have, but it is just adding every single row in my import set to cmn_department with the name field for each row blank:

 

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

	// Add your code here
	
	if (target.name.toString().includes(source.u_department)) {
		source.u_department = target.name.toString();
	} else {
		ignore = true;
	}
	
})(source, map, log, target);

 

 

 

OliverAnderson_0-1682686021618.png

 

 

Thanks for any help!

5 REPLIES 5

Kartik Choudha1
Tera Guru

Hi @Oliver Anderson 

 

Hope you are doing well!

 

Requirement is not that clear to me. As you mentioned import set table has 'u_name' and 'u_id' but in Transform map's field map it is u_rc and u_department.

 

Could you explain little bit on it. I would be able to help you on this

 

By enabling coalesce field on ID field you can update the record which has same ID in target table otherwise it will create the new one. You can use ID as unique identifier here and Based on ID it will update/create records in target table. 

I suggest you to use one field that uniquely identifies the record that will solve the problem here.

 

Regards,

Kartik

 

 

 

Sorry, I decided to generalize the field names prior to adding the screenshot with the actual field names thinking it would be easier to understand. I will edit my post with accurate field names. u_rc = u_id / u_department = u_name

Hi

 

Can you try with coalesce field which i mentioned in first reply.

did you get the reason why '-Branch ' is getting appended?

 

'- Branch' is appended to some of the departments in cmn_department table (target) due to Active Directory sync. That is the way we signify branches in the 'department' attribute on AD user accounts.

 

However, in the Import Set Table (source), there is no '- Branch' on the branch names. During the transform, I'm trying to match the non-appended rows in the source with their respective 'Name - Branch' rows in the target table with that logic in the script.

 

Instead of setting the name, for example, from 'Serviceville' to 'Serviceville - Branch 20', it is just emptying the u_department field.