How to validate a field on another table via a Transform Map script?

Mat Gdowski1
Tera Contributor

Howdy all!

 

My JavaScripting must be a touch rusty, so I'm requesting some assistance.  🙂  

 

What I'm looking to do is validate a country name from my import table (before adding it to the target table) from another table with the list of countries.  If the country matches, then it can use that country name and update the target table with it.  If it doesn't match any of the country names in the other table, then it will post a message to the import log and ignore the record.

 

So, at this point, with the scripting I have below, it is currently rejecting all of the records and not updating any.  😞  
So, I'm guessing I'm missing something or have something in the wrong spot for this to work properly.  Any help is appreciated.

 

Info:

Using a "onBefore" Transform Script in the Transform Map

Source table country field:  u_country

Country list table:  u_fips_country

 

Code:

(function runTransformScript(source, map, log, target /*undefined onStart*/) {

    var sourCountryName = source.u_country;
    var tabFipsCountry = new GlideRecord('u_fips_country');
    tabFipsCountry.addQuery('u_country_name, sourCountryName');
    tabFipsCountry.query();    
   
    if (tabFipsCountry.next()){
       
        if (sourCountryName == tabFipsCountry) {
            var varifCountry = tabFipsCountry.u_country_name;
            return varifCountry;  
        }
        else {
            log.error('The country name, ' + tabFipsCountry + ', does not match a FIPS Country Name');
            ignore = true;
        }
    }
})(source, map, log, target);
 
Again, any help on this would be truly appreciated.
 
Thanks,
Mat
 
# scripting #transform map script #import 
1 ACCEPTED SOLUTION

Johns_MP
Giga Guru

Hi @Mat Gdowski1 ,

Almost correct

On a first glance i can see that you have misplaced the apostrophe in the third line.

Replace the third line with below:

 tabFipsCountry.addQuery('u_country_name', sourCountryName);

 

try this and see if it fixes the issue.

Also you can try replacing the code as below (This is only for handling if there is no record present) (i had just replaced the '}' )

 

  if (tabFipsCountry.next()){
       
        if (sourCountryName == tabFipsCountry) {
            var varifCountry = tabFipsCountry.u_country_name;
            return varifCountry;  
        }
}
        else {
            log.error('The country name, ' + tabFipsCountry + ', does not match a FIPS Country Name');
            ignore = true;
        }
 
Mark Helpful if it resolves your query and helps in solving your issue.
Regards,
Johns M P

View solution in original post

3 REPLIES 3

Johns_MP
Giga Guru

Hi @Mat Gdowski1 ,

Almost correct

On a first glance i can see that you have misplaced the apostrophe in the third line.

Replace the third line with below:

 tabFipsCountry.addQuery('u_country_name', sourCountryName);

 

try this and see if it fixes the issue.

Also you can try replacing the code as below (This is only for handling if there is no record present) (i had just replaced the '}' )

 

  if (tabFipsCountry.next()){
       
        if (sourCountryName == tabFipsCountry) {
            var varifCountry = tabFipsCountry.u_country_name;
            return varifCountry;  
        }
}
        else {
            log.error('The country name, ' + tabFipsCountry + ', does not match a FIPS Country Name');
            ignore = true;
        }
 
Mark Helpful if it resolves your query and helps in solving your issue.
Regards,
Johns M P

Mat Gdowski1
Tera Contributor

Johns,

 

Thank you for the info and suggestions.  I will try and work on this today and advise if all is good and it works properly and then mark it as the correct answer/helpful.  🙂

 

Again, thank you for your timely response!

Take care,

Mat

Mat Gdowski1
Tera Contributor

Johns,

 

Thank you so much.  Yup, the apostrophe movement fixed it and I went with your suggestion regarding closing off the IF statement before the else.  When I thought about it and looked at it, it made much more sense than how I scripted it originally.  Thank you for that.  🙂

 

Have an awesome day,
Mat