• Grant Ongstad
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies
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;
            }
   } 
   }
Hello Everyone,
 
A developer created an @future method that set the Opportunity Owner based on a criteria that is stored in a custom object. During lead conversion our code works almost 99% of the time. What is strange is that when it doesn't work, when the user remains the default user, If i clone the opportunity, the Code works.

I guess what I am trying to ask, and this is before I show some of the code is:
Is there any reason that a trigger would not fire on opportunity creation, but would fire when it is cloned? Arent these the same actions?

Best,
Grant
API Challenge 'Bluebeards Grog House' -

Finished this challenge, following every step and I am still getting the following error
'Challenge Not yet complete... here's what's wrong: 
Could not find an account named 'Bluebeards Grog House' created using SoapUI with the Description 'It is better than Blackbeards.'

I downloaded the Soap API, Followed the exact instructions....
User-added image

Has anyone had this problem?
API Challenge 'Bluebeards Grog House' -

Finished this challenge, following every step and I am still getting the following error
'Challenge Not yet complete... here's what's wrong: 
Could not find an account named 'Bluebeards Grog House' created using SoapUI with the Description 'It is better than Blackbeards.'

I downloaded the Soap API, Followed the exact instructions....
User-added image

Has anyone had this problem?
I get this error: Challenge Not yet complete... here's what's wrong: 
The Suggestion custom object does not contain the correct set of custom fields. Tip: check for typos in the field names.

I've followed all steps, went back and rechecked, and then redid the entire challenge. I can't get it to pass. What am I doing wrong? I have included all custom fields and spelled them correctly.