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
Micky MMicky M 

Update related object

Hi all i have two objects related via a lookup up and depending on the value of one i want to update whats happening on the other via apex my code looks like :

InvoiceBreakdowns = [SELECT  Contract_Recurring_Service_Breakdown__r.Breakdown_Accrual__c, Band_Quantity__c, Banding_Total__c, Billing_Term__c, Break_Down_Type__c, Contract_Recurring_Service_Breakdown__r.Hold_Date__c, Contract_Recurring_Service_Breakdown__r.Breakdown_Hold_Value__c, End_Date__c, Id, Invoice_Date__c, MCS_Contract_Invoice_Line_Item__c, Metric__c, Name, Committed__c, Contract_Recurring_Service_Breakdown__r.Id, Start_Date__c, Start_Quantity__c,  Unit_Sell_Price__c, CreatedDate, Unbilled_Usage__c, Contract_Recurring_Service_Breakdown__c from MCS_Contract_Invoice_Line_Item_Breakdown__c Where CreatedDate >= :fromMidnight];

for(MCS_Contract_Invoice_Line_Item_Breakdown__c b : InvoiceBreakdowns)
        {

            //Is the breakdown billed this month? if it is then do all this lot
            if(InvoiceDate.Month() == system.today().month() &&  InvoiceDate.Year() == system.today().year())
            {
                //Check each value to make sure its not null as that blows everything up.
                Decimal ContractBreakdownAccrual = b.Contract_Recurring_Service_Breakdown__r.Breakdown_Accrual__c == null ? 0 : b.Contract_Recurring_Service_Breakdown__r.Breakdown_Accrual__c;
                Decimal Unbilled_Usage = b.Unbilled_Usage__c == null ? 0 : b.Unbilled_Usage__c;
                Decimal Banding_Total = b.Banding_Total__c == null ? 0 : b.Banding_Total__c;

                //add the contract accrual to the banding total.
                b.Banding_Total__c  =  Banding_Total + ContractBreakdownAccrual;

                //now set the accrual on the contract break down to zero
                if(b.Contract_Recurring_Service_Breakdown__c != null)
                {
                    System.debug('id = b.Contract_Recurring_Service_Breakdown__c = ' + b.Contract_Recurring_Service_Breakdown__c);
                    contractBreakdowns.add(new Contract_Recurring_Service_Breakdown__c(id = b.Contract_Recurring_Service_Breakdown__c, Breakdown_Accrual__c = 0));
                }
            }

does anyone know if there's a better way to update the field on the Contract_Recurring_Service_Breakdown__c object rather than do :

contractBreakdowns.add(new Contract_Recurring_Service_Breakdown__c(id = b.Contract_Recurring_Service_Breakdown__c, Breakdown_Accrual__c = 0));

and then run an update? as on a kinda related note this list is ending up with duplicate id's as there are a few breakdowns looking at the contract breakdown.

Thanks All
Balaji BondarBalaji Bondar
Hi Micky,
Plese use below code to udpate the records with the values:
List<MCS_Contract_Invoice_Line_Item_Breakdown__c> MCS_Contract_Invoice_Line_Item_Breakdown_List = new List<MCS_Contract_Invoice_Line_Item_Breakdown__c>();

for(MCS_Contract_Invoice_Line_Item_Breakdown__c b : InvoiceBreakdowns)
        {

            //Is the breakdown billed this month? if it is then do all this lot
            if(InvoiceDate.Month() == system.today().month() &&  InvoiceDate.Year() == system.today().year())
            {
                //Check each value to make sure its not null as that blows everything up.
                Decimal ContractBreakdownAccrual = b.Contract_Recurring_Service_Breakdown__r.Breakdown_Accrual__c == null ? 0 : b.Contract_Recurring_Service_Breakdown__r.Breakdown_Accrual__c;
                Decimal Unbilled_Usage = b.Unbilled_Usage__c == null ? 0 : b.Unbilled_Usage__c;
                Decimal Banding_Total = b.Banding_Total__c == null ? 0 : b.Banding_Total__c;

                //add the contract accrual to the banding total.
                b.Banding_Total__c  =  Banding_Total + ContractBreakdownAccrual;

                //now set the accrual on the contract break down to zero
                if(b.Contract_Recurring_Service_Breakdown__c != null)
                {
                    System.debug('id = b.Contract_Recurring_Service_Breakdown__c = ' + b.Contract_Recurring_Service_Breakdown__c);
                    //contractBreakdowns.add(new Contract_Recurring_Service_Breakdown__c(id = b.Contract_Recurring_Service_Breakdown__c, Breakdown_Accrual__c = 0));
					MCS_Contract_Invoice_Line_Item_Breakdown_List.add(b);
                }
            }
}
update MCS_Contract_Invoice_Line_Item_Breakdown_List;
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.