You need to sign in to do that
Don't have an account?
learn_sfdc
Copying fields from Opportunity Product to Opportunity
I am trying to copy value from Opportunity product to Opportunity
Field is TotalPrice on Opportunity Product to RSF_CLD_CB_CommValue__c on Opportunity
below is the code i am using but im getting error
Error: Compile Error:
id,RSF_CLD_CB_CommValue__c from Opportunity)
^
ERROR at Row:1:Column:62
Didn't understand relationship 'Opportunity' 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 5 column 51
Field is TotalPrice on Opportunity Product to RSF_CLD_CB_CommValue__c on Opportunity
below is the code i am using but im getting error
Error: Compile Error:
id,RSF_CLD_CB_CommValue__c from Opportunity)
^
ERROR at Row:1:Column:62
Didn't understand relationship 'Opportunity' 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 5 column 51
trigger CommValue on OpportunityLineItem (after insert,after update) { List<Opportunity> lstOppUpdate = new List<Opportunity>(); List<OpportunityLineItem> lstOli = [select id,TotalPrice,(select id,RSF_CLD_CB_CommValue__c from Opportunity) from OpportunityLineItem where id in: trigger.newmap.keyset()]; for(OpportunityLineItem oli : lstOli){ if(oli.TotalPrice != NULL){ for(Opportunity opp: oli.Opportunity__r){ opp.RSF_CLD_CB_CommValue__c += oli.TotalPrice; lstOppUpdate.add(opp); } } } if(lstOppUpdate.size() > 0){ update lstOppUpdate; }
It looks like you have the relationship in the wrong order. Opportunity is parent to the OpportunityLineItem, meaning that OpportunityLineItem has a lookup to the Opportunity.
I believe you'll want to collect the Opportunity Id values into a Set, then query for Opportunities while capturing their child OpportunityLineItems. Here's how I might approach this...
What could be even more fun is using AggregateResult (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_agg_fns.htm) to sum up TotalPrice from OpportunityLineItems directly in the query, without having to iterate over them.
Have you considered what should happen if/when an OpportunityLineItem is deleted? In this case, you'd probably want to fire an "after delete" trigger and reference trigger.old to capture the affected Opportunity Id values.
For that matter, you should probably consider firing a trigger "after undelete" on OpportunityLineItem too, because Salesforce also allows the undeleting of records.
I hope this helps. Please reach out if you think I might have missed something.
<pre>
trigger CommValue on OpportunityLineItem ( after insert, after update )
{
List<Opportunity> lstOppUpdate = new List<Opportunity>();
List<OpportunityLineItem> lstOli =
[ SELECT TotalPrice, Opportunity.Id, Opportunity.RSF_CLD_CB_CommValue__c
FROM OpportunityLineItem
WHERE Id IN :Trigger.new
];
for ( OpportunityLineItem oli : lstOli )
{
if ( oli.TotalPrice == null ) continue;
oli.Opportunity.RSF_CLD_CB_CommValue__c += oli.TotalPrice;
lstOppUpdate.add( oli.Opportunity );
}
// there is no error to update an empty collection,
// and it does not count against DML limits
update lstOppUpdate;
}
</pre>
<pre>
trigger CommValue on OpportunityLineItem ( after insert, after update, after delete, after undelete )
{
Set<Id> oppIds = new Set<Id>();
for ( OpportunityLineItem oli : Trigger.isDelete ? Trigger.old : Trigger.new )
{
oppIds.add( oli.OpportunityId );
}
List<Opportunity> oppsToUpdate = new List<Opportunity>();
for ( AggregateResult result :
[ SELECT SUM(TotalPrice) totalPrice, OpportunityId
FROM OpportunityLineItem
WHERE OpportunityId IN :oppIds
GROUP BY OpportunityId
]
)
{
oppsToUpdate.add
( new Opportunity
( Id = (Id) result.get( 'OpportunityId' )
, RSF_CLD_CB_CommValue__c = (Decimal) result.get( 'totalPrice' )
)
);
}
update oppsToUpdate;
}
</pre>
Thank you so much for rerplying, it helped me a lot.