function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
John_SFDCJohn_SFDC 

Opportunity with TAsks

Scenario: 
I have a Oppotunity Record,Under that record I have added two opportunity Teaam Member

Opportunity Team Mem        Role
Justin                                Account Manager
Rahul                                Account Manager

Activity Type Picklist Values : Banking,Electricity,Heatlh,HR 

When ever the Opportunity StageName is Changed from Prospective to Qualification ,then we need create the Task with the Activity type :Banking and Assigned To is one of the Opportunity Team Member(He should be Account Manager role):
Whenever the that Task (Banking)  status to Completed,then  we need to create the one more task with  the Activity typ :Elictricity and Assigned to is on of the Opportunity Team Mem(He should be Account Manager role).
Whenever the that Task (Electricity)  status to completed we need to create the two  more tasks with with the Activity typ :Health and HR and Assigned to is on of the Opportunity Team Mem(He should be Account Manager role).

NOTE: If no Opportunity team member  on opportunity record ,Assign To should be owner of that record

Please Help me

Thanks In Advance.


 
James LoghryJames Loghry
Sounds like you could use either a before or after update trigger on the Opportunity that queries the opportunities' related oppty team members, and the decides who to assign to which task.  Pretty straight forward trigger to write.
Mahesh DMahesh D
Hi John,

Please find the modified trigger:
 
trigger UpdaetOpportunity on Opportunity (after insert, after update) {

    String recordTypeName = 'Commercial';
    Map<String, Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Opportunity.getRecordTypeInfosByName();
    Id recTypeId = rtMapByName.get(recordTypeName).getRecordTypeId();
    List<task> taskList = new list<task>();
    set<id> oppIdSet = new set<id>();
    for(opportunity opp:trigger.new) {
        if(opp.StageName.contains('Development') && opp.RecordTypeId == recTypeId && (Trigger.isInsert || trigger.OldMap.get(opp.Id).StageName == 'Identification')) {
            oppIdSet.add(opp.id);
        }
    }
    
    if(!oppIdSet.isEmpty()) {
        Map<Id, Id> oppOwnerIdMap = new Map<Id, Id>();
        for(OpportunityTeamMember otm: [SELECT Id, OpportunityId, UserId  FROM OpportunityTeamMember WHERE TeamMemberRole = 'Commercial Manager' AND OpportunityId IN:oppIdSet]) {
            oppOwnerIdMap.put(otm.OpportunityId, otm.UserId);
        }
            
        for(Opportunity opp:trigger.new) {
            if(opp.StageName.contains('Development') && opp.RecordTypeId == recTypeId && (Trigger.isInsert || trigger.OldMap.get(opp.Id).StageName == 'Identification')) {
                Task t = new Task();
                t.Subject = 'Others';
                t.Status = 'Inprogress';
                t.whatid = opp.id;
                t.ActivityDate = System.today()+5;
                t.Type_of_Activity__c = 'Kick off meeting';
                t.Priority = 'Normal';
                t.Ownerid = opp.Ownerid;
                if(oppOwnerIdMap.get(opp.Id) != null) {
                    t.Ownerid = oppOwnerIdMap.get(opp.id);
                }
                taskList.add(t);
            }
            
        }
        if(!taskList.isEmpty())
            insert taskList;
    }
}

Please do let me know if it helps you.

Regards,
Mahesh
Mahesh DMahesh D
Hi John,

Please find the Task trigger:
 
trigger CreatTask on Task (before update) {
    Set<Id> oppIdSet = new Set<Id>();
 
    for(Task t: Trigger.new) {
        If(t.Status == 'Completed' && (t.Type_of_Activity__c == 'Kick off meeting' || t.Type_of_Activity__c == 'Offer Development') && t.whatId.getsObjectType() == Opportunity.sObjectType){
            oppIdSet.add(t.whatId);
        }
    }
    
    if(!oppIdSet.isEmpty()) {
        Map<Id, Id> oppOwnerIdMap = new Map<Id, Id>();
        List<Task> finalTaskList = new List<Task>();
        for(OpportunityTeamMember otm: [SELECT Id, OpportunityId, UserId  FROM OpportunityTeamMember WHERE TeamMemberRole = 'Commercial Manager' AND OpportunityId IN:oppIdSet]) {
            oppOwnerIdMap.put(otm.OpportunityId, otm.UserId);
        }
        
        Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([Select Id, OwnerId from Opportunity where Id IN: oppIdSet]);
        
        for(Task t: Trigger.new) {
            If(t.Status == 'Completed' && t.whatId.getsObjectType() == Opportunity.sObjectType) {
                if(t.Type_of_Activity__c == 'Kick off meeting') {
                    Task newTask = new Task();
                    newTask.Subject = 'Others';
                    newTask.Status = 'Inprogress';
                    newTask.whatid = t.whatid;
                    newTask.Priority = 'Normal';
                    newTask.ActivityDate=System.today()+10;
                    newTask.Type_of_Activity__c= 'Offer Development';
                    if(oppOwnerIdMap.get(t.whatId) != null) {
                        newTask.Ownerid = oppOwnerIdMap.get(t.whatId);
                    } else {
                        newTask.Ownerid = oppMap.get(t.whatId).Ownerid;
                    }
                    finalTaskList.add(newTask);
                }
                
                if(t.Type_of_Activity__c == 'Offer Development') {
                    Task skTask = new Task();
                    Task appTask = new Task();
                    
                    skTask.Subject = 'Others';
                    skTask.Status = 'Inprogress';
                    skTask.whatid = t.whatid;
                    skTask.Priority = 'Normal';
                    skTask.ActivityDate = System.today()+7;
                    skTask.Type_of_Activity__c = 'Approval TW';
                    
                    appTask.Subject = 'Others';
                    appTask.Status = 'Inprogress';
                    appTask.whatid = t.whatid;
                    appTask.Priority = 'Normal';
                    appTask.ActivityDate = System.today()+7;
                    appTask.Type_of_Activity__c = 'Approval TW';
                    
                    if(oppOwnerIdMap.get(t.whatId) != null) {
                        skTask.Ownerid = oppOwnerIdMap.get(t.whatId);
                        appTask.Ownerid = oppOwnerIdMap.get(t.whatId);
                    } else {
                        skTask.Ownerid = oppMap.get(t.whatId).Ownerid;
                        appTask.Ownerid = oppMap.get(t.whatId).Ownerid;
                    }
                    
                    finalTaskList.add(skTask);
                    finalTaskList.add(appTask);
                }
            }
        }
        if(!finalTaskList.isEmpty())
            insert finalTaskList;
    }
}

Please do let me know if it helps you.

Regards,
Mahesh