• nag1.348745048492293E12
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Hi,

 

I am creating an apex trigger which triggers auto population of fields in Account with values based on a particular record type name. However, on saving the trigger it gives no errors neither while testing it on Salesforce. However, ofcourse, the records dont get updated with the values on condition of matching record type. Let me know where am I going wrong in the code below. Please suggest corrective actions accordingly.

 

Thanks,

 

Vimal

 

trigger AccRecTypeMatchAutoPopulateFieldValues on Account (before insert, before update) 
{
 if(Trigger.isInsert)
 {
  Account[] newAccs = Trigger.new;
  RecordType rt1 = [select name from RecordType where SobjectType = 'Account' limit 1];
  if(rt1.name == 'rec_type1') 
  {
   for (Account a:newAccs)
   {
    a.Website = 'http://www.tcs.com';
    a.BillingStreet = 'Yantra Park';
    a.BillingCity = 'Mumbai';
    a.BillingState = 'Maharashtra';
    a.BillingCountry = 'India';
    a.Fax = '223120';
    a.Phone = '2230175';
   }
   insert newAccs;
  }
 }
 if(Trigger.isUpdate)
 {
  Account[] oldAccs = Trigger.new;
  RecordType rt2 = [select name from RecordType where SobjectType = 'Account' limit 1];
   //if (a.RecordType.Name == 'rec_type1') 
   //if(a.Industry == 'Technology')
  if(rt2.name == 'rec_type1')
  {
  for (Account a:oldAccs)
   {
    a.Website = 'http://www.tcs.com';
    a.BillingStreet = 'Yantra Park';
    a.BillingCity = 'Mumbai';
    a.BillingState = 'Maharashtra';
    a.BillingCountry = 'India';
    a.Fax = '223120';
    a.Phone = '2230175';
   }
   update oldAccs;
  }
 }
}