New JS field based on trimmed Extract field

I want to populate 2 x JS fields with data from a single Extract field

Extract field returns something like “10003211111 051”

JS field 1 needs to be “10003211111”
JS field 2 needs to be “051”

Part 1 may or may not exist so sometimes the extract field is just " 051" etc. In this instance

JS field 1 needs to be “” or empty
JS field 2 needs to be “051”

How can I achieve this as a JS expression?

Hi @marrd,

Hereby I would like to ask you the two following questions:

  1. Does the extract field always contains a value of the same length (fifteen or four characters long) and are both values – in the shared example “10003211111” and “051” – always of the same length?
  2. Are both values – in the shared example “10003211111” and “051” – always separated by a space?

Hi Marten

  1. No, it is possible to have different lengths
  2. Yes, always separated by a space

Hi @marrd,

Thank you for answering the asked questions.

Let’s say that you have the following three record fields “fieldOne”, “fieldTwo” and “fieldThree” whereby the Extract Mode of record field “fieldOne” is Location and the Extract Mode of the record fields “fieldTwo” and “fieldThree” are JavaScript, then you can use JavaScript code like the following for record field “fieldTwo”:

var result = "";

if (typeof record.fields.fieldOne === "string") {
	if (record.fields.fieldOne.split(" ").length == 2) {
		result = record.fields.fieldOne.split(" ")[0];
		// For debugging purposes only:
		//logger.info("fieldTwo (length): " + record.fields.fieldOne.split(" ")[0].length); // temp
	}
}

result;

And you can use JavaScript code like the following for record field “fieldThree”:

var result = "";

if (typeof record.fields.fieldOne === "string") {
	if (record.fields.fieldOne.split(" ").length >= 2) {
		result = record.fields.fieldOne.split(" ")[1];
		// For debugging purposes only:
		//logger.info("fieldThree (length): " + record.fields.fieldOne.split(" ")[1].length);
	} else if (record.fields.fieldOne.split(" ").length == 1) {
		result = record.fields.fieldOne.split(" ")[0];
		// For debugging purposes only:
		//logger.info("fieldThree (length): " + record.fields.fieldOne.split(" ")[0].length);
	}
}

result;

Personally, I would set both Field1 and Field2 to an empty string (via JS mode) in the Extract task.
Then I would add an Action task with the following code:

var myFields = data.extract(4,62,7,1,"").trim().split(" ");
var rec = { Field1: '',	Field2: '' };
rec.Field1 = (myFields.length == 2) ? myFields[0] : '';
rec.Field2 = myFields[myFields.length-1].trim();
record.set(rec);

Of course, you would change the data.extract() statement to match your own data.

I’m not saying my method is better or worse than Marten’s, it’s just an alternative using record.set(). I prefer doing it this way because all the JS code is grouped in a single location.

1 Like

Thank you Marten and Phil. Both methods seem to work perfectly, I appreciate your help

1 Like