• SF Admins
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hi,

I am looking for help on Rollup summary on self lookup relationship . I am beginner in apex programming and I was able to implement the below solution. Solution is working fine but for my use case it is updating only first line.
Sales Order Header and Sales Order Lines have Master detail relationship.
Sales Order header has lookup relationship to Sales Order header
Sales Order Lines have lookup relationship to Sales Order lines

Use Case: Parent Sales Order header has  multiple sales order lines. When split Child Sales Order has multiple lines. I want the rollup of all the lines onto respective parent Sales Order lines. Currently the below solution is working for first line.

public static void OnAfterUpdateInsert(List<SCMC__Sales_Order_Line_Item__c> Soline1){
        List <SCMC__Sales_Order_Line_Item__c> oppList = [select id, Original_Qty_Ordered__c, SCMC__Original_Sales_Order_Line_Item__c, SCMC__Status__c from 
                                                         SCMC__Sales_Order_Line_Item__c where id =: trigger.new and 
                                                         SCMC__Original_Sales_Order_Line_Item__c != null Limit 100];
        if(oppList.size() > 0){
            Id oppId = [select SCMC__Original_Sales_Order_Line_Item__c from SCMC__Sales_Order_Line_Item__c where id =: trigger.new 
                        and SCMC__Original_Sales_Order_Line_Item__c != null][0].SCMC__Original_Sales_Order_Line_Item__c;
            Decimal intSum;
            system.debug('what is my id ===' +oppId);
            AggregateResult[] groupedResults = [SELECT SUM(SCMC__Quantity__c) amt FROM SCMC__Sales_Order_Line_Item__c where 
                                                SCMC__Original_Sales_Order_Line_Item__c =: oppId and SCMC__Status__c != 'Cancelled'];
            for (AggregateResult a : groupedResults){
                intSum = (Decimal)a.get('amt');   
            }
            SCMC__Sales_Order_Line_Item__c opp = [select id, Original_Qty_Ordered__c, SCMC__Quantity__c, SCMC__Status__c from 
                                                  SCMC__Sales_Order_Line_Item__c where id =: oppId];
            opp.Original_Qty_Ordered__c = intSum;
            update opp;
            system.debug('sum amount *****' +intSum);
        }
    }

Any help or refernece would be highly appreciated. Thank you.

 

I am looking for some help to create a field on the Opportunity page which sums the amount of Child Opportunities. The child opp has a lookup to the Parent. It does not appear to let me use a Roll-up summary formula to do this so I am looking for help on a way to do this.