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
AswathAswath 

After Insert Trigger is not firing for Bulk inserting?

Hi All,

 

I created one trigger (after insert) for my custom object. If i am inserting single record the trigger is firing, but when i am inserting bulk records using DataLoader the trigger is firing only onec? Plz any one help.....

 

Thanks,

Aswath.

SteveBowerSteveBower

The trigger is supposed to fire only once, however Trigger.new (or .old) will be an array with multiple entries.  So, you have to write your trigger to handle all the entries that there might be in the arrays.

 

If you already knew that, and have taken that into account, post your code.

 

Best, Steve.

AswathAswath

 

Message Edited by Aswath on 04-05-2009 08:32 PM
SteveBowerSteveBower

No matter how many items are in Trigger.new, you will only be setting and resetting, and resetting, etc. myatd[0] and myatd[1].

 

So, Adet and svpln will only ever contain one record.

 

I'd say just make them lists and change your query to "in", however your loops would be creating MxN payment records which may or may not be what you intend.

 

Best, Steve.
AswathAswath
I changed my query to "in" but still my trigger is firing only once? here is my code.

 

 

Message Edited by Aswath on 04-05-2009 08:36 PM
BESV2BESV2

Hi Aswath, I guess the problem might be in this line

 

for (ClinicalTrial__Subjects_Visit__c svp:Trigger.new)
{
myatd[0]=svp.Subject_Visit_Template_del__c;
myatd[1]=svp.Id;
}

 

which will alwaays give you only two records, that to you are using myatd in both the queries. check it once. if your business goal still the same. then

you migt want to use Trigger ' Map '

 

On the bulk if the forloop consists of different items then you might want to use it like myatd[i] =...; i++;

 

 

Message Edited by BESV2 on 04-01-2009 11:23 PM
AswathAswath

Hi,

 

Thanks for reply, the problem is trigger is not firing every time when a record is inserting. It is firing only for the last record?

BESV2BESV2

Trigger will only fire once. Whatever the bulk handling we have to handle inside the trigger using Map and In condintion in queries. i will give you an example see it might be useful

Map<Id, Contact> contactMap = new Map<Id, Contact>();
          
            for(Integer i = 0; i < Trigger.new.size(); i++)
            {
                
                    contactMap.put(Trigger.new[i].Id, Trigger.new[i]);
                  
            }
           
            List<Contact> lstContact = [Select Name, Id From Contact where Id in : contactMap.keyset()];

            for(Integer i=0; i < lstContact.size(); i++)
            {

                    //do changes
             }           

AswathAswath

Hi,

 

Thanks for ur reply, ur code is working fine but its creating duplicates. Here i will explain the criteria :

 


 

Thanks,

Aswath.

 

Message Edited by Aswath on 04-05-2009 08:36 PM
Srinivas_V2Srinivas_V2

 Use this to prevent duplicates

 

Map<Id, Contact> contactMap = new Map<Id, Contact>();
          
            for(Integer i = 0; i < Trigger.new.size(); i++)
            {
                     if(!contactMap.containsKey(Trigger.new[i].Id))
                     {

                                contactMap.put(Trigger.new[i].Id, Trigger.new[i]);


                     }                 
            }

 

      
            List<Contact> lstContact = [Select Name, Id From Contact where Id in : contactMap.keyset()];

            for(Integer i=0; i < lstContact.size(); i++)
            {

                    //do changes
             } 

 

BESv2 and Srinivas_v2 both are me only:)     

Message Edited by Srinivas_V2 on 04-02-2009 02:14 AM
AswathAswath

Hi Srinivas,

 

Thanks for ur reply still its not working, for 1st subject visit 5 payments and for 2nd subject visit 5 payments total 10 payments created.

Srinivas_V2Srinivas_V2
can u post ur current code. or you can reach me by gtalk. srinivas.d2s@gmail.com
AswathAswath
Message Edited by Aswath on 04-05-2009 08:31 PM
Srinivas_V2Srinivas_V2
deleted.
Message Edited by Srinivas_V2 on 04-06-2009 01:28 AM
AswathAswath

Hi Srinivas,

 

Thanks for ur reply, i understood ur logic, now i am getting an error in DataLoader 'SObject row doesn't allow errors'. I will check it & get back to u soon.....

 

Thanks,

Aswath.