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
Dea SimonDea Simon 

Zipcode lookup provides County detail

Hey all - I am an admin new to learning code and need a little help.  I have a custom object called Counties.  In this custom object are records that contain every zipcode, city, county, and state in the U.S.. 

I also have a "County" field on my contact object.

I would like to auto update the County field on my contact object automatically based on its zipcode.  I want to accomplish this by having the contact zipcode lookup the matching zipcode in the counties object and then return the actual County value onto the contact object.

Make sense?

I'm pretty sure I need an apex trigger for this but I haven't learned how to make these yet - can anyone give me some help?  Here are some screen shots of both my contact object and my counties custom object:

User-added image



User-added image
manhnt.bkitmanhnt.bkit
This is an example for you :

//TODO: Make sure you add __C after your custom fields.

trigger TriggerUpdateContact on Contact(before insert, before update) { 	
	// I just handle in case user manual enter OR edit. It will easy for you understand. 
	// Another case when user use bulk update, insert will be more complex .
	
	if(Trigger.new.size() == 1 ){
		List<Counties__c> lstCounties = new List<Counties__c>();
		
		for(Contact c : Trigger.new){
			lstCounties =[select id, zipcode, city, county, state  
				where zipcode =: c.MailingPostalCode OR zipcode =:c.OtherPostalCode];			
		
		}
		
		if(lstCounties.size() > 0){
			Trigger.new[0].County__c = lstCounties[0].zipcode +  ', ' lstCounties[0].county +  ', ' 
				+	lstCounties[0].city 
				+  ', ' + lstCounties[0].state;	//TODO: add more your fields here.		
			
			
		}
		
	}
	
}



Vinit_KumarVinit_Kumar
Dea Simon,

You should be using a Map where Zipcode is Key and County is value so based upon Contact Zipcode.

You should then pull up the county from  County object and populate on Contact object.
Dea SimonDea Simon
OK - Thanks to you both.  To be clear, the code listed above will look at the zipcode on the contact object - find it in the counties object and then return the value of the County into the contact object?  I will give it a try and let you know how it turns out. 
Vinit_KumarVinit_Kumar
Dea Simon,

Do something like below :-

// querying all the name and Zipcode from County object
List<County__c> cntList = [select name,Zipcode__c from County__c];

Map<String,String> zipcodeMap = new Map<string,String>();

//populating the Map
for(County__c c : cntList)
{
       zipcodeMap.put(c.name,c,Zipcode__c);
}

If this helps,please mark it as best answer to help others :)
Dea SimonDea Simon
Hey guys - OK, I got them to move to enterprise edition - I am going to try one of these today and will let you know how it works out - thanks again for the help!
Dea SimonDea Simon
@Vinit_Kumar - am I supposed to add anything to your code in the string area?  Not being familiar with APEX I really don't understand what all of the code means.  I am getting the following error message:

User-added image
manhnt.bkitmanhnt.bkit
Hi Dea, it must be ordered like this:
trigger TriggerUpdateContact on Contact(before insert, before update) { 

  // querying all the name and Zipcode from County object
  List<County__c> cntList = [select name,Zipcode__c from County__c];

  Map<String,String> zipcodeMap = new Map<string,String>();

  //populating the Map
  for(County__c c : cntList)
  {
       zipcodeMap.put(c.name,c,Zipcode__c);
  }

}



Dea SimonDea Simon
Now its telling me the zipcode__c does not exist.  Can one of you please walk me through what each of these lines mean?  Since I don't understand code I'm having a hard time figuring out where the problem is - I really appreciate it!