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
sekhar y 1sekhar y 1 

Sales order price distribution

trigger newSOL on Sales_Order_Line__c (Before Update) {

 String orderItemId,childOrderItemId;
 Decimal Diff, childAdjustedPrice,parentAdjustedPrice,parentUnitPrice,childUnitPrice,ratio,newChildAdjustedPrice;
 
if( Trigger.isUpdate )
{
    for (Sales_Order_Line__c orderLine: Trigger.new) {
            
         orderItemId = String.ValueOf(orderLine.OrderItemID__c);
         parentAdjustedPrice = orderLine.Adjusted_Price__c;
         parentUnitPrice = orderLine.Unit_Price__c;

        list<Sales_order_Line__c> childOrderLines= [select Product__c,Unit_Price__c,Adjusted_Price__c,Parent_OrderItemID__c,OrderItemID__c from Sales_Order_Line__c where Parent_OrderItemID__c =: orderItemId];
        List<Sales_Order_Line__c> recordsToBeUpdated = new List<Sales_Order_Line__c>();
         
        for(Sales_Order_Line__c l: childOrderLines)
        {
                    
             childOrderItemId = String.ValueOf(l.OrderItemID__c);
             childUnitPrice = l.Unit_Price__c;
             ratio = childUnitPrice / parentUnitPrice;
             childAdjustedPrice = l.Adjusted_Price__c;
            // Diff = parentUnitPrice - parentAdjustedPrice ; 
             newChildAdjustedPrice = ratio * parentAdjustedPrice;
             
              if(String.valueOf(l.OrderItemID__c) == childOrderItemId){
                 recordsToBeUpdated.Add(new Sales_Order_Line__c(Adjusted_Price__c= newChildAdjustedPrice));
             }
             
           Database.update(recordsToBeUpdated);
        }
 
        
       }
}
}

getting this error, please help me

Error:Apex trigger newSOL caused an unexpected exception, contact your administrator: newSOL: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.newSOL: line 31, column 1
Anupam RastogiAnupam Rastogi

Hi Sekhar,

In the code, you are creating a list of new 'Sales_Order_Line__c' object records, but you are using Database.Update() method. This is going to error out because update operation requires an existing record.

So you should be using Database.Insert instead. Here is the modified code.

trigger newSOL on Sales_Order_Line__c (Before Update) {
    
    String orderItemId,childOrderItemId;
    Decimal Diff, childAdjustedPrice,parentAdjustedPrice,parentUnitPrice,childUnitPrice,ratio,newChildAdjustedPrice;
    
    if( Trigger.isUpdate )
    {
        for (Sales_Order_Line__c orderLine: Trigger.new) {
            
            orderItemId = String.ValueOf(orderLine.OrderItemID__c);
            parentAdjustedPrice = orderLine.Adjusted_Price__c;
            parentUnitPrice = orderLine.Unit_Price__c;
            
            list<Sales_order_Line__c> childOrderLines= [select Product__c,Unit_Price__c,Adjusted_Price__c,Parent_OrderItemID__c,OrderItemID__c from Sales_Order_Line__c where Parent_OrderItemID__c =: orderItemId];
            List<Sales_Order_Line__c> recordsToBeUpdated = new List<Sales_Order_Line__c>();
            
            for(Sales_Order_Line__c l: childOrderLines)
            {
                
                childOrderItemId = String.ValueOf(l.OrderItemID__c);
                childUnitPrice = l.Unit_Price__c;
                ratio = childUnitPrice / parentUnitPrice;
                childAdjustedPrice = l.Adjusted_Price__c;
                // Diff = parentUnitPrice - parentAdjustedPrice ; 
                newChildAdjustedPrice = ratio * parentAdjustedPrice;
                
                //--- This IF Statement is redundant because childOrderItemId was initialized from String.valueOf(l.OrderItemID__c) itself therefore they will always be equal
                if(String.valueOf(l.OrderItemID__c) == childOrderItemId){
                    recordsToBeUpdated.Add(new Sales_Order_Line__c(Adjusted_Price__c= newChildAdjustedPrice));
                }
                
                Database.insert(recordsToBeUpdated);
            }
            
            
        }
    }
}
Thanks
AR

If you find the reply useful that solves your problem then please mark it as best answer.
sekhar y 1sekhar y 1

Hi AR,
Again i am getting this error.

Database.insert(recordsToBeUpdated);

Error:Apex trigger newSOL caused an unexpected exception, contact your administrator: newSOL: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Sales Order]: [Sales Order]: Trigger.newSOL: line 31, column 1
Anupam RastogiAnupam Rastogi
Hi Sekhar,

These are required field errors. You need to provide values for all the required fields because you are creating a new record. In the below mentioned line provide all required fields and respective values.
 
recordsToBeUpdated.Add(new Sales_Order_Line__c(Adjusted_Price__c= newChildAdjustedPrice));

Thanks
AR
sekhar y 1sekhar y 1
Hi AR,
If you dont mind, can you send text mail for me?
sekhar.cloud@gmail.com 
sekhar y 1sekhar y 1
This is my acutal requirement.
please do needful.


Ability to distribute the value of the "Sales_Order_Line__c.Adjusted_Price" field across related package line items.

Create trigger, on update of "Sales Order Line" object, on change "Adjusted Price" field.

Trigger will distribute the new adjusted price across any sales order line records where the ParentOrderItemID = OrderItemID of the triggering Sales order Line record.

Trigger must use the distribution percentages set in the unit price field across the adjusted price fields.