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
gr4v3rgr4v3r 

Field roll up to parent object

I am trying to roll up the Name field of a custom object (Study__c) to a field (Protocol__c) on its parent object (Opportunity). If there is more than one study the protocol field should have the text "MultiProt" but if there is only one study on the opportunity the protocol field should be populated by the name of the study.

 

Right now the code succesfully populates the field with multiprot if there is more than one study but wont do anything if there is only one study. I cant seem to find where my error is.

 

Any help would be greatly appreciated, thank you.

 

trigger ProtocallRollUp on Study__c (before insert, after update, after delete)
{
    study__c[] newst = trigger.new;    
    Set<Id> oppSet = new Set<Id>();
        for(Study__c p : newst ){
            oppSet.add(p.Opportunity__c);
        }


    List<Opportunity> oppList;
    
    try
    {
        Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>();
        Map<Id,Opportunity> toBeUpdated = new Map<Id,Opportunity>();

         
        for (Opportunity o : (oppList = [SELECT id,protocol_number__c from Opportunity where id IN:oppSet]))
        {
            oppMap = new Map<ID, Opportunity>(oppList);
                 
                List<Study__c> studyRslts = [SELECT ID, Opportunity__c, Name 
                    FROM Study__c 
                    Where Opportunity__c =:o.Id];
                    if (studyRslts!=null && studyRslts.size()>1)
                    {
                        
						
                        Opportunity parentOpp = oppMap.get(String.valueOf('Opportunity__c'));
                        parentOpp.Protocol_number__c = 'MultiProt';
                        
                        oppMap.put(parentOpp.Id,parentOpp);
                       
                    }
					else if (studyRslts!=null && studyRslts.size()==1)
					{
                        for(Study__c s : studyRslts)
                        {
						Opportunity parentOpp = oppMap.get(String.valueOf('Opportunity__c'));
                        parentOpp.Protocol_number__c = s.Name;
						
						oppMap.put(parentOpp.Id,parentOpp);
                        }
					}
                    
        }             
            /*if(oppMap.values().size()>0)
                {
                    toBeUpdated.putAll(oppMap);
                }
            
            else
            {
                
            }
                 
        if(toBeUpdated != null && toBeUpdated.size()>0)
        {
            update toBeUpdated.values();
        }
        else
        {*/
            if(oppMap.values().size()>0)
            {
                update oppMap.values();
            }
        //}
         
        
    
    }
    catch (Exception e) 
    {
        System.debug('\n\n>> OpportunityUpdate - Unable to update Opportunity__c.\nException encountered ' + e.getMessage());
    } //Catch
}

 

Santhosh KumarSanthosh Kumar

One possible reason is that your trigger is before insert so Opportunity will not have any children at this time as record is not inserted yet. So change that to after insert and try again.

 

Some other suggestions from the code below.

 

1. Don't write SOQL queries in for loop as it would lead to Governer limit errors

2. You could query child records using relationship queries.

3. If you know there is only one record in a list, you can directly refer to that using studyRslts[0]

 

Good luck.