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
Grant OngstadGrant Ongstad 

Apex Trigger Help - Does not work with multiple records at once

Hello guys,

Now, full disclaimer. The following trigger is be no means best practice, but I am trying to fix some of the bugs it has.
Basically, what this is suppose to do is when a task comes in from Pardot, the Trigger looks for an existing opportunity for that account. If it finds one, the task gets associated with it. 
If it cannot find an Opportunity, a new Opportunity is created and that task is associated to it.

The Current code works as intended in one off manual task creations, but fails when i test it for multiple records in data loader (It creates a new opportunity even though an account already has one)

Any help would be greatly Appreciated!

trigger taskOpportunity on Task (after insert) {



    
    //Create List to Store Tasks from Pardot
   
    List<Task> tasksFromPardot = new List <task>();
   task [] taskToUpdate = new task[]{};
   
    for(Task t : Trigger.new){
        if(t.is_pardot_task__c == TRUE){
            tasksFromPardot.add(t);
        }
    }
    
        //Update Task if it has Existing Opportunities
        
          List <opportunity> hasOpportunity = new list <Opportunity>();
          
        for(task t2 : tasksFromPardot){
            Opportunity [] existingOpportunity = [Select ID,Ownerid,accountid,Event_code__c,event__c from opportunity where event_code__c = :t2.Event_Code__c 
                                                              AND Accountid = :t2.AccountId AND stageName not in ('Lost','Sold')];
              if(existingOpportunity.size() > 0){                                              
                    hasOpportunity.add(existingOpportunity[0]);
            }
                        
            if(hasOpportunity.size() > 0) {
                
                   
                for(Opportunity O : hasOpportunity){
                    for( Task t1 : [Select ID,Event_code__c,AccountID,Campaign_name__c From Task where Event_code__c = :o.event_code__c and Accountid =: o.accountid]){
                                  
                Campaign CampaignSD = [select id,short_description__c,event__c from campaign where short_description__c = 
                :t2.Campaign_name__c AND Event__c =: o.Event__C];
                  
                Task ta = new task(id = t1.Id);
                                        ta.OwnerId = o.ownerid;
                                        ta.whatid = o.ID;
                                        ta.Subject = t1.Campaign_name__c;
                taskToUpdate.add(ta);
                }
                }
                    
                    update taskToUpdate;
                         
            }Else{
                //If Existing Opportunity does not exist, Create a New Opportunity
              
                list<Opportunity> newOpps = new List <Opportunity>();
                
                for(task taskup :[Select ID,Event_code__c,Whoid,campaign_name__c From Task Where ID in: TasksFromPardot AND ID not in: TasktoUpdate]){
                        id EventID =[Select id from Event__c where Event_code__c = :taskup.Event_Code__c].id;
                        id AccountID = [select accountid from contact where ID = :taskup.WhoId].accountid;
                        id CampaignID = [select id from campaign where short_description__c = :taskup.Campaign_name__c AND Event__c = :EventID].id;
                    
                    opportunity newOpp = new opportunity();
                    newOpp.accountid = Accountid;
                    newOpp.Name = 'x';
                    newOpp.CloseDate = date.today().addDays(1);
                    newOpp.Event__c = EventID;
                    newOpp.CampaignId = CampaignID;
                    newOpp.LeadSource = 'Website Lead';
                    newOpp.StageName = 'New';
                    newOpp.Amount = 0;
                    newOpp.Pardot_Task_ID__c = taskup.id;
                    newOpps.add(newOpp);
                    
               
                }
              
                insert newOpps;
               
                  
               
                //Update Tasks to associate with Newly Created Opportunities
                //
            
                task [] newTaskToUpdate = new task[]{};
                    for(task t4 : TasksFromPardot){
                    
                        Opportunity upOpp2 = [Select ID,OwnerID,Pardot_task_id__c From Opportunity where Pardot_Task_id__c = :t4.id LIMIT 1];
                        
                     Task tak = new task(ID = t4.ID);               
                            tak.whatid = upOpp2.id;
                            tak.OwnerId = upOpp2.ownerid;
                    //if(upopp2.Pardot_task_id__c ==Tak.id){        
                            newTaskToUpdate.add(tak);
                            
            }   
                Update newtaskToUpdate;
            }
   } 
   }