0

EDIT - I tried a different approach and got some traction...

Trying to match a user-entered Country value to a table of 'permutations' to then correct and update the country name, as well as link to the "Country Mapping" object record for that country.

This is the logic and process:

IF Country = CA  map against country code BillingCountry updated to Canada Country_Mapping__c = Canada (record on Country Mapping object)

If Country = Can OR Canadia  map against permutations CillingCountry updated to Canada Country_Mapping__c = Canada

What am I missing, as nothing is happening upon execute for the permutations.

trigger AccountSetCountryMap on Account (before insert, before update) {

String UpperCountry;
String Lookup;

Set<String> Country= new Set<String>();
for(Account record : Trigger.new){
    UpperCountry = record.BillingCountry.toUpperCase();
    Lookup = '%'+record.BillingCountry+'%';
    Country.add(UpperCountry);
   }


Map<String,Country_Mapping__c> CountryMap = new Map<String,Country_Mapping__c>();
    for(Country_Mapping__c CM : [Select ID, Country__c, Permutations__c from     Country_Mapping__c where Permutations__c LIKE :Lookup])
    CountryMap.put(CM.Permutations__c, CM);  

for(Account record : Trigger.new) {       
            UpperCountry = record.BillingCountry.toUpperCase();
            if(record.BillingCountry!= null && CountryMap.containsKey(UpperCountry))
                record.BillingCountry = CountryMap.get(UpperCountry).Country__c;
    }        

}              
8
  • Are you getting any error on screen? May 19, 2016 at 13:27
  • Can you tell where is your problem? Is this query 'Select ID, Country__c, Permutations__c from Country_Mapping__c where Permutations__c LIKE '%can%' LIMIT 1' returns a result when run in dev console? May 19, 2016 at 15:52
  • This query works --> Select ID, Country__c, Permutations__c from Country_Mapping__c where Permutations__c LIKE '%CAN%' LIMIT 1 May 19, 2016 at 15:55
  • you have declared country as a set. so I believe'String Lookup = '%'+Country+'%';' will be giving you an error May 19, 2016 at 15:55
  • Agreed... passed the variable to another field and got this %{USA}%. Where are these { and } coming from? May 19, 2016 at 16:02

1 Answer 1

0

Acc variable is null

Account Acc;

Country_Mapping__c[] CMap = [Select ID, Country__c, Country_Code__c from Country_Mapping__c 
                             where Country_Code__c like :Acc.BillingCountry LIMIT 1];
3
  • It is the loop variable... so it can't be null.
    – Adrian Larson
    May 19, 2016 at 13:48
  • I couldn't get you @AdrianLarson. Can you please tell me how this will get a value? What I saw is a declaration but no initialization in line 3 and used in line 4 just after trigger. May 19, 2016 at 15:43
  • 1
    Ah, only saw it as the loop variable later on. Duplicate variable shouldn't even compile...
    – Adrian Larson
    May 19, 2016 at 15:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .