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
Rachel WinnRachel Winn 

Is this something that can be done with a trigger?

One of my customers sent me the following:

"We have a child object connected to Opportunities that is pretty simple – it consists of a couple of text fields, a date. Reps use it to record “next steps” for opportunities. What we need is the latest “Next Step” created related to an opportunity to populate a field on the parent opportunity with one of its text fields. This way, the latest “Next Step” can be included in reports along with opportunities and their products. I still haven’t looked into it too much but if it’s possible, I believe it’ll have to be a trigger. I guess the high level requirement here is we want to record next steps as deals progress but we also want the latest one – and only the latest one – to fit into a funnel report that is used to manage calls, etc."
techcharlietechcharlie
Yes, this can definitely be done with a trigger. 

Your trigger will fire when a new Next Step record is inserted, and the trigger will have to query all the Next Step records for the opportunity and sort them by created date and limit to 1. The trigger can then use the info that the query returned and update the opp.

I hope this helps?
kevin Carotherskevin Carothers
Hi Rachel -

this might be close to what you need - 
You might need an after update trigger, but I didn't account for that.   Plus you'll need a test class.   I just took a swag at it.

trigger firststep on NextStep__c (after insert, after delete) {
    List<Id> oid = new List<Id>();
    List<Opportunity> theOpportunities;
    List<Opportunity> updateOpportunities = new List<Opportunity>();
    Map<Id, Id> theOppList = new Map<Id, Id>();
    List<Id>theStepIds = new List<Id>();
    
    //AFTER DELETE
    if(Trigger.isDelete) {  
        for(NextStep__c t :Trigger.old) {
            theStepIds.add(t.Id);
            if(t.Opportunity__c != null) {
                oid.add(t.opportunity__c);
                theOppList.put(t.Id, t.Opportunity__c);
                }
            }     
        
        theOpportunities = ([SELECT Id, Name, NextStep__c,
                        (SELECT Id, CreatedDate FROM NextStep__r 
                         WHERE Id NOT IN :theStepIds 
                        ORDER BY CreatedDate DESC LIMIT 1) 
                FROM Opportunity WHERE Id IN :oid]);     
        
        for(Opportunity o :theOpportunities) {
            for(NextStep__c t :Trigger.old) {
               if(o.Id == t.Opportunity__c && o.NextStep__r.size() > 0) {
                   o.NextStep__c = o.NextStep__r[0].Id;
                   updateOpportunities.add(o);  
                   }                
               }
            }
        update updateOpportunities;    
        } 
        
    //AFTER INSERT
    if(Trigger.isInsert) {  
        for(NextStep__c t :Trigger.new) {
            theStepIds.add(t.Id);
            if(t.Opportunity__c != null) {
                System.debug('t.opportunity__c: ' +t.opportunity__c);
                oid.add(t.opportunity__c);               
                }
            }

        theOpportunities = ([SELECT Id, Name, NextStep__c, NextStep__r.CreatedDate 
                FROM Opportunity WHERE Id IN :oid]);   
                            
        for(Opportunity o :theOpportunities) {
            for(NextStep__c t :Trigger.new) {
                if(t.Opportunity__c == o.Id) {                   
                    o.NextStep__c = t.Id;
                    updateOpportunities.add(o);
                    }
                }    
            }   
        update updateOpportunities;
        }                      
}