0

I have CS_Order__c(parent) and cs_Order_Line_Item__c (child) objects. I need to update CS_Order__c field ,but its not updating the parent record. CSOrderMap=

public class testUpdate {
    public static void UpdateParent(Map<Id, String> CSOrderMap) {
        Set<Id> orderIds = new Set<Id>();

        List<cs_Order_Line_Item__c> ordLineitem = [SELECT Id, Name,CS_Order__r.Fullfilment_Status__c FROM cs_Order_Line_Item__c WHERE CS_Order__c IN :CSOrderMap.keySet()];
        if(!ordLineitem.isEmpty()){
            for(cs_Order_Line_Item__c sc: ordLineitem){
                sc.CS_Order__r.Reason__c='Banned';
               sc.Billing_Status__c='Error';
            }

            update ordLineitem;
        }   
    }
}

Please suggest.

I dont want to create a new instance and a new query as well.

CS_Order__c csord=new CS_Order__c();
    csord.Reason__c='Banned';
    csord.id= csord.CS_Order__r.id;
    ordslist.add(acc);

update ordLineitem;
update ordslist;

but i dont want to create a new instance. Please suggest.

  • Please Take a look at provided link, your code will look similar . Link1 Link2 – Amol Dec 19 '17 at 9:45
1

First of all, it isn't required to do the check for an empty list:

if(!ordLineitem.isEmpty()){ ... }
  1. If the list if empty - foreach won't try to loop,
  2. If you run DML on an empty list of records (update ordLineitem;) - it won't perform any database action.

Due to your question - CS_Order__r record on every cs_Order_Line_Item__c is an independent entity and when you run update ordLineitem; there is no impact on CS_Order__r related parent record.

As you mentioned in your question, you could create an instance of CS_Order__c record and accumulate a new list. And when you are ready with a business logic, you could create a generic list to update all record in one DML operation:

// ordLineitems - list of cs_Order_Line_Item__c
// ordslist - list of CS_Order__c
List<SObject> recordsToUpdate = new List<SObject>(ordLineitems);
recordsToUpdate.addAll(ordslist);
update recordsToUpdate;
  • is this possible without creating an instance of order. – sandy Dec 19 '17 at 9:29
  • @aditi basically, when you refer to an order as sc.CS_Order__r in the loop, you get an instance of order. So you could use this instance to add to orders list and then update it as I mention above. – Ruslan Kurchenko Dec 19 '17 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.