You need to sign in to do that
Don't have an account?
raz r
Test class covers only 66% . Please help to cover 100%
Trigger:
trigger RestrictContactByName on Contact (before insert, before update) {
//check contacts prior to insert or update for invalid data
For (Contact c : Trigger.New) {
if(c.LastName == 'INVALIDNAME') { //invalidname is invalid
c.AddError('The Last Name "'+c.LastName+'" is not allowed for DML');
}
}
}
Test class:
@istest
public class TestRestrictContactByName {
public static testmethod void testcontact1(){
list<contact> cont=new list<contact>();
contact con =new contact(lastname='testttt');
cont.add(con);
try{
insert con;
}
catch(System.DmlException e){
system.assert(e.getmessage().contains('The lastname testttt is not allow for DML'));
}
}
public static testmethod void testcontact2(){
list<contact> conn=new list<contact>();
contact con =new contact(lastname='testtttt');
conn.add(con);
try{
insert conn;
}
catch(System.DmlException e){
//system.assert(e.getmessage().contains('The lastname testtttt is not allow for DML'));
}
conn.get(0).lastname='Invaliddata';
try{
update conn;
}
catch(System.DmlException e){
system.assert(e.getmessage().contains('The lastname Invaliddata is not allow for DML'));
}
}
}
trigger RestrictContactByName on Contact (before insert, before update) {
//check contacts prior to insert or update for invalid data
For (Contact c : Trigger.New) {
if(c.LastName == 'INVALIDNAME') { //invalidname is invalid
c.AddError('The Last Name "'+c.LastName+'" is not allowed for DML');
}
}
}
Test class:
@istest
public class TestRestrictContactByName {
public static testmethod void testcontact1(){
list<contact> cont=new list<contact>();
contact con =new contact(lastname='testttt');
cont.add(con);
try{
insert con;
}
catch(System.DmlException e){
system.assert(e.getmessage().contains('The lastname testttt is not allow for DML'));
}
}
public static testmethod void testcontact2(){
list<contact> conn=new list<contact>();
contact con =new contact(lastname='testtttt');
conn.add(con);
try{
insert conn;
}
catch(System.DmlException e){
//system.assert(e.getmessage().contains('The lastname testtttt is not allow for DML'));
}
conn.get(0).lastname='Invaliddata';
try{
update conn;
}
catch(System.DmlException e){
system.assert(e.getmessage().contains('The lastname Invaliddata is not allow for DML'));
}
}
}
All Answers
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Last Name "INVALIDNAME" is not allowed for DML: []