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
sfdc integrator.ax1790sfdc integrator.ax1790 

remove a record from trigger.new list

Hi,

When i insert 10 records if any of the record is not equal to Utility.AccountRecordType then i need to remove the record from the trigger.new list. 

for(integer i=0;i<Trigger.new.size();i++){
        if(Trigger.new[i].RecordTypeId != Utility.AccountRecordType){
            Trigger.new.remove(i);
        }
    }

Regards
Suresh S
Tony TannousTony Tannous
Hello , 

you can't do that and remove from Trigger.new,

you can do the below :

1- add after insert trigger and put the accounts that have a  recordtype different then Utility.AccountRecordType and delete them ex:

list<Account> listAccDel= new list<Account>();

 for(integer i=0;i<Trigger.new.size();i++)
{
        if(Trigger.new[i].RecordTypeId != Utility.AccountRecordType)
    {
        listAccDel.add(Trigger.new[i]);
      }
   }

delete listAccDel;

Good luck
Vatsal KothariVatsal Kothari
Hi Suresh,

you can refer below code:

trigger removeRecordsOfAccount on Account (after insert) {

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

	for(integer i=0;i<Trigger.new.size();i++){
        if(Trigger.new[i].RecordTypeId != Utility.AccountRecordType){
            delList.add(Trigger.new[i]);
        }
    }
	
	delete delList;
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

Dongzhi Yang 27Dongzhi Yang 27
So the only way to do it is in the after trigger, is there a way to do it in a before trigger?
Santhosh BovurothuSanthosh Bovurothu
Hi Dongzhi Yang,
You can't delete a record before it's been inserted.
Ashton AtlasAshton Atlas
Hi, could anyone can personally assist me. As I have removed the record in the account of latari sambad.com (https://www.winscenes.in/lottery-sambad-old-results/) but due to that our other list get disturbed. How will I recover the old listing and setting. Event a technical person in my office look to this issue but he is unable to fix it.