how to use on before script in transform map

Alon Grod
Tera Expert

Hi,

 

I have the fields in my table:

u date of type date

u_hour of type numeric

u_campaign of type string 

u_display_name of type string

 

in my xlsx file which im using to load data i only have u_date, u_hour and u_campaign.

the field u_display_name (does not exist in the xlsx file) should be the Coalesce and it should be auto populate before the transform map run by the combination of u_date + u_hour + u campaign.
How can I achieve that? can anyone help me with the script

 

4 REPLIES 4

Prasad Dhumal
Mega Sage
Mega Sage

Hello Alon,

You can concatenate the available fields and set it to the u_display_name field.

Configure onBefore Transform Script something like this:

function onBeforeTransform(current) {
  // Combine the values of u_date, u_hour, and u_campaign to form the value for u_display_name
  current.u_display_name = current.u_date + " " + current.u_hour + " " + current.u_campaign;
  return current;
}

 

And from the field map, you can set 'Coalesce' to true for display name.  

 

@Prasad Dhumal hi but i dont need to change the date and hour to string?

Pratik Malviya
Tera Guru

Hi @Alon Grod ,

If you wish to write Before transform then write below,

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

	// Add your code here
	//Combine the values of u_date, u_hour, and u_campaign to form the value for u_display_name
	if(source.u_display_name == ""){
		target.u_display_name = source.u_date + " " + source.u_hour + " " + source.u_campaign;

	}

})(source, map, log, target);

Also,

 You can try for after transform script as well.

Could be same as above for after just remember to add target.update(); in after transform script.

PratikMalviya_0-1675065949969.png

If it helps to resolve your issue, please mark correct

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Thanks,
Pratik Malviya

Basheer
Mega Sage

Hi @Prasad Dhumal ,

If you wish to populate u_display_name with the combination of u_date+u_hour+u_campaign, you can do as below to set the coalesce

Go to field maps and select script and the target field as u_display_name and make the coalesce checked

In the script do as follows:

 

var name = source.u_date + " " + source.u_hour + " " + source.u_campaign;
return name;

 

 

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.