function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
naveen pnaveen p 

Error while writing a trigger

Hi Everyone,

i m new to SFDC. I m writing a basic trigger by using handler class for avoiding duplicate values but it is giving an error.
my code is
 trigger AvoidingDuplicateValues1 on Account (Before Insert,Before Update)
{
    DuplicateValue dup=new DuplicateValue();
}

Error: Compile Error: Invalid type: DuplicateValue at line 3 column 28

Pls help.
Best Answer chosen by naveen p
Onesh ReddyOnesh Reddy
Hi Naveen,
Please Re-check the name of your apex class and the name you used in Trigger.
Class name is "DuplicateVaiue" where as in trigger you used "DuplicateValue".
Please find the revised code.
public class DuplicateValue {
    public void dup(List<Account> accList) {
		Set<String> accSet = new Set<String>();
		for(Account acc : accList) {
			accSet.add(acc.Name);
		}
		
		Map<String, Account> existAccMap = new Map<String, Account>();
		
		for(Account acc: [Select Id, Name from Account where Name IN :accset]) {
			existAccMap.put(acc.Name, acc);
		}
        
		for(Account acc : accList) {
			if(existAccMap.containsKey(acc.Name)) {
				acc.adderror('Name already eists');
			}
		}
    }
}


Please let me know if it helps you.

Regards,
Onesh.K

All Answers

Onesh ReddyOnesh Reddy
Hi Naveen,
Your Trigger has no Erors, Can you post the Helper Class.
naveen pnaveen p
Hi Reddy,

Thanks for ur reply..
public class DuplicateVaiue
{
    public void dup(List<Account> TriggerNew)
    {
    Set<String>accSet=new Set<string>();
    for(Account acc:trigger.new)
    {
    accSet.add(acc);
    }
        List<Account>listacc=[select name from account where name in :accset];
        Map<String,Account>mapacc=new Map<String,Account>();
        for(Account objacc:ListofAccount)
        {
            mapacc.put(objacc.name,objacc);
        }
            for(Account objacc:Trigger.new)
            {
                if(mapacc.containskey(objacc.name))
                {
                    objacc.adderror('Nmae already eists');
                }
            }
    }

}

please tell me where i was wrong
Thanks in advance

Naveen
Mahesh DMahesh D
Hi Naveen,

Please find the below code:

Here I handled :

(1) Naming Convention.
(2) Alignment.
(3) Corrected the logic.

 
public class DuplicateVaiue {
    public void dup(List<Account> accList) {
		Set<String> accSet = new Set<String>();
		for(Account acc : accList) {
			accSet.add(acc.Name);
		}
		
		Map<String, Account> existAccMap = new Map<String, Account>();
		
		for(Account acc: [Select Id, Name from Account where Name IN :accset]) {
			existAccMap.put(acc.Name, acc);
		}
        
		for(Account acc : accList) {
			if(existAccMap.containsKey(acc.Name)) {
				acc.adderror('Nmae already eists');
			}
		}
    }
}

Please do let me know if it helps you.

Regards,
Mahesh

 
Onesh ReddyOnesh Reddy
Hi Naveen,
Please Re-check the name of your apex class and the name you used in Trigger.
Class name is "DuplicateVaiue" where as in trigger you used "DuplicateValue".
Please find the revised code.
public class DuplicateValue {
    public void dup(List<Account> accList) {
		Set<String> accSet = new Set<String>();
		for(Account acc : accList) {
			accSet.add(acc.Name);
		}
		
		Map<String, Account> existAccMap = new Map<String, Account>();
		
		for(Account acc: [Select Id, Name from Account where Name IN :accset]) {
			existAccMap.put(acc.Name, acc);
		}
        
		for(Account acc : accList) {
			if(existAccMap.containsKey(acc.Name)) {
				acc.adderror('Name already eists');
			}
		}
    }
}


Please let me know if it helps you.

Regards,
Onesh.K
This was selected as the best answer
naveen pnaveen p
thanks Mahesh and Onesh for ur replies ..it really helped me.Thanks a lot
naveen pnaveen p
Hi Guys,

i am able to restrict the duplicate name entries,but i am unable to edit remaining fiels of the recod means i want to change a particular field value but i am unable to do.
it is throwing an error " NAME AREADY EXISTS"

please advice
Naveen.