You need to sign in to do that
Don't have an account?

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; } } } } }
Hi,
I have updated the code as you might getting some compile time errors.
Regards,
Kamlesh
All Answers
Hi,
I think this code will help to you.
Regards,
Kamlesh
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
Hi,
I have updated the code as you might getting some compile time errors.
Regards,
Kamlesh
You are awesome. That worked like a charm. Thanks so much!!
Awesome, this is just what I need. Do you have the test method?
I didn't create a test method ( I know I know..shame on me) . Anyway, it's deployed in production and working great.
How did you get it deployed in production without test coverage? it won't let me do that,.
I use the force.com IDE to deploy it. What tool do you use?
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?
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
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?
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.
Hmm, how did i even deploy those other testless triggers before then i wonder
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.
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.
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?