function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mario CMario C 

custom settings vlookup

Hi All,

I need to write a trigger on contact which will update the phone number when the the country code is not included in the number. 

I thought that I could achieve this through Custom settings. I've got a custom settings call Country_letter_codes__c already setup. it includes 3 fields:
Country__c (name of the country in full)
Letter_Code__c (ISO code for each country - i.e. United Kingdom = GB)
Phone_Code__c (I've added this for my task and I populated it with all the corresponding phone codes i.e. Country__c = United Kingdom, Phone_code__c  = +44...) 

This cs is currently used to update the MailingCountry with a corresponding Letter_Code (i.e. from United Kingdom to GB), but I thought about using it for updating the contact phone too, and that is why I added a new field for Phone_Code__c. 

Considering that I actually know how to write the code to update the number itself, I actually need to understand how to run a sort of vertical lookup on Letter_Code. Basically I need to find the contact MailingCountry (which is actually a letter_code) in the cs.letter_code and fetch the corresponding phone code, so that I can then append it to he phone number wher required.  
Best Answer chosen by Mario C
Raj VakatiRaj Vakati
try this one 
 
trigger contactFromAccount on Contact (before insert) {
	List<Country_letter_codes__c > cs =[Select Id,Country__c ,Letter_Code__c ,Phone_Code__c  from Country_letter_codes__c ] ;
	Map<String ,Country_letter_codes__c> cntCode = new Map<String ,Country_letter_codes__c>() ;
	for(Country_letter_codes__c c :cs){
		cntCode.put(c.Country__c ,c);
	}
      
	  for(Contact c : Trigger.new){
		  If(c.MailingCountry!=null){
			 Country_letter_codes__c codes =  cntCode.get(c.MailingCountry);
			 c.Phone_Code__c = codes.Phone_Code__c;
		  }
	  }
}

 

All Answers

Raj VakatiRaj Vakati
try this one 
 
trigger contactFromAccount on Contact (before insert) {
	List<Country_letter_codes__c > cs =[Select Id,Country__c ,Letter_Code__c ,Phone_Code__c  from Country_letter_codes__c ] ;
	Map<String ,Country_letter_codes__c> cntCode = new Map<String ,Country_letter_codes__c>() ;
	for(Country_letter_codes__c c :cs){
		cntCode.put(c.Country__c ,c);
	}
      
	  for(Contact c : Trigger.new){
		  If(c.MailingCountry!=null){
			 Country_letter_codes__c codes =  cntCode.get(c.MailingCountry);
			 c.Phone_Code__c = codes.Phone_Code__c;
		  }
	  }
}

 
This was selected as the best answer
Mario CMario C
Thanks Raj.

I eventualkly used the following:
trigger contactFromAccount on Contact (before insert,before update) {
	
    List <Country_letter_codes__c > cs = [Select Id,Country__c ,Letter_Code__c ,Phone_Code__c  from Country_letter_codes__c ];
	
    Map <String, Country_letter_codes__c> cntCode = new Map<String ,Country_letter_codes__c>() ;
	
    for(Country_letter_codes__c c :cs){
        cntCode.put(c.Country__c,c);
    }
      
	  for(Contact c : Trigger.new){
		  If(c.Country_USE_ME__c!=null && c.Phone !=null){
              String phNumber = c.Phone;
             
              //if number starts with 00, replace 00 with +
              If(phNumber.startswith('00')){		
               phNumber = phNumber.right(phNumber.length()-2);
               phNumber = '+' + phNumber;
               c.Phone =  phNumber;
              }
                     
			  //do not update if the number starts with +
       If(!phNumber.startswith('+')){
             Country_letter_codes__c codes =  cntCode.get(c.Country_USE_ME__c);
			       String compPhone = '+'+ codes.Phone_Code__c + phNumber;
             c.Phone =  compPhone;}
              }
		  }
	  }

I am now creating the test class but I only get 86% coverage with the following:
@isTest
private class PhoneCorrectorTest {
	
	@isTest static void createContactA() {
		Contact c = new Contact();
		c.LastName = 'ConteA';
		c.Country_USE_ME__c = 'Italy';
		c.MailingCountry = 'IT';
		c.LeadSource = 'Email';
		c.Email = 'test@teste.com';
		c.Source_Type__c = 'Salesforce';
		c.Newsletter__c = 'Yes';
		String phoneN = '0044'; 
		c.Phone = phoneN;
		insert c;

	}
		@isTest static void createContactB() {
		Contact x = new Contact();
		x.LastName = 'ConteB';
		x.Country_USE_ME__c = 'Italy';
		x.MailingCountry = 'IT';
		x.LeadSource = 'Email';
		x.Email = 'test@teste.com';
		x.Source_Type__c = 'Salesforce';
		x.Newsletter__c = 'Yes';
		String phoneN = '+44'; 
		x.Phone = phoneN;
		insert x;
		
	}

		@isTest static void createContactC() {
		Contact z = new Contact();
		z.LastName = 'ConteB';
		z.Country_USE_ME__c = 'Italy';
		z.MailingCountry = 'IT';
		z.LeadSource = 'Email';
		z.Email = 'test@teste.com';
		z.Source_Type__c = 'Salesforce';
		z.Newsletter__c = 'Yes';
		String phoneN = '044'; 
		z.Phone = phoneN;
		insert z;
		
	}


}
The code coverage do not cover this line:
cntCode.put(c.Country__c,c);
and this line:
c.Phone =  compPhone;}

I'd like to get to 100% code coverage if possibile; can you help me?
 
Raj VakatiRaj Vakati
You need to insert Country_letter_codes__c ( Your custom setting into the test class )
Mario CMario C
got it...here is the final test class with 100% coverage - happy me! :)
 
@isTest
private class PhoneCorrectorTest {
	
	@isTest static void createContactA() {
		Country_letter_codes__c iT = new Country_letter_codes__c();
		iT.Name = 'Italy';
		iT.Letter_Code__c = 'IT';
		iT.Country__c = 'Italy';
		iT.Phone_Code__c = '39';
		insert iT;

		Contact c = new Contact();
		c.LastName = 'ConteA';
		c.Country_USE_ME__c = 'Italy';
		c.MailingCountry = 'IT';
		c.LeadSource = 'Email';
		c.Email = 'test@teste.com';
		c.Source_Type__c = 'Salesforce';
		c.Newsletter__c = 'Yes';
		String phoneN = '0044'; 
		c.Phone = phoneN;
		insert c;

	}
		@isTest static void createContactB() {
		Country_letter_codes__c iT = new Country_letter_codes__c();
		iT.Name = 'Italy';
		iT.Letter_Code__c = 'IT';
		iT.Country__c = 'Italy';
		iT.Phone_Code__c = '39';
		insert iT;

		Contact x = new Contact();
		x.LastName = 'ConteB';
		x.Country_USE_ME__c = 'Italy';
		x.MailingCountry = 'IT';
		x.LeadSource = 'Email';
		x.Email = 'test@teste.com';
		x.Source_Type__c = 'Salesforce';
		x.Newsletter__c = 'Yes';
		String phoneN = '+44'; 
		x.Phone = phoneN;
		insert x;
		
	}

		@isTest static void createContactC() {
		Country_letter_codes__c iT = new Country_letter_codes__c();
		iT.Name = 'Italy';
		iT.Letter_Code__c = 'IT';
		iT.Country__c = 'Italy';
		iT.Phone_Code__c = '39';
		insert iT;

		Contact z = new Contact();
		z.LastName = 'ConteB';
		z.Country_USE_ME__c = 'Italy';
		z.MailingCountry = 'IT';
		z.LeadSource = 'Email';
		z.Email = 'test@teste.com';
		z.Source_Type__c = 'Salesforce';
		z.Newsletter__c = 'Yes';
		String phoneN = '044'; 
		z.Phone = phoneN;
		insert z;
		
	}


}