1

I would like to update parent(ExpenseChild__c.EC_Pick__c) and the child record's(ExpenseGChild__c.EGC_Text__c) fields with the string value.

I would like to finish the DML on Child and then move on to Parent

This is what I have come up so far and I am able to update the child records successfully.

public class CascadeUpdate {
    
    public static void Cascade(String s)
    {
        System.debug('Input given '+ s);
               
       
        List<ExpenseGChild__c> egc = [SELECT Id,EGC_Text__c,ExpenseChild__r.EC_Pick__c FROM ExpenseGChild__c];
        
        for(ExpenseGChild__c egci : egc)
        {
        egci.EGC_Text__c = s;
        
        }
        update egc;
        
                                                                     
        
        
    }

}

Can someone tell me how can I update the parent further ?.

My issue is unable to think of way of building a SOQL that can be used to intialize the parent collection (ExpenseChild__c)

EDIT

@Torsen

I made some changes and I was able to solve my problem of updating parent & child. Do take a look and let me know if something looks silly.

public class CascadeUpdate {
    
    public static void Cascade(String s)
    {
        System.debug('Input given '+ s);
               
        List<ExpenseChild__c> ec = new List<ExpenseChild__c>();
        List<ExpenseGChild__c> egc = [SELECT Id,EGC_Text__c,ExpenseChild__c FROM ExpenseGChild__c];
        
        for(ExpenseGChild__c egci : egc)
        {
        egci.EGC_Text__c = s;
        ec.add(new ExpenseChild__c(Id = egci.ExpenseChild__c,EC_Pick__c = s));
        
        }
        update egc;
        update ec;

                                                                     
        
        
    }

}

1 Answer 1

2

Your soql are retriving field from the parent and you are updating the same list. SOQL dont like this. Your best solucion is update 2 list child records and parent record.

Can you try this?

public class CascadeUpdate {

    public static void Cascade(String s)
    {
        System.debug('Input given '+ s);

        Set<ExpenseChild__c> parentExpenses = new Set<ExpenseChild__c>();
        List<ExpenseGChild__c> egc = [SELECT Id,EGC_Text__c,ExpenseChild__c FROM ExpenseGChild__c];

        for(ExpenseGChild__c egci : egc)
        {
            egci.EGC_Text__c = s;

            parentExpenses.add(new ExpenseChild__c(Id = egc.ExpenseChild__c, EC_Pick__c = s ));
        }

        update egc;
        update new List<ExpenseChild__c>(ExpenseChild__c);


    }
}
5
  • Just curious..is your last statement right ?..update new List<ExpenseChild__c>(ExpenseChild__c);
    – Varun
    Jan 9, 2015 at 9:17
  • 1
    Cant we use something like "update parentexpenses;"
    – Varun
    Jan 9, 2015 at 9:17
  • I think that i cant update a Set, and have to ve a List because that i my last statement, i am converting from a Set to a List
    – Torsen
    Jan 9, 2015 at 9:20
  • I was able to solve the problem via another method..Can you take a look and let know if that looks fine...I have updated the original question
    – Varun
    Jan 9, 2015 at 9:23
  • i correct your ExpenseGChild__c query, carefull with your List<ExpenseChild__c> ec = new List<ExpenseChild__c>(); if you have childs with the same parent you are putting in the list the same record, and you will have troubles updating the parens. Because this i use a set of ExpenseChild__c
    – Torsen
    Jan 9, 2015 at 9:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .