You need to sign in to do that
Don't have an account?
NikiG22
Updateing UnitPrice with Discount Field on Opp
Hello - I am trying to update the Unitprice on the opportuntiylineitem when a discount is added/removed on the opportuntiy. I have code here but it is not working. Any help would be greatly appriciated!!!
The Opportuntiy discount field is called "Ad_Agency_Discount__c".
trigger AgencyDiscount on Opportunity (after update) { List<Opportunity> discopps=new List<Opportunity>(); for (Opportunity opp : trigger.new) { Opportunity discOpp=trigger.oldMap.get(opp.id); if (opp.Ad_Agency_Discount__c >0.00) { discopps.add(opp); } } if (!discopps.isEmpty()) { List<OpportunityLineItem> olis = [SELECT ID,UnitPrice ,OpportunityLineItem.opportunity.Ad_Agency_Discount__c,Opp_Agency_Discount__c FROM OpportunityLineItem WHERE OpportunityId in :discopps]; List<Opportunity> opps=new List<Opportunity>(); for (OpportunityLineItem OLupdisc: olis) { For (integer i = 0; i < OLupdisc.opportunity.Ad_Agency_Discount__c; i++){ OpportunityLineItem newUnitPrice = new OpportunityLineItem (); newUnitPrice.unitprice = OLupdisc.unitprice-(OLupdisc.unitprice *OLupdisc.opportunity.Ad_Agency_Discount__c); update OLupdisc; } } } }
Thank you,
Niki
NikiG22
Why is this line here?
seems like a completely errant for loop
Eric - Im just tryinh to get this project out so i am recycling old code. I did take it out but nothing is happening...
Thanks for the reply.
Niki
Hey Niki,
In the last FOR loop ,you have created "newUnitPrice" and then Updating "OLupdisc"
There is no connection. I think you should update OLupdisc.
Thanks
thank you for your reply - i have changed my code a littlebit, i do not get errors but nothing happens?
nothing is happening because i think, your query isn't returing anything, in OpportunityLineItem oppurtunityId is the fields API name that you are using in where clause,, make it OpportunityId , Just like I did
And one more thing use debug logs to check you query results, that'll help you alot
I did more research and modified my code to see if this helps and nothig...ugh. As you can tell im not a developer and dont create APEX very often so i appriciate your help.
Hey Nikki..in your code everything looks fine,...but i think you should change thins thing in your code
Thank you. I have changed the code to what you recomended and no errors... but the unitprice still is not changing...im ready to pull my hair out :-)
Hey Nikki. change your events
just paste this code, I have changed the events this time,
trigger AgencyDiscount on Opportunity (before insert, before update){
public static void afterUpdateTrigger (List<Opportunity> optys, Map<ID, Opportunity> oldOptys){
Set<ID> optyIDs = new Set<ID>();
for(Opportunity opty : optys) {
Opportunity oldOpty = oldOptys.get(opty.Id);
system.debug('Old Discount: ' + oldOpty.Ad_Agency_Discount__c);
system.debug('New Discount: ' + opty.Ad_Agency_Discount__c);
if(opty.HasOpportunityLineItem == true) {
optyIDs.add(opty.Id);
}
}
if(!optyIDs.isEmpty()) {
List<OpportunityLineItem> lineItems = [SELECT Id, OpportunityId, Agency_Discount_Applied__c,Opportunity.Ad_Agency_Discount__c, unitprice
FROM OpportunityLineItem
WHERE OpportunityId IN :optyIDs];
List<OpportunityLineItem> linesToUpdate = new List<OpportunityLineItem>();
try {
for(OpportunityLineItem oli : lineItems) {
if(oli.opportunity.Ad_Agency_Discount__c >0.00) {
oli.unitprice = oli.unitprice-(oli.unitprice* oli.opportunity.Ad_Agency_Discount__c);
linesToUpdate.add(oli);
}
}
} catch (Exception e) {
system.debug('Message: ' + e.getMessage() + ' Stack Trace: ' + e.getStackTraceString());
}
}
}
}
if it is not yet solved then kindly mail me @ abhi.rec08@gmail.com