You need to sign in to do that
Don't have an account?
Alexandra 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,
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,
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.
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.
Please find below updated code. It should be working.
Best regards,
BALAJI
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?
Please see below updated code:
Let us know if this works for you.
Best Regards,
BALAJI
It is working as expected now.
Thank you.
Please close this thread by Marking a Best Answer so that it can be useful for other community members.
Best Regards,
BALAJI