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
  • what do you mean by batch already got executed? can you post the code where child records are created? – Preya Jan 19 '17 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. – Testing_SFDC Jan 19 '17 at 11:12
  • can you post the code for inserting both master and child using batch class? – Preya Jan 19 '17 at 11:13
  • I couldn't code for the child having newly inserted Ids. I am updating my question on how I intend to. – Testing_SFDC Jan 19 '17 at 11:15
  • @PreyaMohandoss Please find my updated question. – Testing_SFDC Jan 19 '17 at 11:21
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());
    }
}
| improve this answer | |
  • Can I get the ownerId value of sr iteration? – Testing_SFDC Jan 19 '17 at 11:54
  • 1
    You won't get ownerId. You need a separate query for that. – Reshma Jan 19 '17 at 12:09

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.