Can I get help with a trigger that will populate the opportunity stage on a related task? I'm very green with triggers so you cannot get to basic if you feel so inclined.
Thanks for the help.
Attribution to: Jim
Possible Suggestion/Solution #1
If you navigate to "Customize" in the setup menu, and then "Activities" and finall "Task Triggers" you will be taken to your task trigger page. Chances are you will want to click "New" here. This will take you to a new task trigger page with a skeleton of:
You will want to populate it up to be something along the lines of:
trigger atrigger on Task (before insert, before update)
{
// Prepare a list of the parent opportunities and load into a map
List<Id> theParents = new List<Id>();
for(Task thisTask : trigger.new)
{
theParents.add(thisTask.WhatId);
}
Map<Id, Opportunity> theOpps = new Map<Id,Opportunity>([SELECT Id, StageName FROM Opportunity WHERE Id IN :theParents]);
// Now go through the tasks and set their stage on the child task field (by pulling the relevant parent back out the map)
for(Task thisTask : trigger.new)
{
thisTask.Parent_Stage__c = theOpps.get(thisTask.WhatId).StageName;
}
}
(Now, whilst that compiles, but I don't know the specifics of what you are trying to achieve, so expect some tweaking on your part to make this work)
Attribution to: Simon Lawrence
Possible Suggestion/Solution #2
I Hope below link help you!
populate the Opportunity Stage in a custom field on the Task object: https://developer.salesforce.com/forums/ForumsMain?id=906F000000091fKIAQ
Attribution to: Beginer
Possible Suggestion/Solution #3
Beginer,
Thanks for the link, that is exactly what I'm looking for! Can someone offer up suggestions for the test class?
I have the following, but I'm not sure how to fill in the blanks to make this work:
@isTest
public class TestClass{
static testMethod void Test(){
// Create the data that you would need here, for example in your case you would create a collection of tasks
List<Task> tasks = new List<Task>();
for(Integer i = 0; i < 50; i++){ //You can add as many test cases as you want here, typically I go for 25-100
<Create new tasks while giving them the required information here, you should include some sort of test indicator>
tasks.add(<the task you created above>)
}
test.startTest();
insert tasks;
test.stopTest();
//Query the database using SOQL to get the list of tasks you just inserted and their related opportunities
//Using System.assertEquals(value a, value b) make sure that the opportunity stage is update correctly
}
}
Attribution to: Jim Gentile
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33357