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
Sana123Sana123 

add contacts to only the succesfull account as i am passing two arguments 1 List<Account> and 2 List<contact> in my method. Here is my code

 public static void searchForAccount (List<Account>listOfAccount , List<Contact>listOfContact){
         
                // List<Account>listOfAccount = new List<Account>();
               //  List<Contact>listOfContact = new List<Contact>();      
              
         
            Database.SaveResult[] srList = Database.insert(listOfAccount, false);
                 for (Database.SaveResult sr : srList) {
            if (sr.isSuccess()) {
                
               //listOfContact.add(sr.AccountId);  
                Database.insert(listOfContact , false);
                
            } 
               else 
               {
                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());                    
                    Error_Log__c a1 = new Error_Log__c(Parent_Record_Name__c = '' , Parent_Record_Id__c = '' ,Error_Details__c = err.getStatusCode() + ': ' + err.getMessage());
                    database.upsert(a1 , false);
             }
               }
                 }
                   system.debug(listOfContact);
     }
}
AbhinavAbhinav (Salesforce Developers) 
Hi Anu,

It seems your question is not clear, Please frame it more clarity .

Thanks!
Sana123Sana123
Hlo Abhinav , as you see in my code i am taking two arguments one is of list of contact and the other is of account . Simply in my code i am doing to upsert recors of account and during this if any succes or failure occur in populating of account records than the failure records save to the custom object i created in my database like in this code is doing
 if (sr.isSuccess()) {
                
               //listOfContact.add(sr.AccountId);  
                Database.insert(listOfContact , false);
                
            } 
               else 
               {
                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());                    
                    Error_Log__c a1 = new Error_Log__c(Parent_Record_Name__c = '' , Parent_Record_Id__c = '' ,Error_Details__c = err.getStatusCode() + ': ' + err.getMessage());
                    database.upsert(a1 , false);
but the problem which i am facing is that i have to add contact to only those account which are going to success ..how can i add the contacts by passing the contact and account as a argument..
Kindly help me in this