The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Transform Event Script Inserting instead of Updating

Marc D
Tera Contributor

Hey everyone. I'm having some technical difficulties with using a transform script. I currently have an onBefore transform event script that is meant to change what the mapping does to a sys_user record based on certain conditions. My basic algorithm is this:

 

 

if (condition1 == true) {
	var closest = new GlideRecord('sys_user');
	closest.get('sys_id', <sys_id gotten from previous functionality>);
	action = 'update';
	target = closest;
} else if (condition2 == true) {
	ignore = true;
} else {
	action = 'insert';
}

 

 

Basically, it's supposed to update an existing sys_user record under a certain condition, ignore under a second, and insert as a new record under a third.

However, it's not working properly. Despite gs.info() calls showing that the script is properly getting into each of the blocks, whenever it gets into block 1, it will insert instead of update. I cannot use the script debugger to get a closer look at the values because script debugger is not working with transform scripts.

 

I've narrowed it down to two possibilities, but I don't know which would be causing it, or how to remediate it in either case:
Option 1: I am coalescing the transform map on the field user_name, and this coalesce is overriding the functionality I set in this onBefore script.

Option 2: I am setting target and/or action incorrectly.

Which is the most likely option, and what can I do to remediate the issue?

Thank you in advance.

EDIT: I have tried removing the coalesce and it had no effect. Records were still inserting instead of updating. The only change was that my import set transforms now get errors.

3 REPLIES 3

Niklas Peterson
Mega Sage
Mega Sage

Hi Marc,

I doubt it can be done the way you are trying. Action is a result of the transform finding a record to update or not. And target will be the record found or a new record if not found.

 

If you want to identify different records to update you may script the logic for the coalesce field with a source field script. 

 

Regards,
Niklas

 

Jim Coyne
Kilo Patron

As @Niklas Peterson mentioned, you'll have to script it in a Field Map record.  This is how you can do it.

 

1. create a new Field Map where the Target field is "Sys id" on the User table.  Make sure "Use source script" and "Coalesce" are selected and the only field that you are coalescing on.
The Source script would look like this:

answer = (function transformEntry(source) {
	var result = "-1";  //default value to create a new record

	if (condition1 == "true") {
		result = sysId;  //we are going to update a record
	} else if (condition2 == "true") {
		result = "ignore record";  //as stated, we will ignore this one
	}
	return result;
})(source);

I don't know where you are getting the values for "condition1" and "condition2" in your script example so this will need some tweaking.

 

JimCoyne_0-1693338730831.png

 

2. At the top of the "Script" field on the Table Transform Map record itself, add this bit of code:

if (target.getValue("sys_id") == "ignore record") {
	gs.warn("Skipping import record - " + source.whatever_field_is_useful_if_needed);
	ignore = true;
}

This will skip the import record.

Marc D
Tera Contributor

Hello, Jim. Thank you and Niklas for the suggestion. My needs are a bit more complicated, however, and while your suggestion may have worked, it would have required substantial reworking of much of my condition building.

I ended up setting ignore to true within the condition1 block and, instead of trying to set "target", instead directly updated "closest" with GlideRecord functionality.

I will still give both you and Niklas helpful votes for your suggestions.