+ Start a Discussion
Giri Kumar BGiri Kumar B 

Duplicate name not allowed i am write trigger . i am try to write test class can you please how to start write

class
--------
public class duplicateerrormsg {
    public static void mains(list<account> acc){
        list<string> a = new list<string>();
        set<string> s = new set<string>();
        for(account at: acc){
            a.add(at.name);
        }
        list<account> acs = [select name from account where name in:a];
        for(account a2:acs){
            s.add(a2.name);
        }
        for(account a3:acc){
            a3.name.adderror('this record has implument ');
        }
    }
}
 
cl0s3rcl0s3r
Verify that you dont already have a class by the name of "duplicateerrormsg" in your org...
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to learn about test classes
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

I hope you are calling above class from account trigger. Then try below code.
@isTest 
public class duplicateerrormsgTest 
{
    static testMethod void testMethod1() 
	{
		// Perform DML here only
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;
		
		Test.startTest();
		
			Account testAccount1 = new Account();
			testAccount1.Name='Test Account' ;
			try
			{
				insert testAccount1;
			}
			Catch(Exception ee)
			{}
		Test.stopTest();
	}
}

If not then try below code
 
@isTest 
public class duplicateerrormsgTest 
{
    static testMethod void testMethod1() 
	{
		// Perform DML here only
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;
		
		Test.startTest();
		
			Account testAccount1 = new Account();
			testAccount1.Name='Test Account' ;

			list<Account> lstAcc = new List<Account>();
			lstAcc.add(testAccount1);	
			
			try
			{
				duplicateerrormsg.mains(lstAcc);
			}
			Catch(Exception ee)
			{}
		Test.stopTest();
	}
}

Let us know if this will help you

 
Giri Kumar BGiri Kumar B
amit both test class is not working they are showing like thisUser-added image
 
Giri Kumar BGiri Kumar B
 Amith Gi i am write a class . duplicate not allowed in this senarious concept throw on trigger  .  i am not understand the test class. you are not the refered the class name instancely . previous senario is you refered to class name instancely . on this class why you not refered the class name . can you please elaborate . me sir 
Amit Chaudhary 8Amit Chaudhary 8
Can you please share the screen shot of your error ?
Giri Kumar BGiri Kumar B
line 2, column 1: Method does not exist or incorrect signature: duplicateerrormsg.main(List<Account>)
Giri Kumar BGiri Kumar B
User-added image
this is the error
 
Amit Chaudhary 8Amit Chaudhary 8
It should be duplicateerrormsg.mains(List<Account>)

not duplicateerrormsg.main(List<Account>)

Please add s