OL Learn

Post Function to return value based on another field's value

Would like to check the value of another field (extracted previously) and based on the value, either set the new field to nothing “” or to return the new value.

if (record.fields.CODE == 'S'){
    //return empty value
} else {
    //return extracted value 
}

Probably don’t need the else

if (record.fields.CODE == 'S'){
    //return empty value
}

I don’t think that the post function will allow it.

Plan B is to use an action script in the datamapper.
This works for two of the fields which are strings, but not on the field that is a float

I assume that you would like to apply the JavaScript code to assign a value to a Field extracted by a Extract Step, is it not? Should this be correct then I assume that you have to use the following JavaScript code instead:

var result = "";

if (record.fields.CODE !== "S") {
	result = "Example";
}

result;

I get an error when I attempt this in the DM Post Function.

image


image

What I’m looking for is if record.fields.HOURLY_SALARIED_CODE equals ‘S’ then I want to replace the extracted value with zero or empty value, but otherwise if record.fields.HOURLY_SALARIED_CODE is anything else to keep the extracted value.

I’m working on another approach which is partially working.
Added an action script.

image


image

How can I set EARNINGS_HOURS to ‘0’ if record.fields.HOURLY_SALARIED_CODE equals ‘S’?

I completely missed that I was passing a string instead of the number.
After removing them this now appear to work in the datamapper design view.
Still need to test all the way to output.

Hi @UomoDelGhiaccio, I am glad to hear that you were able to resolve the error message you were seeing.

Please note that this* JavaScript code isn’t meant to be used as a Post function script but as follows:

The step property Based on needs to be changed from Location to JavaScript in order to be able to execute the shared JavaScript code.

*By this I’m referring to the following JavaScript code, shared through my previous comment:

var result = "";

if (record.fields.CODE !== "S") {
	result = "Example";
}

result;

Thanks, that make more sense.

My 3 field definitions are already using javascript to build the values.
They threw this twist at the last moment, so I’m going with the action script because I suspect their might me more. I can then control them all at the same location instead of on each field definition.

In case you want to avoid scripting you could also use a Condition step and set the value of the EARNINGS_HOURS to 0 or null in one branch and extract the actual value in the other. Not sure if this is helpful but thought it was worth sharing.

Erik