OL Learn

Split String with delimiter

Hello, I have problems while doing a datamapping for a pdf file. Since it’s a invoice with multiple source codes, the codes look like the following example:

JJA- SI335/42522414214213

So I have 2 questions in this case:

  1. Since I already created a repeat step to capture the full source code by position, say I already set the name of the field as [“fullSourceCode”], how should I call the string in another field? I tried the following code but it didn’t work.

record.tables.detail[“fullSourceCode”];

  1. Since I only need the number after the delimiter ‘/’, so may I ask how to split the full string into a new field in the table?

I am completely new to Planet press and JavaScript, sorry in advance for the noob questions, and any help would be greatly appreciated.

Many thanks.

Hi HK,

If your fullSourceCode is in a detail table you will need to do this in the template. And since it is in a detail table, the table could consist of many rows, so you would need to specify an index as to what line your code is in. Add a standard script to your template and add the following code. (In the below script I’m assuming the code is in the first row, therefore an index of 0.

results.each(function(index) {
	var arrfield, result = "";
	//Target index 0 of detail table (["detail"][0]) that has the SourceCode you want to return.
	//Use the .split("") function to split the string on "/" to create an array.
	arrfield = record.tables["detail"][0].fields["fullSourceCode"].split("/");
	//return index 1 of array field that contains just the number after the /
	if (arrfield[1] !== "") result += arrfield[1];
	
	this.html(result);
});

Regards,
S

To read the value you stored in you detail table, you have to specify the index of the row where it is stored since there can be multiple rows. After that, you use the split() method, which returns an array of elements based on a separator ("/", in this case) and you extract the second element of that array.
Something simliar to this:

var code = record.tables.detail[0].fields["fullSourceCode"];
code.split("/")[1];

Note that the above code picks the fullSourceCode field from the first row of the detail table (index [0]).

EDIT: Sorry, for some reason, @Sharne’s reply did not show up on my monitor until after I posted my own reply. I’m still leaving mine here in case @HKPoSFJ wanted to apply the procedure in the DataMapper rather than in the Template.

Hi Phil,

I was wondering why I could not get it working in the mapper :slight_smile: My brain was slow this morning and I simply used something like:

record.tables.detail[0].fields["fullSourceCode"].split("/")[1];

Must have made a typo or something :man_facepalming: :rofl: , so I just went ahead with a template script solution.

Regards,
S

Hi Sharne and Phil,

Thank you both for your help, at the moment I don’t really have the template here for my data mapping, so I used the 2nd script and it works.

Thanks so much for your effort. :slight_smile:

Regards,
SFJ

Hi Phil,

Thank you so much for your help last time, and hope this message find you well.

As there is a new situation, so I would like to have a follow up question.

With the above script, I was able to read the value stored in the detail table. However, since I have multiple records in the nested table this time. I have created a new field with the above code, but when I changed to other record, the new field doesn’t change along.

May I ask how to modify the script, and make it change along with different records.

Thanks and regards,
SFJ

The detail[0] portion of the expression is what gives you access to each separate record in the detail table. The 0 value accesses the first record, 1 accesses the second record, and so on.
You could write a loop to go through each individual record.