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

Update opportunity from task subject
I am trying to get a trigger in place that upon creating a task with the subject 'Requalify', change the closed opportunity to stage "Re-Qualify". This below code is not working. Any ideas?
trigger TaskSubject on Task (after update, after insert) {
set<id> accountIdSet = new set<id>();
//capture all the subject names in a set
//set<string> subjectName = new set<string>{'Requalify'};
map<id, list<opportunity>> opptyMap = new map<id, list<opportunity>>();
list<opportunity> updateOppList = new list<opportunity>();
//get those tasks where the tasks are related to account. Capture the account IDs in a set.
for(task t: trigger.new){
string tempId = string.valueOf(t.WhatId);
if(tempId.startsWith('001')){
accountIdSet.add(t.whatId);
}
}
//If we have any account IDs then get the associated opportuity and store the one account to many opportunity in a map<id, list<opportunity>>
if(accountIdSet.size()>0){
for(opportunity opp:[select Name, stageName, accountid from opportunity where accountid IN :accountIdSet]){
if(opptyMap.containsKey(opp.accountId)){
list<opportunity> oppList = opptyMap.get(opp.accountId);
oppList.add(opp);
opptyMap.put(opp.accountId, oppList);
}else{
list<opportunity> newOppList = new list<opportunity>();
newOppList.add(opp);
opptyMap.put(opp.accountId, newOppList);
}
}
}
//If we get the opportunities then change the stage.
if(Trigger.isUpdate && opptyMap.size()>0){
for(task t: trigger.new){
//Check if the subject of the task is one among the set
//and also confirm that the subject has been changed.
if(t.subject != trigger.oldMap.get(t.id).subject && opptyMap.containsKey(t.whatId)){
//iterate thru the list of the opportunity associated with that account
//and check which of the opportunity contains the task subject in the
//opportunity name and update the stage to re-qualify
for(opportunity oppty: opptyMap.get(t.whatId)){
{
oppty.stageName='Re-Qualify';
updateOppList.add(oppty);
}
}
}
}
}
if(Trigger.isInsert && opptyMap.size()>0){
for(task t: trigger.new){
if(opptyMap.containsKey(t.whatId)){
for(opportunity oppty: opptyMap.get(t.whatId)){
if(oppty.name.contains(t.subject)){
oppty.stageName='Re-Qualify';
updateOppList.add(oppty);
}
}
}
}
}
//update the oppties
if(updateOppList.size()>0){
update updateOppList;
}
}
trigger TaskSubject on Task (after update, after insert) {
set<id> accountIdSet = new set<id>();
//capture all the subject names in a set
//set<string> subjectName = new set<string>{'Requalify'};
map<id, list<opportunity>> opptyMap = new map<id, list<opportunity>>();
list<opportunity> updateOppList = new list<opportunity>();
//get those tasks where the tasks are related to account. Capture the account IDs in a set.
for(task t: trigger.new){
string tempId = string.valueOf(t.WhatId);
if(tempId.startsWith('001')){
accountIdSet.add(t.whatId);
}
}
//If we have any account IDs then get the associated opportuity and store the one account to many opportunity in a map<id, list<opportunity>>
if(accountIdSet.size()>0){
for(opportunity opp:[select Name, stageName, accountid from opportunity where accountid IN :accountIdSet]){
if(opptyMap.containsKey(opp.accountId)){
list<opportunity> oppList = opptyMap.get(opp.accountId);
oppList.add(opp);
opptyMap.put(opp.accountId, oppList);
}else{
list<opportunity> newOppList = new list<opportunity>();
newOppList.add(opp);
opptyMap.put(opp.accountId, newOppList);
}
}
}
//If we get the opportunities then change the stage.
if(Trigger.isUpdate && opptyMap.size()>0){
for(task t: trigger.new){
//Check if the subject of the task is one among the set
//and also confirm that the subject has been changed.
if(t.subject != trigger.oldMap.get(t.id).subject && opptyMap.containsKey(t.whatId)){
//iterate thru the list of the opportunity associated with that account
//and check which of the opportunity contains the task subject in the
//opportunity name and update the stage to re-qualify
for(opportunity oppty: opptyMap.get(t.whatId)){
{
oppty.stageName='Re-Qualify';
updateOppList.add(oppty);
}
}
}
}
}
if(Trigger.isInsert && opptyMap.size()>0){
for(task t: trigger.new){
if(opptyMap.containsKey(t.whatId)){
for(opportunity oppty: opptyMap.get(t.whatId)){
if(oppty.name.contains(t.subject)){
oppty.stageName='Re-Qualify';
updateOppList.add(oppty);
}
}
}
}
}
//update the oppties
if(updateOppList.size()>0){
update updateOppList;
}
}