+ Start a Discussion
wixxeywixxey 

Test Code for the trigger

Kindly help me in writing the test code for the below trigger, i will be thankful

trigger detectClone on Contact (before insert, after update){
    for(Contact acc : trigger.new){
        if(Trigger.isInsert){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {acc.Email}; 
            mail.setToAddresses(toAddresses);
            mail.setSenderDisplayName('Support Team');
            mail.setSubject('LogIn information');
            mail.setBccSender(false);
            mail.setUseSignature(false);
         
            Double rnd = math.random();
            String str_rnd = String.valueOf(rnd);
            String temp_pass = str_rnd.replace('.','1');
            String pass = temp_pass.substring(1,6);
            
            mail.setHtmlBody('Dear '+acc.LastName+'<br/><br/>'+
            'Welcome to EZQCP! You have been invited to join the EZQCP [Account Name] account team.<br/> '+
            'Your user name is below:<br/><br/>'+
            'User Name: '+ acc.User_Name__c+'<br/>'+
            'Password: '+ pass +'<br/><br/>'+
            ' To accept the invitation and set your username, log in automatically by clicking www.xxxxx.xxxxxx<br/> '+
            'If you experience problems with the above link, please contact support@ezqcp.com for assistance.');
           
           Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });             
           acc.Password__c = pass;     
        }
        else if(Trigger.isUpdate ){
            List<Contact> admin = [Select Id, Type__c From Contact Where AccountId =:acc.AccountId AND
             Type__c = 'Customer Admin' ];
             if(admin.size()<1){
                 acc.addError('Each Account must have atleast one Contact with Admin privileges');
             }
        }
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi,

 

It is custom validation error try below code.

 

Test Class:

@istest
public class TestdetectClone{
    Private Static testmethod void TestdetectClone(){
        Account a=new account();
        a.Name = 'Test Name';
        insert a;
        
        Contact c =new Contact();
        c.lastname='Customer Admin';
        c.Email = 'test@test.com';
        c.accountid=a.id;
        insert c;
        
        c.Email = 'test1@test.com';
        update c;
    }
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

All Answers

hitesh90hitesh90

Hi,

 

Here is the Test class for your trigger.

 

Test Class:

@istest
public class TestdetectClone{
    Private Static testmethod void TestdetectClone(){
        Account a=new account();
        a.Name = 'Test Name';
        insert a;
        
        Contact c =new Contact();
        c.lastname='Customer Admin';
        c.Email = 'test@test.com';
        c.accountid=a.id;
        insert c;
        
        c.lastname='Admin';
        update c;
    }
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

wixxeywixxey

Thanx for giving your time ...The test code got failed generating the following error code

System.DmlException: Update failed. First exception on row 0 with id 0039000000NoBdxAAF; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Each Account must have atleast one Contact with Admin privileges: []

hitesh90hitesh90

Hi,

 

It is custom validation error try below code.

 

Test Class:

@istest
public class TestdetectClone{
    Private Static testmethod void TestdetectClone(){
        Account a=new account();
        a.Name = 'Test Name';
        insert a;
        
        Contact c =new Contact();
        c.lastname='Customer Admin';
        c.Email = 'test@test.com';
        c.accountid=a.id;
        insert c;
        
        c.Email = 'test1@test.com';
        update c;
    }
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

This was selected as the best answer
wixxeywixxey

I have tried the other code but the same error.. thanx for giving your precious time