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
woodmanzeewoodmanzee 

Apex Trigger to populate custom field on Contact Object

I need to write an apex trigger that takes information from some of the Contact fields and uses them to generate a value for a custom field. 

 

For example, say I have a custom field called "Area Code." When I create a new contact, after filling out the Mobile field and click save, the trigger should fire and (using apex code i would write) populate the Area Code field with the first three numbers from the Mobile field. 

 

The problem is that the triggers I write don't seem to fire correctly - when the record is saved, nothing happens, or I have to edit the Mobile field and save it again in order for the trigger to work. I don't really need to know how to make an area code field, but I do need to do something similar. How can I make a trigger that fires correctly when a record is created or edited?

Best Answer chosen by Admin (Salesforce Developers) 
nick1505nick1505

Hi,

 

trigger GenerateTextEmail on Contact (before insert, before update)
{
for (Contact objCon : Trigger.new)
{

String areaCode = '';
if (objCon.MobilePhone == null)
{
// can't populate the Area Code field if there is no number saved
}
else
{

String temp = '';
if(objCon.MobilePhone != null)
temp = objCon.MobilePhone;


if (temp == null || temp == '') {}
else if (temp.length() == 9)
{
areaCode = temp.substring(0,3);
}
else
{
areaCode = temp.substring(1,4);
}


}
if(areaCode != null && areaCode != '')
objCon.Area_Code__c = areaCode;

}
}

 

You can refer to the above code. It fires on both before insert and before update of Contact and updates the Area_Code__c field.

Also never use query inside a for loop you will hit Limitations.

I havent changed your Areacode logic assuming that it is proper.

 

Let me know if it works for you!

 

All Answers

JBabuJBabu

Can you share the code?

nick1505nick1505

Hi

 

The custon fiield 'Area Code ' is on which object ?

 

If it exists on the same Contact object you can use 'before insert' and 'before update event'. It it is existing on a different object you can use 'after insert' and 'after update' event.

 

If you can share your code, and what is the exact issue you are facing, will make it easy to provide you a solution.

 

Let me know if it works for you!

 

woodmanzeewoodmanzee

Yes, all fields are on the contact Object. 

So using the Area Code example, my code is like this:

 

trigger GenerateTextEmail on Contact (before update, before insert) {
    
    for (Contact c : Trigger.new) 
    {
        
        if (c.MobilePhone == null) {
		// can't populate the Area Code field if there is no number saved
        } else {

            List<Contact> toUpdate = [SELECT j.MobilePhone, Area_Code__c FROM Contact j WHERE j.Id =: c.Id FOR UPDATE];
	// should only return the one contact
            
	String areaCode = '';
            for (Contact con : toUpdate)
            {
            
                String temp = String.valueOf(c.MobilePhone);
            
                if (temp == null) {}
                else if (temp.length() == 9) {
                    areaCode += temp.substring(0,3);
                } else {
                    areaCode += temp.substring(1,4);
                }
                      
           }
	
	// use the new value to populate the field
	c.Area_Code__c = areaCode;
          
        } 
    }
}

 

 So I want to take the Mobile Phone field and use it to populate the Area Code field - all while the record is being created. It should also fire on any modification of the Mobile Phone Field.

 

Thanks

nick1505nick1505

Hi,

 

trigger GenerateTextEmail on Contact (before insert, before update)
{
for (Contact objCon : Trigger.new)
{

String areaCode = '';
if (objCon.MobilePhone == null)
{
// can't populate the Area Code field if there is no number saved
}
else
{

String temp = '';
if(objCon.MobilePhone != null)
temp = objCon.MobilePhone;


if (temp == null || temp == '') {}
else if (temp.length() == 9)
{
areaCode = temp.substring(0,3);
}
else
{
areaCode = temp.substring(1,4);
}


}
if(areaCode != null && areaCode != '')
objCon.Area_Code__c = areaCode;

}
}

 

You can refer to the above code. It fires on both before insert and before update of Contact and updates the Area_Code__c field.

Also never use query inside a for loop you will hit Limitations.

I havent changed your Areacode logic assuming that it is proper.

 

Let me know if it works for you!

 

This was selected as the best answer
woodmanzeewoodmanzee

Thanks! That worked great!