• David Vickers 44
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I am a beginner with coding and looking for help

The code I have below works for getting 100% coverage and passes the trailhead evaluation however I also wanted to assert that the Error message is correct.

because I wanted to set up all test data outside of the starttest() and I wanted to check and valid insert as well as a invalid one I need to write the saveresults to a list. Everything works fine untill i tried to access the  error messages.
I have played with every variation but cannot figure out the syntax

system.debug(r1.getErrors());  gives me  DEBUG|(Database.Error[getFields=();getMessage=The Last Name "INVALIDNAME" is not allowed for DML;getStatusCode=FIELD_CUSTOM_VALIDATION_EXCEPTION;])

system.debug(r1.getErrors().getMessage);  doesn't compile " Initial term of field expression must be a concrete SObject: List<Database.Error>"
system.debug(r1.getErrors().getMessage()); doesn't  Method does not exist or incorrect signature: [List<Database.Error>].getmessage()

And so my tries Have gone on!

how do I get the getMessage field from the getErrors?

 
@istest
private class TestRestrictContactByName {
    @isTest Static void TestRestictedname(){
        List<contact> testcontacts = New List<contact>();
        contact c0 = new contact(LastName = 'Testcontact');
        testcontacts.add(c0);
        Contact c1 = new contact(LastName = 'INVALIDNAME');
        testcontacts.add(c1);
        Test.startTest(); 
        Database.SaveResult[] srList = Database.insert(testcontacts, false); 
        Test.stopTest();
        Database.SaveResult R0 = srlist[0];
        Database.SaveResult R1 = srlist[1];
        
        System.assert(r0.isSuccess());
        System.assert(!r1.isSuccess());
                      
                      }
                      }

 
I am a beginner with coding and looking for help

The code I have below works for getting 100% coverage and passes the trailhead evaluation however I also wanted to assert that the Error message is correct.

because I wanted to set up all test data outside of the starttest() and I wanted to check and valid insert as well as a invalid one I need to write the saveresults to a list. Everything works fine untill i tried to access the  error messages.
I have played with every variation but cannot figure out the syntax

system.debug(r1.getErrors());  gives me  DEBUG|(Database.Error[getFields=();getMessage=The Last Name "INVALIDNAME" is not allowed for DML;getStatusCode=FIELD_CUSTOM_VALIDATION_EXCEPTION;])

system.debug(r1.getErrors().getMessage);  doesn't compile " Initial term of field expression must be a concrete SObject: List<Database.Error>"
system.debug(r1.getErrors().getMessage()); doesn't  Method does not exist or incorrect signature: [List<Database.Error>].getmessage()

And so my tries Have gone on!

how do I get the getMessage field from the getErrors?

 
@istest
private class TestRestrictContactByName {
    @isTest Static void TestRestictedname(){
        List<contact> testcontacts = New List<contact>();
        contact c0 = new contact(LastName = 'Testcontact');
        testcontacts.add(c0);
        Contact c1 = new contact(LastName = 'INVALIDNAME');
        testcontacts.add(c1);
        Test.startTest(); 
        Database.SaveResult[] srList = Database.insert(testcontacts, false); 
        Test.stopTest();
        Database.SaveResult R0 = srlist[0];
        Database.SaveResult R1 = srlist[1];
        
        System.assert(r0.isSuccess());
        System.assert(!r1.isSuccess());
                      
                      }
                      }

 
I am a beginner with coding and looking for help

The code I have below works for getting 100% coverage and passes the trailhead evaluation however I also wanted to assert that the Error message is correct.

because I wanted to set up all test data outside of the starttest() and I wanted to check and valid insert as well as a invalid one I need to write the saveresults to a list. Everything works fine untill i tried to access the  error messages.
I have played with every variation but cannot figure out the syntax

system.debug(r1.getErrors());  gives me  DEBUG|(Database.Error[getFields=();getMessage=The Last Name "INVALIDNAME" is not allowed for DML;getStatusCode=FIELD_CUSTOM_VALIDATION_EXCEPTION;])

system.debug(r1.getErrors().getMessage);  doesn't compile " Initial term of field expression must be a concrete SObject: List<Database.Error>"
system.debug(r1.getErrors().getMessage()); doesn't  Method does not exist or incorrect signature: [List<Database.Error>].getmessage()

And so my tries Have gone on!

how do I get the getMessage field from the getErrors?

 
@istest
private class TestRestrictContactByName {
    @isTest Static void TestRestictedname(){
        List<contact> testcontacts = New List<contact>();
        contact c0 = new contact(LastName = 'Testcontact');
        testcontacts.add(c0);
        Contact c1 = new contact(LastName = 'INVALIDNAME');
        testcontacts.add(c1);
        Test.startTest(); 
        Database.SaveResult[] srList = Database.insert(testcontacts, false); 
        Test.stopTest();
        Database.SaveResult R0 = srlist[0];
        Database.SaveResult R1 = srlist[1];
        
        System.assert(r0.isSuccess());
        System.assert(!r1.isSuccess());
                      
                      }
                      }