0

I have below code where I am creating parent object records in a batch class. I need to create the child records of the same on same batch class. Can someone please suggest a way out for this?

Apex Code

if(!MapOfAcctListItem.keyset().contains(key)){
  Account_List_vod__c Alst = new Account_List_vod__c();
                              Alst.Name = ListName;
                              Alst.Icon_Name_vod__c = '1';
                              Alst.OwnerId = Key;
                              InsertActList.add(Alst);  
}

Proper DML successfully inserts the records I checked works fine. But for child records can't as the batch already got executed.

Child records should be like this.

Child records

 Account_List_Item_vod__c Alstitem = new Account_List_Item_vod__c();
      Alstitem.Account_List_vod__c = ____;  // Get newly inserted records here
      Alstitem.Account_vod__c = actID;
      Alstitem.External_ID_vod__c = actID+'-'+key;
                              insAccListItem.add(Alstitem);

DML for parent records

 // Insert Acct Lists
      if(!InsertActList.isEmpty())
            {
                                try
                                {
                                    insert InsertActList;
                                    System.debug('InsertActListSize@'+InsertActList.size());
                                }
                            Catch(Exception Ex)
                            {
                                 system.debug(Ex);
                            }
            }




    // Insert Acct Lists : Completed
7
  • what do you mean by batch already got executed? can you post the code where child records are created?
    – Preya
    Commented Jan 19, 2017 at 11:09
  • I mean when running the batch class i am not getting the master record's ID since DML is pending. So i couldn't create the child records. Commented Jan 19, 2017 at 11:12
  • can you post the code for inserting both master and child using batch class?
    – Preya
    Commented Jan 19, 2017 at 11:13
  • I couldn't code for the child having newly inserted Ids. I am updating my question on how I intend to. Commented Jan 19, 2017 at 11:15
  • @PreyaMohandoss Please find my updated question. Commented Jan 19, 2017 at 11:21

1 Answer 1

1

Try this way.

if(!InsertActList.isEmpty()){
    Database.SaveResult[] srList = Database.insert(InsertActList, false);

    for (Database.SaveResult sr : srList) {
        if (sr.isSuccess()) {
             Account_List_Item_vod__c Alstitem = new   Account_List_Item_vod__c();
             Alstitem.Account_List_vod__c = sr.getId();  
             Alstitem.Account_vod__c = actID;
             Alstitem.External_ID_vod__c = actID+'-'+key;
             insAccListItem.add(Alstitem);
        }
    }
    try {
        INSERT insAccListItem;
    } 
    catch(Exception e) {
        system.debug(e.getMessage());
    }
}
2
  • Can I get the ownerId value of sr iteration? Commented Jan 19, 2017 at 11:54
  • 1
    You won't get ownerId. You need a separate query for that.
    – Reshma
    Commented Jan 19, 2017 at 12:09

You must log in to answer this question.

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