+ Start a Discussion
Alexandra CuyversAlexandra Cuyvers 

Woulid like to rollup custom field from OpportunityLineItem to Opportunity

Hello, 
I am trying to rollup a custom field "Product_Alert_Message" from the OpportunityItemLine to an Opportunity field "Product_Alerts_Roll_up__c".
I have the below Apex Class:
global with sharing class OppProductReqRollupTriggerHandler 
{
    global static void MainProcess(set<id> OpportunityIds)
    {
        //Create List to hold final concatenated result
        List<Opportunity> OpportunityList = new List<Opportunity>();
        
         //Create list to Children that have Parents within Parent Set.
        List<Opportunity> Opportunity = [Select Id,Product_Alerts_Roll_up__c, (SELECT Id,   OpportunityLineItem.Product_Alert_Message__c from OpportunityLineItem) from Opportunity where Id in :OpportunityIds];
 
        //Loop through List result to build concatenated string and add to sampleList
        for (Opportunity s:Opportunity)
        {
           String concatenateString = '';
              for (OpportunityLineItem sp: s.OpportunityLineItem)
              {
                if(sp.OpportunityLineItem__r.Product_Alert_Message__c <> null)
                {
                    concatenateString += sp.OpportunityLineItem__r.Product_Alert_Message__c + ';';
                }
              }
              s.Product_Alerts_Roll_up__c = concatenateString.replace('null','').removeEnd(';');
              OpportunityList.add(s);
        }
 
        //Update Parent object with concatenated string
        update OpportunityList;
    }
}
 but it's giving me the below errors:
Error: Compile Error:
OpportunityLineItem.Product_Alert_Message__c from OpportunityLineItem) from Opportunity
^
ERROR at Row:1:Column:102
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 9 column 41.
Can someone help me with this?
Thanks,
abhishek singh 497abhishek singh 497
Hello,
so far I can see that your query is not correct.
Please use the below query.

List<Opportunity> Opportunity = [Select Id,Product_Alerts_Roll_up__c, (SELECT Id, Product_Alert_Message__c from OpportunityLineItems) from Opportunity where Id in :OpportunityIds];

Please let me know are there any further errors.

Thanks & Regards,
Abhishek Singh.
Meghna Vijay 7Meghna Vijay 7
Hi ,
When fetching child records from parent use it's Child Relationship Name which is 'OpportunityLineItems' in case of OpportunityLineItem.
Replace the SOQL query with this query:-
 List<Opportunity> Opportunity = [Select Id,Product_Alerts_Roll_up__c, (SELECT Id,   OpportunityLineItem.Product_Alert_Message__c from OpportunityLineItems) from Opportunity where Id in :OpportunityIds]; 
Hope it helps, if it does mark it as solved.
 
Meghna Vijay 7Meghna Vijay 7
Also there is no need to use OpportunityLineItem.Product_Alert_Message__c if it is a field on OpportunityLineItems. You can just use Product_Alert_Message__c in inner SOQL.
BALAJI CHBALAJI CH
Hi Alexandra,

Please find below updated code. It should be working.
 
global with sharing class OppProductReqRollupTriggerHandler 
{
    global static void MainProcess(set<id> OpportunityIds)
    {
        //Create List to hold final concatenated result
        List<Opportunity> OpportunityList = new List<Opportunity>();
        
        //Create list to Children that have Parents within Parent Set.
        List<Opportunity> Opportunity = [Select Id,Product_Alerts_Roll_up__c, (SELECT Id, Product_Alert_Message__c from OpportunityLineItems) from Opportunity where Id in :OpportunityIds];
        
        //Loop through List result to build concatenated string and add to sampleList
        for (Opportunity s:Opportunity)
        {
            String concatenateString = '';
            for (OpportunityLineItem sp: s.OpportunityLineItems)
            {
                if(sp.Product_Alert_Message__c <> null)
                {
                    concatenateString += sp.Product_Alert_Message__c + ';';
                }
            }
            s.Product_Alerts_Roll_up__c = concatenateString.replace('null','').removeEnd(';');
            OpportunityList.add(s);
        }
        
        //Update Parent object with concatenated string
        update OpportunityList;
    }
}

Best regards,
BALAJI​​​​​​​
Alexandra CuyversAlexandra Cuyvers
Thanks Balaji and Meghna
the Apex Class is working now.
I am struggeling with the Apex trigger:

trigger OppProductReqRollupTrigger on OpportunityLineItem (after insert, after update, after delete, after undelete){ 

   Set<id> setOfParents = new set<id>();
    if (Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete){
        for (OpportunityLineItem   iSP  :Trigger.new){
            setOfParents.add(iSP.Opportunity);
        }
    } else{
        //Trigger.isDelete
        for (OpportunityLineItem   iSP  :Trigger.old){
            setOfParents.add(iSP.Opportunity);
        }
    }
 
    OppProductReqRollupTriggerHandler.MainProcess(setOfParents);
    }
-> I get this 
Error: Compile Error: Method does not exist or incorrect signature: void add(Opportunity) from the type Set<Id> at line 6 column 26.

Any thoughts?
BALAJI CHBALAJI CH
The Set is declared as Id, so you need to change it to 
setOfParents.add(iSP.OpportunityId);

Please see below updated code:
trigger OppProductReqRollupTrigger on OpportunityLineItem (after insert, after update, after delete, after undelete){ 
    
    Set<id> setOfParents = new set<id>();
    if (Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete){
        for (OpportunityLineItem iSP  :Trigger.new){
            setOfParents.add(iSP.OpportunityId);
        }
    } else{
        //Trigger.isDelete
        for (OpportunityLineItem   iSP  :Trigger.old){
            setOfParents.add(iSP.OpportunityId);
        }
    }
    
    OppProductReqRollupTriggerHandler.MainProcess(setOfParents);
}

Let us know if this works for you.

Best Regards,
BALAJI​​​​​​​
Alexandra CuyversAlexandra Cuyvers
You are awesome ! 
It is working as expected now.
Thank you.
BALAJI CHBALAJI CH
Great to listen.
Please close this thread by Marking a Best Answer so that it can be useful for other community members.

Best Regards,
BALAJI​​​​​​​