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
Eelco LitjensEelco Litjens 

Update opportunity product quantity when opp field is changed

Hi, i was reading this question: https://developer.salesforce.com/forums/?id=906F000000094QPIAY 
and thought i might change the used field to my own field, and give it a go, but it doesn't work.
Should i change something to the recordtypes etc?
i want this for 1 recordtype.
Is there anybody that can help me?
trigger opportunityLineItemsQuantity on Opportunity (after update) {
    //Get list of record types from Opportunity
    list<RecordType> rTypes = [Select Name, Id from RecordType Where SObjectType= 'Opportunity'];
    
    //Create a Map for the Opportunity record types
    Map<String,Id> OppRecordTypes = new Map<String, Id>{};
    for(RecordType rt: rtypes)
    OppRecordTypes.put(rt.Name,rt.Id);
       
    //Create a map for Opportunity records
    //Map<String, Decimal> oppList = new Map<String, Decimal>{};
    Map<ID, Opportunity> oppList = new Map<ID, Opportunity>();
    
    //Iterate through the Opportunities
    for(Opportunity opp : Trigger.New)  
    if(opp.RecordTypeId == OppRecordTypes.get('Consultancy')){
    if(Opp.Consultinghours_spent2__c != Null){
    //oppList.put(opp.Id,Opp.Consultinghours_spent2__c);
     oppList.put(opp.Id,Opp);
    }
    }
     list<OpportunityLineItem> listOfOpportunityLineItems = new list<OpportunityLineItem>();
    //Get Opportunity Line Items that are associated to the Opportunity
    for(OpportunityLineItem oli :  [Select OpportunityId, Quantity From OpportunityLineItem Where OpportunityId In : oppList.keyset()]){
   //This is where the code is failing. This is where I want the OpportunityLineItem Quantity to be the same as the Opportunity Expected_Number_Of_Sudents__c value.

    //oli.Quantity = oppList.get(oppList.Expected_Number_Of_Students__c);
    Opportunity OppObj = oppList.get(oli.OpportunityId);
       if(OppObj != NUll){
           oli.Quantity = OppObj.Consultinghours_spent2__c;
       }
       listOfOpportunityLineItems.add(oli);
    }
    Update listOfOpportunityLineItems;

}

 
James WooleyJames Wooley
Have you thought about using Process Buider for this? You can sepcify the Opportunity conditions then update all of its child Line Items with the Opportunity's quantity.

Thanks,
Jamie.
Eelco LitjensEelco Litjens
Hi Jamie, yes, i tried, but it didn't update the product quantity
i selected the opportunity Line Item,... and based on the change of the field in the opportunity, it should update the quantity referenced to the field in the opportunity. 
i tried it from object: opportunity/opportunity product
but nothing worked.
Eelco LitjensEelco Litjens

the field in the opportunity is a formula (number) which refers to a field in the (related) case, could that cause issues?
James WooleyJames Wooley
Hi Eelco,

I can't see any issues with your code and I don't think being a formula field should cause an issue. I'd suggest adding some debug statements, particularily on the value of Consultinghours_spent2__c at different stages, and then check in your debug logs that the code is definitely doing what you expect and has access to the correct values.

Thanks,
Jamie.
Eelco LitjensEelco Litjens
Jamie,
i'm a newbie regarding coding, and debugging i haven't done either.  is it hard to explain how to do it?
James WooleyJames Wooley
Hi Eelco,

Go to setup -> Logs -> Debug Logs. Search for your user and switch on debug logs. Keep an eye on these as they may run out.

Edit your code and add in system.debug statements where you want to see what the values are at different points in the code. E.g.
system.debug('*OppObj.Consultinghours_spent2__c* ' + OppObj.Consultinghours_spent2__c);

Then, when you make the code run, either via a unit test or in the UI, a debug log will be created and this will include information, including your debug statements.

Thanks,
Jamie.
Eelco LitjensEelco Litjens
Ok, thanks, i'll give it a try, thanks so far.