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 

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

crop1645crop1645

NikiG22

 

Why is this line here?

 

For (integer i = 0; i < OLupdisc.opportunity.Ad_Agency_Discount__c; i++){

 

seems like a completely errant for loop

NikiG22NikiG22

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

Abhi_TripathiAbhi_Tripathi

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

NikiG22NikiG22

thank you for your reply - i have changed my code a littlebit, i do not get errors but nothing happens?

 

trigger AgencyDiscount on Opportunity (before update) {
for( Opportunity opp: Trigger.new)
{

List<OpportunityLineItem> lineitem = [ SELECT Id, unitprice, Opp_Agency_Discount__c, OpportunityLineItem.opportunity.Ad_Agency_Discount__c, Opportunity.ID, Discount from
OpportunityLineItem where Opportunity.id = :opp.Id];

List<OpportunityLineItem> lineitemToUpdate = new List<OpportunityLineItem>();

for(OpportunityLineItem thislineitem: lineitem )
{
   if( thislineitem.Opp_Agency_Discount__c > 0.00)
   {
       thislineitem.unitprice =   thislineitem.unitprice -(thislineitem.unitprice * thislineitem.Opp_Agency_Discount__c);
      lineitemToUpdate.add(thislineitem);
   }
}


{
   update lineitemToUpdate;
}

}
}

 

Abhi_TripathiAbhi_Tripathi

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

 

List<OpportunityLineItem> lineitem = [ SELECT Id, unitprice, Opp_Agency_Discount__c, OpportunityLineItem.opportunity.Ad_Agency_Discount__c, Opportunity.ID, Discount from
OpportunityLineItem where OpportunityId = :opp.Id];
NikiG22NikiG22

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.

 

trigger AgencyDiscount on Opportunity (after delete, after insert, after undelete, 
after 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>();
           
            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);
                }
            }   
            
            try {
                if(linesToUpdate.size() > 0) {
                    update linesToUpdate;
                }
            } catch (Exception e) {
                system.debug('Message: ' + e.getMessage() + ' Stack Trace: ' + e.getStackTraceString());
            }
        }
    }                                             
       }                                            
                                                   

 

 

 

Abhi_TripathiAbhi_Tripathi

Hey Nikki..in your code everything looks fine,...but i think you should change thins thing in your code

 

for(OpportunityLineItem oli : lineItems) {
                if(oli.opportunity.Ad_Agency_Discount__c >0.00) {

//new OpporutunityLineItem
OpportunityLineItem oppLineItem = new OpportunityLineItem();
oppLineItem.Id = oli.Id;
//<-----assign all required field of oli to oppLineItem here just like above Id oppLineItem.unitprice = oli.unitprice-(oli.unitprice* oli.opportunity.Ad_Agency_Discount__c); linesToUpdate.add(oppLineItem); } }
NikiG22NikiG22

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 :-) 

Abhi_TripathiAbhi_Tripathi

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());
}
}
}
}

Abhi_TripathiAbhi_Tripathi

if it is not yet solved then kindly mail me @ abhi.rec08@gmail.com