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
bheemudu neelibheemudu neeli 

Need a help on trigger about to Insert and update

Hi All, 
Please help to update the trigger for old value, please  where i'm doing wrong.
here is the code
trigger BulkifyTri1 on Account (before insert, before update){
       set<Id> accIds = new set<Id>();
       list<Account> UpdateAcc = new list<Account>();
       list<Account> accst = new list<Account>();
       list<Account> InsertAcc = new list<Account>();
     for ( Account a : trigger.new){
        if ( a.AccountNumber != NULL )
            a.Type = 'Other';
            InsertAcc.add(a);
            }
            insert InsertAcc;
        
    accst = [select id, name,AccountNumber,Type from account where Id in :accIds];  
    for(Account ac:accst )
    {
    if (ac.AccountNumber != NULL ){
    ac.Type = 'Prospect';
    UpdateAcc.add(ac);
    }
    }
    
    if(UpdateAcc.size()>0)
        {
            update UpdateAcc;
        }
      
  }
    

With Regards,
Bheemudu Neeli
Best Answer chosen by bheemudu neeli
sfdcMonkey.comsfdcMonkey.com
and you dont need to use insert/updtae dml also list ,set because before insert and before update trigger have no need to do it
write your trigger like that
trigger BulkifyTri1 on Account (before insert, before update){
       
     if(trigger.isInsert){
     for ( Account a : trigger.new){
        if ( a.AccountNumber != NULL )
            a.Type = 'Other';
            }
          }
          if(trigger.isUpdate){
    for(Account ac:trigger.new)
    {
    if (ac.AccountNumber != NULL ){
    ac.Type = 'Prospect';
    }
    }
    }  
  }
Thanks
i hop it helps you
Please Mark it best answer if it helps you :)
 

All Answers

sfdcMonkey.comsfdcMonkey.com
hi Bheemudu Neeli
in your trigger
accst = [select id, name,AccountNumber,Type from account where Id in :accIds];  

this id list is blank :)
add account id to  set<Id> accIds = new set<Id>();
Thanks
sfdcMonkey.comsfdcMonkey.com
and you dont need to use insert/updtae dml also list ,set because before insert and before update trigger have no need to do it
write your trigger like that
trigger BulkifyTri1 on Account (before insert, before update){
       
     if(trigger.isInsert){
     for ( Account a : trigger.new){
        if ( a.AccountNumber != NULL )
            a.Type = 'Other';
            }
          }
          if(trigger.isUpdate){
    for(Account ac:trigger.new)
    {
    if (ac.AccountNumber != NULL ){
    ac.Type = 'Prospect';
    }
    }
    }  
  }
Thanks
i hop it helps you
Please Mark it best answer if it helps you :)
 
This was selected as the best answer
bheemudu neelibheemudu neeli
Thanks Piyush,
Is it work for mass Insert and Update?
can you please help me to make it Bulkify, code is remain same.

Thanks and Regards,
Bheemudu
sfdcMonkey.comsfdcMonkey.com
yes it bulkify code :) try this
sfdcMonkey.comsfdcMonkey.com
if any issue with it you can ask here.
mark it best answerif it helps you :)
bheemudu neelibheemudu neeli
Thank you, working as expected...