You need to sign in to do that
Don't have an account?
David Vickers 44
Create a unit test for a simple Apex trigger How do i access the getmessages() correctly?
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?
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()); } }
Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000BNjqIAG
2) https://developer.salesforce.com/forums/?id=906F0000000BQwoIAG
you can try below code
Sample code 2:-
Let us know if this will help you
Thanks
Amit Chaudhary
All Answers
The way that I completed this challenge, was to test the save result was equal to the error message exactly which is outputted there - because it is custom and you know what it is - and it doesn't appear to be a "proper" error standard message such as "can't delete related xyz" or "can't insert because there are required fields" something like this...
I think this information will help you complete the challenge. But you may want to try and test the same code when checking for a standard error message.
Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000BNjqIAG
2) https://developer.salesforce.com/forums/?id=906F0000000BQwoIAG
you can try below code
Sample code 2:-
Let us know if this will help you
Thanks
Amit Chaudhary
A little differnt to yours but thanks for line 14 in your sample code 2 that was what I was looking for
@isTest
public class TestRestrictContactByName {
@isTest static void testName(){
Contact con = new Contact(FirstName='XYZ', LastName='INVALIDNAME');
insert con;
}
}