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
KCarmenKCarmen 

How to create a field on an opp that shows status of most recent activity

Hi All - 

I am looking to bring in the task status of the most recent task activity (which is a particular record type, btw) onto the opportunity record. How can I do this?
I thought about a roll-up field but can't do that out of the box
I thought about Process Builder but can't seem to use that either
I thought about a cross-object formula field with a field update

What I need is to view what the current status is on a particular Task record type's most current record any time it is updated.
Any suggestions?
Best Answer chosen by KCarmen
HARSHIL U PARIKHHARSHIL U PARIKH
Hi OEMGirl,

I am pretty sure this is not possible with a standard functionalitie since there are lots of different types of functionalities involved such as checking/sorting the most recent date, checking After Delete operation, After UnDelete Operation, etc...

However, this is absolutely possible via Apex Trigger.
I would suggest the following approach,

1) First have a field named Most_Recent_Task_Status__c On an Opportunity Object's Pagelayout
2) Have this trigger on Task Object (Go to Task Triggers).

Trigger Code:
 
Trigger gettingMostRecentTaskStatus On Task(After Insert, After Update, After Delete, After UnDelete){
    
    List<Id> parentOppIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Task tsk : Trigger.New){
            If(tsk.WhatId != Null && tsk.Status != Null && tsk.WhatId.getSObjectType() == Opportunity.getSObjectType()){
                parentOppIds.add(tsk.WhatId);
            }
        }
    }
    If(Trigger.IsDelete){
        For(Task tsk : Trigger.Old){
            If(tsk.WhatId != Null && tsk.Status != Null && tsk.WhatId.getSObjectType() == Opportunity.getSObjectType()){
                parentOppIds.add(tsk.WhatId);
            }
        } 
    }
    
    List<Opportunity> oppFinalListToUpdate = New List<Opportunity>();
    
    For(Opportunity Opp : [Select Id, Most_Recent_Task_Status__c,
                                (Select Id, Status FROM Tasks WHERE Status != Null ORDER BY CreatedDate DESC LIMIT 1)
                                                FROM Opportunity WHERE Id =:parentOppIds])
    {
        List<String> recentStatus = New List<String>();
        
        If(Opp.Tasks.Size() > 0)
        {
            For(Task EveryTask : Opp.Tasks)
            {
                recentStatus.add(EveryTask.Status);
            }
        }
        
        If(recentStatus.size() > 0)
        {
            Opp.Most_Recent_Task_Status__c = recentStatus[0];
        }
        
        oppFinalListToUpdate.add(Opp);
        
    }
    
    try{
        If(!oppFinalListToUpdate.IsEmpty()){
            Update oppFinalListToUpdate;
        }
    }
    
    Catch(Exception e){
        System.debug('Thrown Exception for gettingMostRecentTaskStatus Trigger Is:: ' + e.getMessage());
    } 
}

What does trigger does,

Let's say you have 5 tasks under an Opportunity record and all with the different dates. Trigger is going to take the most recent task record's Status and puts it under the "Most Recent Task Status" field.

Trigger is also taking care all the consitions such as After Delete, After Undelete etc.. This is After Trigger so it fire right after you save the task record for a particulat opportunity.

This is how the final output look like,

User-added image


I hope this helps and if it solves the question then please mark it as best answer since it will help other users in community as well.
Thank You, OEMGirl!

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
Hi OEMGirl,

I am pretty sure this is not possible with a standard functionalitie since there are lots of different types of functionalities involved such as checking/sorting the most recent date, checking After Delete operation, After UnDelete Operation, etc...

However, this is absolutely possible via Apex Trigger.
I would suggest the following approach,

1) First have a field named Most_Recent_Task_Status__c On an Opportunity Object's Pagelayout
2) Have this trigger on Task Object (Go to Task Triggers).

Trigger Code:
 
Trigger gettingMostRecentTaskStatus On Task(After Insert, After Update, After Delete, After UnDelete){
    
    List<Id> parentOppIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Task tsk : Trigger.New){
            If(tsk.WhatId != Null && tsk.Status != Null && tsk.WhatId.getSObjectType() == Opportunity.getSObjectType()){
                parentOppIds.add(tsk.WhatId);
            }
        }
    }
    If(Trigger.IsDelete){
        For(Task tsk : Trigger.Old){
            If(tsk.WhatId != Null && tsk.Status != Null && tsk.WhatId.getSObjectType() == Opportunity.getSObjectType()){
                parentOppIds.add(tsk.WhatId);
            }
        } 
    }
    
    List<Opportunity> oppFinalListToUpdate = New List<Opportunity>();
    
    For(Opportunity Opp : [Select Id, Most_Recent_Task_Status__c,
                                (Select Id, Status FROM Tasks WHERE Status != Null ORDER BY CreatedDate DESC LIMIT 1)
                                                FROM Opportunity WHERE Id =:parentOppIds])
    {
        List<String> recentStatus = New List<String>();
        
        If(Opp.Tasks.Size() > 0)
        {
            For(Task EveryTask : Opp.Tasks)
            {
                recentStatus.add(EveryTask.Status);
            }
        }
        
        If(recentStatus.size() > 0)
        {
            Opp.Most_Recent_Task_Status__c = recentStatus[0];
        }
        
        oppFinalListToUpdate.add(Opp);
        
    }
    
    try{
        If(!oppFinalListToUpdate.IsEmpty()){
            Update oppFinalListToUpdate;
        }
    }
    
    Catch(Exception e){
        System.debug('Thrown Exception for gettingMostRecentTaskStatus Trigger Is:: ' + e.getMessage());
    } 
}

What does trigger does,

Let's say you have 5 tasks under an Opportunity record and all with the different dates. Trigger is going to take the most recent task record's Status and puts it under the "Most Recent Task Status" field.

Trigger is also taking care all the consitions such as After Delete, After Undelete etc.. This is After Trigger so it fire right after you save the task record for a particulat opportunity.

This is how the final output look like,

User-added image


I hope this helps and if it solves the question then please mark it as best answer since it will help other users in community as well.
Thank You, OEMGirl!
This was selected as the best answer
KCarmenKCarmen
Thanks Harshil - this worked beautifully! Now I have to figure out how to only populate the Most Recent Task Status with a record type of Demo Sheet. We track our demos via task record type.

Thanks again!!!