0
votes

Hi i have written the trigger and apex class as below but this does not update the existing records in Account object, I am trying to update the website for the Account Amazon

trigger AccountTrigger on Account (After Insert) {

if(Trigger.isAfter==true && Trigger.isInsert==true){

updateAmazon.check();


}
}


public class updateAmazon{

public static void Check(){


List<Account> Listrecord=new List<Account>();

Listrecord=[SELECT name,website from account where name='Amazon'];


List<Account> UpdateList=new List<Account>();

for(Account acc:Listrecord){

acc.website='www.amazon.com';

UpdateList.add(acc);

}

update UpdateList;

}

}

Please help, unless i create a new record the existing records do not get updated.

Thanks

1
Can you be more specific on your goal? I'm not sure this logic should exist in a trigger. But as for your statement "unless i create a new record the existing records do not get updated" is correct because you've only written this in the after insert trigger as per your class definition "trigger AccountTrigger on Account (After Insert)" so it will only fire when a new account is created.Psymn
Thanks how do i fire it for already existing records , i want to update the existing records only without creating a new record .eg i already have 10 Account records named Amazon and want to update the website info for these records without adding a new recorduser12178514

1 Answers

0
votes

From your comment, I'm not sure this should be a trigger unless you are expecting more accounts with the name Amazon to be inserted if so, Then you should do this is two steps.

Step 1.

  • Open Developer console
  • Open "Execute Anonymous Apex"
  • Paste the code from your check method into the window and execute it

This will update all your existing amazon accounts

Step 2. - Modify your trigger

trigger AccountTrigger on Account (before Insert) {
    for(Account a: Trigger.New)
    {
       if(a.Name == 'Amazon')
       {
          a.website='www.amazon.com'
       }
    }
}

This will ensure that any "amazon" accounts created will get the website set. You should create this trigger first and then run the anonymous apex.