Monday, June 11, 2012

POPULATE CUSTOM FIELD:

POPULATE CUSTOM FIELD:
This is a simple trigger populate custom field on contact from mobile number standard field.Area code is your custom field should be populate with part of the mobile number.
Apex Trigger:
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() == 10)
{
areaCode = temp.substring(0,3);
}
else
{
areaCode = temp.substring(1,4);
}


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

}
}

No comments:

Post a Comment