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;
    }        

}              
  • Are you getting any error on screen? – Saumya Ranjan Satapathy May 19 '16 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? – Saumya Ranjan Satapathy May 19 '16 at 15:52
  • This query works --> Select ID, Country__c, Permutations__c from Country_Mapping__c where Permutations__c LIKE '%CAN%' LIMIT 1 – dannycheeko May 19 '16 at 15:55
  • you have declared country as a set. so I believe'String Lookup = '%'+Country+'%';' will be giving you an error – Saumya Ranjan Satapathy May 19 '16 at 15:55
  • Agreed... passed the variable to another field and got this %{USA}%. Where are these { and } coming from? – dannycheeko May 19 '16 at 16:02
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];
  • It is the loop variable... so it can't be null. – Adrian Larson May 19 '16 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. – Saumya Ranjan Satapathy May 19 '16 at 15:43
  • 1
    Ah, only saw it as the loop variable later on. Duplicate variable shouldn't even compile... – Adrian Larson May 19 '16 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.