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
champ_vimalchamp_vimal 

Auto Population of fields based on Record type name

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;
  }
 }
}
Best Answer chosen by Admin (Salesforce Developers) 
champ_vimalchamp_vimal

Can anyone help me with this?

 

Thanks

 

Vimal

All Answers

champ_vimalchamp_vimal

Can anyone help me with this?

 

Thanks

 

Vimal

This was selected as the best answer
nag1.348745048492293E12nag1.348745048492293E12

Before insert and update we can't perform DML operations in above mention code.