• gr4v3r
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

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
}

 

  • August 16, 2013
  • Like
  • 0