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
Anu-SFDCAnu-SFDC 

List of Records inserting one by one?

Hi,

 

I have a strange problem..

 

I have a class where I'm inserting the records to an object from that class.

Lets say the object is Data__c..

 

I have all records in one list variable. Suppose i have 5 records in that list..

 

so., if i wrote

 insert data;(data is a list variable) all 5 records are inserted successfuly.. But I have a trigger on that object(before insert)..

 

For five records trigger is firing only once.. That trigger is firing only for the last record..

 

How to do an insert in which for all the records trigger will fire one by one..

 

Thanks

Anu

Navatar_DbSupNavatar_DbSup

Hi,

I think maybe you have not written your trigger for bulk handling, you have passed the index id as harcoded. Try to search for your trigger name inside the log of trigger you will find that it is called 5 times .Even if you find the issue then please posts your trigger code.

 

you can try the below code sample

trigger chkfor on Account (before insert)
{
//account aa=trigger.new[0];
for(Account aa:trigger.new)
{
if(aa.Active__c=='No')
{
aa.addError('not allow to insert');

}
}
}

 

Active__c is a piklist and contain 'Yes' and 'No' values

 

system Debug code

List<account>a1=new List<account>();
for(integer i=0;i<5;i++)
{
account aa=new account(name='aaa1' ,Active__c='No');
a1.add(aa);
}
insert a1;
system.debug('@@@@@@@@'+a1);

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

Anu-SFDCAnu-SFDC

Hi,

 

Thanks for Reply.

 

My trigger will work for only one record.. each time the record is posting..

 

But what I need is,, this insertion need to happen one after other...(Better if we put a delay in each insertion of a record. but how to put a delay in time?).

 

so, that my trigger will work one after other..

 

let me know if u have any idea?

 

Thank

Anu

bob_buzzardbob_buzzard

Triggers can receive up to 200 records at once - you should write your trigger to be able to handle that many.

 

Trigger.new is a list that contains all records from the insert - is there any reason why you can't simply process that in a for loop rather than just the first element?

 

 

Anu-SFDCAnu-SFDC

But the problem is that trigger is from another package.. which is segregating the values in Data__c object to other objects one by one..

 

I just want to put  a gap in the records atleast.

 

Thanks

Anu

bob_buzzardbob_buzzard

I'm not sure I understand what you mean here - if the trigger from the other package is writing the records one at a time, I'd expect you to receive them one at a time.  However, as your trigger is only firing once, that implies that your trigger is receiving all the records in a single batch.

Anu-SFDCAnu-SFDC

as your trigger is only firing once, that implies that your trigger is receiving all the records in a single batch.

 

 

yes.. but Mine is class.. when i insert records.. that trigger is firing... I want to fire that trigger one after the other insertion..

 

 

bob_buzzardbob_buzzard

I'm still not getting it I'm afraid - if you have a trigger on the insertion of a record, that will fire regardless of where the record is inserted from.  Thus if the other trigger inserts a record, your trigger will still fire. 

Anu-SFDCAnu-SFDC

I have a trigger on the before insertion of a record.. i'm inserting the records from a list.. Without changing my trigger code during the insertion only can I make the insertion one after the other..?

 

Not like batch (since they are stored in list)..

bob_buzzardbob_buzzard

One way would be to iterate the list. Assuming your list is called 'myList' and it contains 'Data__c' instances, you can do the following:

 

for (Data__c obj : myList)
{
   insert obj;
}

 This won't scale for larger inserts though, as there is a limit of 150 DML statements per transaction.

raj_sfdccraj_sfdcc
iam also facing the same problem .Can anybody help on this?
Thanks in advance.