Datamapper - if a field exists condition

Hi

I am trying to create a single data mapper for 2 different types of data input (csv) all field headers are the same between the 2 files except 1 of the files has an additional header field.

How can I check to see if this field exists before extracting in datamapper?

I have a condition on my template based on if this field exists.

Thanks

Liam

Hi iBaldie

I’m not aware of a method to check the field before mapping. I suppose another Preprocessor script could be used. However I have encountered something like this before. My way of resolving this is to not have the mapper use the first row for field names. You need to extract them and name them yourself and set Lines To Skip to 1. I added the extract fields using the database with the most fields first. Then wrote a Preprocessor script that will add blank columns to the data. Do note that this method requires all databases columns/fields to be in the same order and the Preprocessor script will need to be run manually in the data mapper while you are designing your template each time the data mapper is opened.

Sample Data Mapper

Hope this helps.

Regards,

S

Hi Sharne

Thanks for this, I think I got there by adding the suspect field first with blank content, then update the contents with a try:

if(record.fields["suspect_field"]=='')
{
    try { record.fields["suspect_field"] =  data.extract('suspect_field',0);}
    catch(err){false;}
}

Was your script added to the template and not the data mapper?

I have it in datamapper as a conditional javascript. It will return false for data that doesn’t contain the suspect_field header or true for data that does contain it and extract the contents of the field.

Here is a shorter bit of Javascript which I have used in many datamappers:

data.fieldExists('suspect_field')?data.extract('suspect_field',0):false;

Nice! I always like to know what other methods others are using.

Nice! I didn’t realise there was a method called fieldExists, is this in the help documentation?

It’s in the datamapper API documentation: http://help.objectiflune.com…datamapper/API/Method_data_fieldExists.htm