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
chanchal_:)chanchal_:) 

record create after database.insert operation.

global class Batch_AddConToAcc implements Database.Batchable <sObject> {
    
    
    global Database.QueryLocator start(Database.BatchableContext bc) {
        String query = 'SELECT Id, Name FROM Account WHERE Id NOT IN(SELECT AccountId FROM Contact)';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc,List<Account> batch) {
        List<contact> lstCon = new List<Contact>();

        for (Account a : batch) {
            Contact c =  new Contact();
            c.LastName = a.Name;
            c.AccountId = a.Id;
            lstCon.add(c);
        }
        Database.SaveResult[] conList = Database.insert(lstCon, false);
            for (Database.SaveResult sr : conList) {
                if (sr.isSuccess()) {
                    // Operation was successful, so get the ID of the record that was processed
                    System.debug('Successfully inserted contact. contact ID: ' + sr.getId());
                }
                else {
                    // Operation failed, so get all errors                
                    for(Database.Error err : sr.getErrors()) {
                        System.debug('The following error has occurred.');                    
                        System.debug(err.getStatusCode() + ': ' + err.getMessage());
                        System.debug('contact fields that affected this error: ' + err.getFields());
                    }
                }
            }
    }
    
    global void finish(Database.BatchableContext bc) {
        //Do Nothing.
    }
}

how do I create opportunities for only success full inserted contacts ? one opportunity for each successfully inserted contact. 
RituSharmaRituSharma
Add logic after if (sr.isSuccess()) statement to create an opportunity and add to the list. After closure of for (Database.SaveResult sr : conList) loop do the DML using the list.
chanchal_:)chanchal_:)
@RituSharma

on which list do I perform DML ? 
conList will have all (succeed or failed) records.
I dont want to do another query.

I want to create opportunity in which I can write
opportunity.description = contact.LastName
RituSharmaRituSharma
If you need to populate opportunity description as LastName and you don't want to query successfully inserted contacts then after closure of for (Database.SaveResult sr : conList) loop, loop on lstCon. If contact is inserted then it will have id for the contact else Id will be null. This is SF standard behaviour, if record is inserted then it automatically adds the id to each element of the list.

So prepare a map using lstCon list where contact id is not null. Use key as the contact id and value if the corresponding contact record. You can then use this map to create the opportnities.
chanchal_:)chanchal_:)

First error: Invalid id:


I get this error 
because records which failed don't have Id as coloumn. so for those I get this error and again batch process failed.