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
Saravana RavikumarSaravana Ravikumar 

Opportunities are not being created. How should I modify this code assuming getOpportunities is implemented elsewhere? List<Opportunity> opp = getOpportunuites(); Database.insert(opp,false);

SubratSubrat (Salesforce Developers) 
Hello ,

To modify the code and ensure that opportunities are created successfully, you can make the following adjustments:
 
List<Opportunity> opp = getOpportunities();
List<Database.SaveResult> insertResults = Database.insert(opp, false);

// Check the results of the insert operation
for (Database.SaveResult result : insertResults) {
    if (result.isSuccess()) {
        // Opportunity was successfully inserted
        System.debug('Opportunity created: ' + result.getId());
    } else {
        // Handle the error if the opportunity creation failed
        for (Database.Error error : result.getErrors()) {
            System.debug('Error creating opportunity: ' + error.getStatusCode() + ' - ' + error.getMessage());
        }
    }
}
By implementing these modifications, you can obtain better visibility into the creation process of Opportunities and identify any potential errors that may be preventing successful insertion.

If this helps , please mark this as Best Answer.
Thank you.
Arun Kumar 1141Arun Kumar 1141
Hello Sarvana,

You can try this code,
List<Opportunity> opp = getOpportunities(); 

if (opp != null && !opp.isEmpty()) {
    try {
        Database.SaveResult[] saveResults = Database.insert(opp, false);

        // Process the save results
        for (Database.SaveResult saveResult : saveResults) {
            if (saveResult.isSuccess()) {
              system.debug('created');
                
            } else {
                for (Database.Error error : saveResult.getErrors()) {
                    System.debug('Error creating Opportunity: ' + error.getStatusCode() + ' - ' + error.getMessage());
                }
            }
        }
    } catch (Exception e) {
        System.debug('Exception occurred while creating Opportunities: ' + e.getMessage());
    }
} else {
    System.debug('No Opportunities to create.');
}

You might catch the error if the record is not created.

Thanks.
sravani proddatur 10sravani proddatur 10
Hi Saravana,
You can try this code..It will help you..

List<Opportunity> opp = getOpportunities();

List<Database.SaveResult> insertResults = Database.insert(opp, false); 
 for (Database.SaveResult saveresult : insertResults) {    
       if (saveresult .isSuccess()) {      
           //  successfully inserted        
           System.debug('Opportunity created: ' + saveresult .getId());  
      } else {         
                for (Database.Error error : result.getErrors()) {            
                     System.debug('Error creating opportunity: ' + error.getStatusCode() );
                     System.debug('Error Message:' + error.getMessage());       
                }    
        }
}


If this code will Helps you...then mark as the best answer.
Thanks.