You need to sign in to do that
Don't have an account?
chanchal_:)
how do I create opportunities for only success full inserted contacts ? one opportunity for each successfully inserted contact.
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.
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
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.
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.