0

I'm looking to conditionally update the Opportunity stage when the Task subject (Opportunity & Task belong to same Account) is changed:

Here is the task page, field of concern is Subject:

Image1

When the subject is changed to 'Send Email', the Opportunity stage linked to the same account auto updates to 'Qualification', for example. Here, the Opportunity and the Task are for Account ABC:

Image2

Now this is what I've done so far; the following is the underdeveloped Apex code to auto update the stage:

public class UpdateOpp {

   public static void update_oppstage(task[] tasks) {

       for (Task t :tasks) {

           if (t.subject == 'Send Letter') {
            // update (account) corresponding opportunity stage field to say 'Qualification'          
            }        
        }
    }
}

And finally, the trigger code:

trigger TaskSubject on Task (after update) {

    Task[] tasks = Trigger.new;
    UpdateOpp.update_oppstage(tasks);
}

I need help to update the stage to a defined value for the corresponding opportunity. Thanks for your time.

Important Update: My task subject has custom entries in the comboBox - Call EJF, Email Centurion, Follow up Merion, etc. Opportunity names are created meaningfully for an account and the opp name will have one of these (EJF, Centurion, Merion) matching names. So when a task with subject Email EJF is created for account ABC, the opp for the same account ABC with opp name 'EJF' should auto update the opp stage to 'Qualification'.

This is more complex but the basic subject change updates opportunity stage remains unchanged.

2
  • 1
    As you know one account may have many opportunity tagged to it. So as per your logic, when the task's subject associated to a account is changed to 'Send Email', then the stage for all the opportunity associated to that account should be changed to 'qualification'??
    – Sam
    Jul 24 '13 at 5:06
  • I have modified the code. Can you check? please post further comments on the answer itself
    – Sam
    Jul 25 '13 at 6:37
1

Hope the below code helps!

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>{'Call EJF','Email Centurion','Initiate Merion'};
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 qualification

            for(opportunity oppty: opptyMap.get(t.whatId)){
                if(oppty.name.contains(t.subject)){
                    oppty.stageName='Qualification';
                    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='Qualification';
                      updateOppList.add(oppty);
                   }
               }
            }
        }
    }
//update the oppties
if(updateOppList.size()>0){
    update updateOppList;
}
}

EDIT:
This code is working. I followed the below flow: Create an account(test account) --> Create a task for this account (test account) and make sure the subject is not 'Initiate Merion'.
create an opportunity (make sure the stage is not qualification) for the same account, name it 'test oppty Initiate Merion'.
Now edit the task created, change the subject to 'Initiate Merion'. You can now see that the Opportunity stage has been changed to 'Qualification'.

EDIT 2
Added an after insert event. Also the one more code block to handle the insert and update events seperately

EDIT 3 Task subject need not necessarily be from a set of predefined strings. As long as task subject is part of Opp name, stage must be updated

5
  • Are you pasting the code AS-IS. Because I just did that and never got any error. But anyways if you get a EOF error message, which might be coz you will be missing a curly braces at the end.
    – Sam
    Jul 25 '13 at 12:52
  • I have not tested the code for the functionality. It was just an example.. Anyways I will test it and back to you asap
    – Sam
    Jul 26 '13 at 16:04
  • Updated my answer..
    – Sam
    Jul 26 '13 at 16:55
  • 1
    Yes, the stage will not change for the second time. As per our functionality we are checking if the opportunity name contains the task's subject, since 'Call Merion' is not part of the opportunity name(Test Oppty Initiate Merion), the stage will not get changed... You could can check the contiansOnly() methods or probably split the task subject by whitepace and compare each value in the opportunity name.. You can check out the string methods here :salesforce.com/us/developer/docs/apexcode/Content/…
    – Sam
    Jul 29 '13 at 11:14
  • Sameer, confused with string methods. I tried this piece of coding but doesn't work: (Opp name = Test Oppty Initiate Merion). Code Start: string subjectNames = 'Merion EJF'; if(subjectNames.containsAny(t.subject)){update stage}. Code End. If t.subject is 'Merion', stage updates, when t.subject is 'Call Merion' however, stage doesn't update (when it actually should). Doesn't this string method work for strings or words in addition to characters? Just need a stage update when t.subject has either Merion or EJF in it. Is it possible to do so with std string methods instead of splitting subject?
    – Rosh
    Jul 30 '13 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.