• Chandana L
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
My Trigger is as following:-

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');
}
}
}

--------------------------------------------------
And my Test class as follows:-
@isTest
private class TestRestrictContactByName
{
    @isTest static void TestContact()
    {

        Contact con=new Contact(FirstName='Arjun', LastName='Mahi');
        if(con.LastName=='INVALIDNAME')
        {
             con.AddError('The Last Name "'+con.LastName+'" is not allowed for DML');
            
        }else{insert con;}  
 
        Contact conn=new Contact(FirstName='Arjun', LastName='Kapur');
         if(conn.LastName=='INVALIDNAME')
        {
             conn.AddError('The Last Name "'+conn.LastName+'" is not allowed for DML');
            
        }else {update conn;}  
       
        Contact com=new Contact(FirstName='Rama', LastName='INVALIDNAME');
        
        if(com.LastName=='INVALIDNAME')
        {
             com.AddError('The Last Name "'+com.LastName+'" is not allowed for DML');
            
        }else {insert conn;}
    }
}


plz help me complete this Challenge :)
Thanx in Advance :)