• sankar Landa 30
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
This is my class

trigger ContactActive on contact(before insert, before update){
    for(contact con : trigger.new){
        if(con.accountId != null){
            //Id a = con.accountId;
            list<contact> cList = [SELECT Id, Active__c FROM Contact where Active__c = true and AccountId =: con.accountId];
            if(cList.size() > 0){
                con.addError('no more primary contacts');
            }
        }
        
    }
}

Test Class
------------------
@isTest
public class TestAccountActive {
     static testmethod void testme(){
         account a = new account();
         a.name = 'Test Name';   
         
         insert a;
         contact c =new contact(lastname = 'siva',Active__c = true,accountid = a.id);       
         try{
          insert c;   
            // Database.SaveResult result = Database.insert(c, false);
         }
        catch(Exception e)
          {
          Boolean expectedExceptionThrown = e.getMessage().contains('My Error Message') ? true : false; 
          System.AssertEquals(expectedExceptionThrown, false );
}
  }
   }
This is my class

trigger ContactActive on contact(before insert, before update){
    for(contact con : trigger.new){
        if(con.accountId != null){
            //Id a = con.accountId;
            list<contact> cList = [SELECT Id, Active__c FROM Contact where Active__c = true and AccountId =: con.accountId];
            if(cList.size() > 0){
                con.addError('no more primary contacts');
            }
        }
        
    }
}

Test Class
------------------
@isTest
public class TestAccountActive {
     static testmethod void testme(){
         account a = new account();
         a.name = 'Test Name';   
         
         insert a;
         contact c =new contact(lastname = 'siva',Active__c = true,accountid = a.id);       
         try{
          insert c;   
            // Database.SaveResult result = Database.insert(c, false);
         }
        catch(Exception e)
          {
          Boolean expectedExceptionThrown = e.getMessage().contains('My Error Message') ? true : false; 
          System.AssertEquals(expectedExceptionThrown, false );
}
  }
   }