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 

How do I get a field to populate with text based on the value of another field?

I have a custom object named County that houses all U.S. data for zipcodes and counties.

I have a County custom field on my contacts.

I need help creating a simple trigger that will update the County field on my contact.  I want to match the zipcode on the contact with the zipcode on the county object and return the value of the county.

Screen shots are below:

Contact Image



User-added image
Madhura BMadhura B

Hi Dea

Try this

countyTrigger on Contact (after Insert, after Update)
{
    Contact con = trigger.new(); 
    County__c listCounty = [select county__c from county_Object__c where ZipCode__c = con.ZipCode__c];
if(listCounty!=null)
    {
       con.County__c = listCounty.county__c;
     }



*This is just an example. Please replace appropriate APIs 

If this resolves your issue, please mark this as an answer

 

Dea Simon 3Dea Simon 3
Thanks Madhura - I am getting the following error message - not sure what it means:

User-added image
Madhura BMadhura B

Sorry Dean,
You cannot update fields on after trigger. Ooops! My Bad. Here's the updated code

trigger countyTrigger on Contact (before Insert, before Update)
{
    for (Contact con : trigger.New)
    {
      County__c listCounty = [select county__c from county__c where ZipCode__c =: con.ZipCode__c];
        if(listCounty!=null)
            {
               system.debug('------'+listCounty);
               con.County__c = listCounty.county__c;
             }
    }
}



Pleae mark this as the answer if it resolves your issue

Regards,
Madhura