+ Start a Discussion
Warren Wade, AEWarren Wade, AE 

Roll Up Trigger of Max Record that Returns text

I think I've exhausted my declarative means to get this info (native features, Process Builder and Flow) and I'm not a programmer so I'm reaching out.
  • I have a two objects: Preserve__c (Master) and Activities__c (Detail). 
  • The Activities__c object has two pertinent fields: Date__c & Type__c.
  • On Preserve__c, I have a field Most_Recent_Activity_Type__c.
  • I need to roll the Type__c of the most recent Activity record to the Preserve record to that field.
Example
  1. Type = Walk, Date = 1/1/2017
  2. Type = Run, Date = 6/1/2017
  3. Type = Fly, Date = 9/1/2017
Most Recent Activity = Fly

Help!
Best Answer chosen by Warren Wade, AE
Hemant_SoniHemant_Soni
Hi,
Please try below code and please mark it solved.
trigger RollUpRecentActivity on Preservation_Activities__c (after insert, after update) {
    set<Id>setPreserveId = new set<Id>();
    list<Preservation_Object__c>lstPreserve = new list<Preservation_Object__c>();
    if(trigger.isAfter){
        system.debug('@Developer --> isAfter:');
        if(trigger.isInsert || trigger.isUpdate){
            for(Preservation_Activities__c oActivities:trigger.new){
                setPreserveId.add(oActivities.Preservation_Object__c);
            }
        }
        system.debug('@developer-->setPreserveId: '+setPreserveId);
        if(setPreserveId.size()>0){
        for(Preservation_Activities__c oActivities:[
            	Select Preservation_Object__c,id,Name,Activity_Type__c,Activity_Date__c  
            	from Preservation_Activities__c 
            	where Preservation_Object__c In:setPreserveId and Activity_Date__c != null 
            	order by Activity_Date__c desc limit 1]){
            Preservation_Object__c oPreserve = new Preservation_Object__c();
            oPreserve.Id = oActivities.Preservation_Object__c;
            oPreserve.Last_Activity_Type__c = oActivities.Activity_Type__c;
            lstPreserve.add(oPreserve);
        }
        system.debug('@developer-->lstPreserve: '+lstPreserve);
        if(lstPreserve.size()>0){
            update lstPreserve;
        }
        }
    }
 }

Thanks
Hemant​

All Answers

VamsiVamsi
Hi,

You can get this done using process builder with the below.

1. Create a Process builder on Activities__c 
2. make sure to select the criteria as on record creation only 
3. specify filter criteria if any 
4. Select field update action on Preserve__c record 
5. Then perform assignment to (Preserve  object field)Type__c = Type__c (Activity__C field value)

So that (Preserve  object field)Type__c field will get updated with the newly inserted Activities__c record Type__c value 

Hope this helps .......!!!

Please mark as best answer if the above helps..!!!
Hemant_SoniHemant_Soni
Hi,
Please try below code and please before saving this code please update field according to your fields.
trigger RollUpRecentActivity on Activities__c (after insert, after update) {
    set<Id>setPreserveId = new set<Id>();
    list<Preserve__c>lstPreserve = new list<Preserve__c>();
    if(trigger.isAfter){
        system.debug('@Developer --> isAfter:');
        if(trigger.isInsert || trigger.isUpdate){
            for(Activities__c oActivities:trigger.new){
                setPreserveId.add(oActivities.YOUR Preserve__c LookUp FIeld In Activities__c);
            }
        }
        system.debug('@developer-->setPreserveId: '+setPreserveId);
        if(setPreserveId.size()>0){
        for(Activities__c oActivities:[Select YOUR Preserve__c LookUp FIeld In Activities__c,id,Name,Type__c,Date__c  from Activities__c where YOUR Preserve__c LookUp FIeld In Activities__c In:setPreserveId and Date__c != null order by Date__c desc limit 1]){
            Preserve__c oPreserve = new Preserve__c();
            oPreserve.Id = oActivities.YOUR Preserve__c LookUp FIeld In Activities__c;
            oPreserve.Most_Recent_Activity_Type__c = oActivities.Type__c;
            lstPreserve.add(oActivities);
        }
        system.debug('@developer-->lstPreserve: '+lstPreserve);
        if(lstPreserve.size()>0){
            update lstPreserve;
        }
        }
    }
 }
If this helps you please mark it solved.
Thanks
Hemant
 
Warren Wade, AEWarren Wade, AE
Hi, @Hemant_Soni!  Thanks so much for that.  I've updated the code to reflect the actual API information (see below)

When I made those adjustments, I'm now getting the following error from line 21 (lstPreserve.add(oActivities);) Any idea how to resolve this?  I feel like you've gotten me 99% of the way there and I probably just typed something wrong.
Method does not exist or incorrect signature: void add(Preservation_Activities__c) from the type List<Preservation_Object__c>

Updated Code
trigger RollUpRecentActivity on Preservation_Activities__c (after insert, after update) {
    set<Id>setPreserveId = new set<Id>();
    list<Preservation_Object__c>lstPreserve = new list<Preservation_Object__c>();
    if(trigger.isAfter){
        system.debug('@Developer --> isAfter:');
        if(trigger.isInsert || trigger.isUpdate){
            for(Preservation_Activities__c oActivities:trigger.new){
                setPreserveId.add(oActivities.Preservation_Object__c);
            }
        }
        system.debug('@developer-->setPreserveId: '+setPreserveId);
        if(setPreserveId.size()>0){
        for(Preservation_Activities__c oActivities:[
            	Select Preservation_Object__c,id,Name,Activity_Type__c,Activity_Date__c  
            	from Preservation_Activities__c 
            	where Preservation_Object__c In:setPreserveId and Activity_Date__c != null 
            	order by Activity_Date__c desc limit 1]){
            Preservation_Object__c oPreserve = new Preservation_Object__c();
            oPreserve.Id = oActivities.Preservation_Object__c;
            oPreserve.Last_Activity_Type__c = oActivities.Activity_Type__c;
            lstPreserve.add(oActivities);
        }
        system.debug('@developer-->lstPreserve: '+lstPreserve);
        if(lstPreserve.size()>0){
            update lstPreserve;
        }
        }
    }
 }

 
Hemant_SoniHemant_Soni
Hi,
Please try below code and please mark it solved.
trigger RollUpRecentActivity on Preservation_Activities__c (after insert, after update) {
    set<Id>setPreserveId = new set<Id>();
    list<Preservation_Object__c>lstPreserve = new list<Preservation_Object__c>();
    if(trigger.isAfter){
        system.debug('@Developer --> isAfter:');
        if(trigger.isInsert || trigger.isUpdate){
            for(Preservation_Activities__c oActivities:trigger.new){
                setPreserveId.add(oActivities.Preservation_Object__c);
            }
        }
        system.debug('@developer-->setPreserveId: '+setPreserveId);
        if(setPreserveId.size()>0){
        for(Preservation_Activities__c oActivities:[
            	Select Preservation_Object__c,id,Name,Activity_Type__c,Activity_Date__c  
            	from Preservation_Activities__c 
            	where Preservation_Object__c In:setPreserveId and Activity_Date__c != null 
            	order by Activity_Date__c desc limit 1]){
            Preservation_Object__c oPreserve = new Preservation_Object__c();
            oPreserve.Id = oActivities.Preservation_Object__c;
            oPreserve.Last_Activity_Type__c = oActivities.Activity_Type__c;
            lstPreserve.add(oPreserve);
        }
        system.debug('@developer-->lstPreserve: '+lstPreserve);
        if(lstPreserve.size()>0){
            update lstPreserve;
        }
        }
    }
 }

Thanks
Hemant​
This was selected as the best answer