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

Trigger on task to change opportunity status
I am trying to write a trigger on task that queries the subject, and if it is equal to "Requalify", find the related account and related opportunity and change the opportunity status to "Re-qualify". I have the below trigger, but it is not changing the opportunity status. Any help would be greatly appreciated!
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;
}
}
trigger TaskAfterInsertUpdate on Task (after update, after insert)
{
list<opportunity> liOpportunities = new list<opportunity>();
list<id> liIDs = new list<id>();
for(Task sTask : trigger.new)
{
if(sTask.Status == 'Requalify' && ('' + sTask.WhatId).startsWith('006'))
{
liIDs.add(sTask.WhatId);
}
}
for(Opportunity sOppty : [select Id, status from Opportunity where Id in : liIDS])
{
sOppty.status = Requalify;
liOpportunities.add(sOppty);
}
update liOpportunities;
}