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
sree prasadsree prasad 

hi Triggeto batch class

I got Limit 50001 Exception so, i need to be clear through batch apex.so how can i call this class from batch apex 

    trigger LeadTrigger on Lead (before insert) {
    final String errMsg = ' Email already exists ';
    final String errMsg1 = 'Phone number already exists ';
    Map< String, Lead > duplicatePhoneLeadMap = new Map< String, Lead >();
    Map< String, Lead > duplicateEmailLeadMap = new Map< String, Lead >();
    for( Lead l : [select Id,Name, Email,Phone from Lead] ){
        if( l.Email!= null){
            duplicateEmailLeadMap.put(l.email,l);
        }
        if( l.Phone != null){
            duplicatePhoneLeadMap.put(l.Phone,l);
        }
    }
    for( Lead l : Trigger.new ){
        if(duplicateEmailLeadMap.containsKey(l.Email)){
            l.Email.addError(errMsg);
        }
        if(!duplicatePhoneLeadMap.isEmpty() && duplicatePhoneLeadMap.containsKey(l.Phone) && duplicatePhoneLeadMap.get(l.Phone)!=NULL ){
            l.Phone.addError(errMsg1);
        }
    }
}
AshishkAshishk
You can do it with validation rule, use "vlookup", no need of trigger.

https://success.salesforce.com/answers?id=90630000000gyFzAAI

Hope this works.
Thanks,
Ashish
Raj VakatiRaj Vakati
You need to do it like below .. this code is not compleley bulkfied
 
trigger LeadTrigger on Lead (before insert) {
    final String errMsg = ' Email already exists ';
    final String errMsg1 = 'Phone number already exists ';
    Map< String, Lead > duplicatePhoneLeadMap = new Map< String, Lead >();
    Map< String, Lead > duplicateEmailLeadMap = new Map< String, Lead >();
	
	Set<String> emails = new Set<String>() ;
		Set<String> phone = new Set<String>() ;

	for( Lead l :Trigger.new){
		emails.add(l.Email);
		phone.add(l.Phone);
	}
	
    for( Lead l : [select Id,Name, Email,Phone from Lead where Email IN :emails] ){
        if( l.Email!= null){
            duplicateEmailLeadMap.put(l.email,l);
        }
        if( l.Phone != null){
            duplicatePhoneLeadMap.put(l.Phone,l);
        }
    }
	for( Lead l : [select Id,Name, Email,Phone from Lead where Phone IN :phone] ){
        if( l.Phone != null){
            duplicatePhoneLeadMap.put(l.Phone,l);
        }
    }
    for( Lead l : Trigger.new ){
        if(duplicateEmailLeadMap.containsKey(l.Email)){
            l.Email.addError(errMsg);
        }
        if(!duplicatePhoneLeadMap.isEmpty() && duplicatePhoneLeadMap.containsKey(l.Phone) && duplicatePhoneLeadMap.get(l.Phone)!=NULL ){
            l.Phone.addError(errMsg1);
        }
    }
}

 
sree prasadsree prasad
No Actually i wrote this one also.It limit is exceeds alredy.
Now i want to write Through batch class only to overcome this problem ...So please could you know how can we call batch class from trigger