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
NikiG22NikiG22 

Rollup number of Custom Objects

Hello - I have a custom object called a Sectacular__c that is located on the OpportutiyLineItem.

 

I need to roll up the count of all the Products (opportutniyLineItems) the Spectatular has.

 

I have gotten the following error but can not figure out the issue:

Error	Error: Compile Error: Didn't understand relationship 'OpportunityLineItem' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 10 column 30

 

 

Here is my code:

trigger OppLineItmRollup on Spectacular__c (after insert,after update)
{
   
  Set<id> RollChilSet  = new Set<id>();
  List<Spectacular__c> updateList = New List<Spectacular__c>();
  for(OpportunityLineItem rollUpchild :trigger.new){
      RollChilSet.add(rollUpchild.Spectacular__c); 
  }

   for(Spectacular__c rolPar:[select id,Sponsorships_Sold__c,Sold_Amount__c,(select id, UnitPrice, Quantity, Spectacular__c FROM OpportunityLineItem) FROM Spectacular__c where id IN:RollChilSet]) {
  decimal totalval = 0;  
 
    for(OpportunityLinrItem rol : rolPAr.OpportuntityLineItem){
    totalval = totalval  + rol.Quantity;
   
     }  

    rolPa.Sponsorships_Sold__c = totalval ;
    updateList.add(rolPar);

    }

   update updateList;

 }

 Please help.

Niki

jbroquistjbroquist
for(Spectacular__c rolPar:[select id,Sponsorships_Sold__c,Sold_Amount__c,(select id, UnitPrice, Quantity, Spectacular__c FROM Opportunity_Product__r) FROM Spectacular__c where id IN:RollChilSet]) 
{
    //loop code here
}

 

For queries like that you are going to want to use the Child Relationship Name, not the API Name from the custom field detail page on the Opportunity Product object. It should be something along the lines of "Opportununity_Product" etc...

 

You will then want to add "__r" to the end.

 

Hope that makes sense...

NikiG22NikiG22

I have tried OpportuntiyLineItems, OpportunityLineItem__r?

jbroquistjbroquist

I modified my post above, check it out again.

NikiG22NikiG22

that worked. but now im getting this error.

Error	Error: Compile Error: Loop variable must be of type SOBJECT:Spectacular__c at line 6 column 27

 Code:

  Set<id> RollChilSet  = new Set<id>();
  List<Spectacular__c> updateList = New List<Spectacular__c>();
  for(OpportunityLineItem rollUpchild :trigger.new){
      RollChilSet.add(rollUpchild.Spectacular__c);
  }

 

thanks for all your help

jbroquistjbroquist

Right, because the trigger is on the Spectacular__c object. Trigger.new has references to all the Spectacular__c objects, in that loop you are trying to reference OpportunityLineItems....

NikiG22NikiG22

sorry APEX is not my strong point.

 

So i have my Spectacular object that has opportunityLine items associated with it. So sales will sell a Spec and add it to their opportuntiy product. i need the roll up to count everytime a new Spec is added or removed and add the number to the Sponsorships_Sold__c field. 

 

I will also need it to roll up all the UnitPrice (amounts) and add it to the Sold_Amount__c field.

 

 

jbroquistjbroquist

You're going to want the trigger on the Opportunity List Items then. Something like this...

trigger OpportunityLineItemTrigger on OpportunityListItem (after insert, after update, after delete, after undelete)
{
    Set<Id> spectacularIds = new Set<Id>();

    for(OpportunityListItem o : Trigger.new)
    {
        spectacularIds.add(o.Spectacular__c);
    }

    Map<Id, Spectacular__c> spectacularMap = new Map<Id, Spectacular__c>([SELECT Id, Sponsorships_Sold__c, Sold_Amount__c FROM Spectacular__c WHERE Id IN: spectacularIds]);

    for(OpportunityListItem o : Trigger.new)
    {
        Spectacular__c s = spectacularMap.get(o.Spectacular__c);

        s.Sponsorships_Sold__c += 1;
        s.Sold_Amount__c += o.Amount__c;
    }

    update spectacularMap.values();
}

 You are also going to want to expand this logic to subtract the values when a record is deleted. Let me know if you have any questions.

NikiG22NikiG22

Ok - so I have this code. It dosent throw any errors but it isnt adding the number to my Spectacular Object?

 

trigger rollupfieldupdate on OpportunityLineItem (after update, before delete) {
    public Id sId;
    
      
   if(Trigger.isInsert)
    
  {
       Set<Id> spectacularIds = new Set<Id>();
       for(OpportunityLineItem  opp : Trigger.New)
        
        {
           spectacularIds.add(opp.Spectacular__c);
        }  
             
       Map<Id, Spectacular__c> spectacularMap = new Map<Id, Spectacular__c>
       ([SELECT Id, Sponsorships_Sold__c, Sold_Amount__c FROM Spectacular__c WHERE Id IN: spectacularIds]);
        
        for(OpportunityLineItem opp : Trigger.new)
        
       { 
           Spectacular__c s = spectacularMap.get(opp.Spectacular__c);
           s.Sponsorships_Sold__c +=1;
            
            update s;
        }
    }
    
    
    if(Trigger.isDelete)
  {
       Set<Id> spectacularIds = new Set<Id>();
       for(OpportunityLineItem  opp : Trigger.old)
        
        {
           spectacularIds.add(opp.Spectacular__c);
        }  
             
       Map<Id, Spectacular__c> spectacularMap = new Map<Id, Spectacular__c>
       ([SELECT Id, Sponsorships_Sold__c, Sold_Amount__c FROM Spectacular__c WHERE Id IN: spectacularIds]);
        
        for(OpportunityLineItem opp : Trigger.new)
        
       { 
           Spectacular__c s = spectacularMap.get(opp.Spectacular__c);
           s.Sponsorships_Sold__c =-1;
            
            update s;
        }
    
}


}