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
CaitlinGMCaitlinGM 

trigger finessing - updating master object based on detail objects

I'm trying to have a field on my master object (Opportunity, field is called AFPAK_Student_Names__c) reflect the a field on all detail objects (Student_Assignment__c) meeting a certain criteria (Student_Assignment__c.status = Active) at any given time. Kind of like a text roll-up summary field. 

 

This trigger works just fine to add student names to the field when I add new Student Assignments with Status Active. However, what if I delete one or change the status? I want the field to reflect the change. 

 

In that case, is there a way to use Apex to test if the text field contains certain text (Student Name from the now non-active Student Assignment) and then remove just that? 

 

trigger StudentAssignmentClassUpdate on Student_Assignment__c (after insert) { 

if (trigger.isInsert){
Map<Id,Id> AssignmentToClassMap = new Map<Id,Id>();
for(Student_Assignment__c A : trigger.new)
           AssignmentToClassMap.put(A.Class__c,A.Id);
 
List<Opportunity> OppsToUpdate = new List<Opportunity>();

      
    for (Opportunity TestOpp: [SELECT Id, AFPAK_Student_Names__c FROM Opportunity WHERE Id IN:  AssignmentToClassMap.keySet()])
    {
        Id TestOppId = AssignmentToClassMap.get(TestOpp.Id);
        Student_Assignment__c StudAss = trigger.newMap.get(TestOppId);
        
               
//CASE 1: if inserting a Student Assignment with status Active, add student name to AFPAK student names field        
if (StudAss.status__c=='Active'){
TestOpp.AFPAK_Student_Names__c =TestOpp.AFPAK_Student_Names__c+' '+[SELECT Name From Contact Where Id = :StudAss.Student_Name__c limit 1].Name;
OppsToUpdate.add(TestOpp);
}
}
if(OppsToUpdate != null && !OppsToUpdate.isEmpty())
Database.update(OppsToUpdate);
}
}

 

Starz26Starz26

try:

 

String nString;

nString = string.replace('searchFor','');

field = nString;

 

 

I have not tested but I believe if the searchFor string is not found nothing will happen. If it errors you will have to:

 

Integer sStart =0;

String nStringa ='';

String nStringb = '';

Integer lSearch = 0;

 

lSearch = searchstring.length('searchFor');

sStart = field.indexOf('searchFor',0);

 

If (sStart != -1){

   nStringa = field.substring(0,sStart);

   nStringb = field.sibstring(sStart + lSearch);

   field = nStringa + nStringb

}

 

You will have to play with the indexes and ensure the spaces between the string remain....If the first way works when the search string is not found then you will not have to go this route.

 

CaitlinGMCaitlinGM

OK, thank you. I decided that an alternate approach would work--I can accomplish what I'm trying to do without actually having the Opportunity field reflect the detail object data if I set up a view on the detail object rather than the Opportunity and train my users to find all their needed info there.