• Lizzy
  • NEWBIE
  • 35 Points
  • Member since 2016

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
Hi Community

I am new to using Database.update() method and I thought that using the Database class would not throw any kind of Exceptions. I use this as my source information:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database.htm
@TestVisible private void updateExistingOpportunities() {
	list<Database.SaveResult> results = Database.update(existingOpportunityMap.values());
	String errorMessage = '';
	for ( Integer i = 0; i < results.size(); i++ ) {
		if ( !results.get(i).isSuccess() ) {
			if ( errorMessage.equals('') ) {
				errorMessage = 'Following Opportunities have failed during the update:\n\n';
			}
			errorMessage += existingOpportunityMap.values().get(i).Name;
			errorMessage += '\n';
			errorMessage += 'https://eu4.salesforce.com/';
			errorMessage += existingOpportunityMap.values().get(i).Id;
			errorMessage += 'Errors:\n';
			for ( Database.Error theError : results.get(i).getErrors() ) {
				errorMessage += theError.getStatusCode();
				errorMessage += '\n';
				errorMessage += theError.getMessage();
			}
		}
	}
}
The above is my method I like to test. I want to force running through the result loop with an error so i get maximum code coverage and see that the output is as I desired it. I tried it with an inserted Opportunity of which I tried putting the CloseDate on null. But this throws a DML Exception. I thought I would not get any exceptions, but be able to read out the Database.result...

Any hints on that?
Hi Community

I am new to using Database.update() method and I thought that using the Database class would not throw any kind of Exceptions. I use this as my source information:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database.htm
@TestVisible private void updateExistingOpportunities() {
	list<Database.SaveResult> results = Database.update(existingOpportunityMap.values());
	String errorMessage = '';
	for ( Integer i = 0; i < results.size(); i++ ) {
		if ( !results.get(i).isSuccess() ) {
			if ( errorMessage.equals('') ) {
				errorMessage = 'Following Opportunities have failed during the update:\n\n';
			}
			errorMessage += existingOpportunityMap.values().get(i).Name;
			errorMessage += '\n';
			errorMessage += 'https://eu4.salesforce.com/';
			errorMessage += existingOpportunityMap.values().get(i).Id;
			errorMessage += 'Errors:\n';
			for ( Database.Error theError : results.get(i).getErrors() ) {
				errorMessage += theError.getStatusCode();
				errorMessage += '\n';
				errorMessage += theError.getMessage();
			}
		}
	}
}
The above is my method I like to test. I want to force running through the result loop with an error so i get maximum code coverage and see that the output is as I desired it. I tried it with an inserted Opportunity of which I tried putting the CloseDate on null. But this throws a DML Exception. I thought I would not get any exceptions, but be able to read out the Database.result...

Any hints on that?
Hi,
I am new to apex developer and I am having issues test the code I created for one of the modules. Below is the code I am attempting to test.

public class StringArrayTest {
    public static String[] generateStringArray(Integer i){
      String [] tests= new List<String>();
        for(Integer n=0;n<i;n++){
            tests.add('test '+n);
            }
        return tests;
    }
}

I click on run all tests and it states there are no tests. When I try to complete the challenge it tells me there is no class created.
Any help would be great.
Thank you.