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
SpunkySpunky 

Trigger that updates oppportunity with related task information

Hi there.

 

I have a trigger that works almost perfectly but I need to change the behavior and I'm at a loss how to.

 

Behavior

Updates Opportunity field "Next Task Date" with the task due date

Updates Opportunity field "Next Task Subject" with the task subject

Triggered when

A new task is created or an existing open task is updated

ex. if there are 2 tasks , one due 1/1/2013 and another due 1/1/2014

if either one of these is updated or a new task is created, the opportunity fields are updated

Challenge

I need to limit the update so that it only pulls info from the next closest task due date

 

How do I change the trigger so that it achieves this? I would really appreciate some coding help.

 

Thanks  in advance. Please see code below.

 

trigger NextTaskInfo on Task (after insert, after update) {
//this is the trigger that updates next step and next step date on opportunity
  if(Trigger.new.size() == 1 ) {
    Task tk = Trigger.new.get(0);
      if (tk.WhatId != null && String.valueOf(tk.WhatId).startsWith('006')) { 
        Opportunity opp = [select OwnerId,NextTaskSubject__c,Next_Task_Date__c from Opportunity where Id = :tk.WhatId ];
          if( tk.OwnerId == opp.OwnerId && tk.ActivityDate != null) {                   
            if (tk.isClosed == false) {
              opp.Next_Step_Date__c = tk.ActivityDate;
              opp.NextStep = tk.Type;
              update opp;
}
}
}
}
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
kamlesh_chauhankamlesh_chauhan

Hi,

 

I have updated the code as you might getting some compile time errors.

 

trigger NextTaskInfo on Task (after insert, after update) {
          

  if(Trigger.new.size() == 1 ) {

    Task tk = Trigger.New[0];
    String str = tk.whatid;
    if(str != null && str.substring(0,3)== '006')
    {

         Opportunity opp = [select OwnerId,NextTaskSubject__c,Next_Task_Date__c from Opportunity where Id = :tk.WhatId ];

        List<Task> tskMin = [Select ActivityDate,Subject From Task where whatid=:tk.whatid and  what.type = 'Opportunity' and isClosed  = false order By ActivityDate limit 1];  

        if (tskMin.size()>0) {
                opp.Next_Step_Date__c=tskMin[0].ActivityDate;
                opp.NextTaskSubject__c=tskMin[0].Subject;
        }
        else {
                opp.Next_Step_Date__c=null;        
                opp.NextTaskSubject__c='';
        }
        update opp;
    }
}
}

 

Regards,

Kamlesh

 

All Answers

kamlesh_chauhankamlesh_chauhan

Hi,

 

I think this code will help to you.

 

 

trigger NextTaskInfo on Task (after insert, after update) {
          

  if(Trigger.new.size() == 1 ) {

    Task tk = Trigger.New[0];
    String str = tk.whatid;
    if(str != null && str.substring(0,3)== '006')
    {

         Opportunity opp = [select OwnerId,NextTaskSubject__c,Next_Task_Date__c from Opportunity where Id = :tk.WhatId ];

        List<Task> tskMin = [Select ActivityDate From Task where whatid=:tk.whatid and  what.type = 'Opportunity' and isClosed  = false order By ActivityDate limit 1];  

        if (tskMin.size()>0) {
                opp.Next_Step_Date__c=tskMin.ActivityDate;
                opp.NextStep__c=tskMin.Type;
        }
        else {
                opp.Next_Step_Date__c=null;        
                opp.NextStep__c='';
        }
        update opp;
    }
}
}

 

 

Regards,

Kamlesh

SpunkySpunky

Thanks very much Kamlesh. I appreciate your help. 

 

I updated my trigger but it still does not pull the next activity date.

 

I saw that you are ordering by activity date.

Do you think it's missing something like sort by Ascending order?

Which line of the trigger is the instruction to pull the respective date?

 

Regards

Kavita

 

kamlesh_chauhankamlesh_chauhan

Hi,

 

I have updated the code as you might getting some compile time errors.

 

trigger NextTaskInfo on Task (after insert, after update) {
          

  if(Trigger.new.size() == 1 ) {

    Task tk = Trigger.New[0];
    String str = tk.whatid;
    if(str != null && str.substring(0,3)== '006')
    {

         Opportunity opp = [select OwnerId,NextTaskSubject__c,Next_Task_Date__c from Opportunity where Id = :tk.WhatId ];

        List<Task> tskMin = [Select ActivityDate,Subject From Task where whatid=:tk.whatid and  what.type = 'Opportunity' and isClosed  = false order By ActivityDate limit 1];  

        if (tskMin.size()>0) {
                opp.Next_Step_Date__c=tskMin[0].ActivityDate;
                opp.NextTaskSubject__c=tskMin[0].Subject;
        }
        else {
                opp.Next_Step_Date__c=null;        
                opp.NextTaskSubject__c='';
        }
        update opp;
    }
}
}

 

Regards,

Kamlesh

 

This was selected as the best answer
SpunkySpunky

You are awesome. That worked like a charm. Thanks so much!!

JAW99JAW99

Awesome, this is just what I need. Do you have the test method?

SpunkySpunky

I didn't create a test method ( I know I know..shame on me) . Anyway, it's deployed in production and working great.

JAW99JAW99

How did you get it deployed in production without test coverage? it won't let me do that,.

SpunkySpunky

I use the force.com IDE to deploy it. What tool do you use?

JAW99JAW99

I built it in my sandbox and then used the deployments in there. Doesn't the ForceIDE in Eclipse also ask for coverage as well?

SpunkySpunky

it depends on the existing code coverage for your production triggers. I forgot the threshold but it has to meet a certain % and if thats the case, it will allow you to deploy it with a warning.

 

Attaching my code-i may have made slight modifications to it from the last one posted

 

trigger UpdateOpptyNextStepandNextStepDate on Task (after insert,after update) {
if(Trigger.new.size() == 1 ) {
    Task tk = Trigger.New[0];
    String str = tk.whatid;
    if(str != null && str.substring(0,3)== '006'){
        Opportunity opp = [select Id,recordtypeid,StageName,IsClosed,NextStep,Next_Step_Date__c from Opportunity where Id = :tk.WhatId ];
        List<Task> tskMin = [Select Id,recordtypeid,ActivityDate,Type From Task where whatid=:tk.whatid and isClosed  = false order By ActivityDate limit 1];  
        if (opp.recordtypeid<>'012300000000lqS'&& opp.IsClosed!=true){
        if (tskMin.size()>0) {
                opp.Next_Step_Date__c=tskMin[0].ActivityDate;
                opp.NextStep=tskMin[0].Type;
                update opp; 
        }
        else {
                opp.Next_Step_Date__c=null;        
                opp.NextStep='';
                update opp; 
        }       
        }   
    }
}
}

 

 

JAW99JAW99

I see the update, but it's still not letting me deploy. I need coverage.

also, why is it when I try to deploy one trigger, test coverage for other already deployed triggers factor in?

SpunkySpunky

There is an overall test coverage minimum for all the code so looks like you need to increase coverage on existing test classes. (When you try to deploy, it will display the percentage you need to meet)

 

I went through this and had to increase coverage on all my existing triggers before i could deploy it.

 

 

 

 

JAW99JAW99

Hmm, how did i even deploy those other testless triggers before then i wonder

JAW99JAW99

anyway, this is fristrating. people are very helpful with code sharing here. then i feel like a pest b/c i have to ask for help with the accompanying test coverage code.

SpunkySpunky

Sorry I cant help more. I'm not a developer but thank heavens to the discussion forums and all the apex and visualforce development guides, I'm learning painfully slowly.

Dave_KellerDave_Keller

I see it's been some time since you wrote this, and I have verified in sandbox that your code works perfectly, but I'm also struggling with test coverage. I've spent weeks trying to get it above zero coverage, to no avail. Does anyone have a sample of test code for this trigger that gets full coverage?