• PankajJha
  • NEWBIE
  • 5 Points
  • Member since 2019
  • Salesforce Developer Trainee

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 1
    Replies
Hi everyone,
I'm new in salesforce. if you know plz reply 
how to merge duplicate lead with batch apex based on email
Actually i'm unable to merge chatter feed, and fields like name, title, company, fax etc.
plz reply in code becoz if you provide me link and i try, again the same problem occurs. not able to merge fields. i posting second time this question because someone replyed with link and that is not working, and when i asked again i got no reply. 
thanks in advance.


global class LeadBatchAviodDuplicacy implements Database.Batchable<sObject> {

public String queryString;

global Database.QueryLocator start(Database.BatchableContext BC) {
     
  queryString = 'SELECT Id, Name, Email, Phone, Fax, Company FROM Lead ';
    return Database.getQueryLocator(queryString);
}
  global void execute(Database.BatchableContext BC, List<Lead> obj) {

List<Lead> leads = [Select Id, Email, Phone, Fax, Company, Name, (Select Id, WhoId from Tasks), (Select Id, WhoId from Events) from Lead ORDER BY CreatedDate];

Map<String,List<Lead>> leadsMap = new Map<String,List<Lead>>();

for( Lead l : leads ) {
    if( !leadsMap.containsKey( l.Email ) ) {
        leadsMap.put( l.Email, new List<Lead>() );
    }

    leadsMap.get( l.Email ).add( l );
}

List<Event> eventsToUpdate = new List<Event>();
List<Task> tasksToUpdate = new List<Task>();
List<Lead> leadsToDelete = new List<Lead>();


for( String email : leadsMap.keySet() ) {
    List<Lead> leadsToMerge = leadsMap.get( email );
    if( leadsToMerge.size() > 1 ) {
        Lead masterLead = leadsToMerge[0];

        for( Integer i = 1; i < leadsToMerge.size(); i++ ) {
            Lead mergedLead = leadsToMerge[i];
            for( Task t : mergedLead.Tasks ) {
                t.WhoId = masterLead.Id;
                tasksToUpdate.add( t );
            }

            for( Event e : mergedLead.Events ) {
                e.WhoId = masterLead.Id;
                eventsToUpdate.add( e );
            }
            leadsToDelete.add( mergedLead );
        }

    }
}

if( eventsToUpdate.size() > 0 ) update eventsToUpdate;
if( tasksToUpdate.size() > 0 ) update tasksToUpdate;
if( leadsToDelete.size() > 0 ) delete leadsToDelete;
}

global void finish(Database.BatchableContext BC) {

}
}
Hi friends,
I am new  in salesforce, if you know plz reply.
thanks in advance.

how to merge duplicate lead with batch apex based on email ?

global class LeadBatchAviodDuplicacy implements Database.Batchable<sObject> {

public String queryString;

global Database.QueryLocator start(Database.BatchableContext BC) {
     
  queryString = 'SELECT Id, Name, Email FROM Lead ';
    return Database.getQueryLocator(queryString);
}
  global void execute(Database.BatchableContext BC, List<Lead> obj) {

Map<String,List<Lead>> LeadMap = new Map<String,List<Lead>>();
  Set<String> LeadName = new Set<String>();
  Set<String> LeadEmail = new Set<String>();
  

  for(Lead a :obj){
     LeadName.add(a.Name.toLowerCase());
      if(String.isnotblank(a.Email))
     LeadEmail.add(a.Email.toLowerCase());
     
  }

  //Get All non formula fields of Lead
  String sobjectApiName = 'Lead';
  Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
  Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
  List<String> fieleList = new List<String>();
  for(String fieldName : fieldMap.keySet()){
     Schema.DescribeFieldResult fieldResult = fieldMap.get(fieldName).getDescribe();
     if(!fieldResult.isCalculated() && fieldResult.isCreateable() && !fieldResult.isExternalId()&& fieldResult.isUpdateable()){
        fieleList.add(fieldName);
     }
     /*if(fieldName != 'isPartner' && fieldName != 'IsCustomerPortal'){
        fieleList.add(fieldName);
     }*/
  }

  String fetchFields = 'SELECT '+String.join(fieleList,',')+' FROM '+sobjectApiName+' WHERE Email IN :LeadEmail';
  integer i = 1;
  for(Lead a : Database.query(fetchFields)){
     String key = a.Email;
     key = key.toLowerCase();
     if(LeadMap.containsKey(key)){
        LeadMap.get(key).add(a);
     }else {
        LeadMap.put(key,new List<Lead>{a});
     }
     i++;
  }
  System.debug('i >'+i);
System.debug('LeadMap >> '+LeadMap);
  List<Lead> mergeList;
for(String key : LeadMap.keySet()){
     System.debug('Size >>'+ LeadMap.get(key).size());
    if(LeadMap.get(key).size()>1 ){
        mergeList = new List<Lead>();
        Lead masterLead = LeadMap.get(key)[0];
        mergeList.addAll(LeadMap.get(key));
        mergeList.remove(0);
        System.debug('mergeList >> '+mergeList);
        for(Lead a : mergeList){
            Database.MergeResult mergeResult = Database.merge(masterLead, a, true);
        } 
    }
}

//if(mergeList.size()>0)
//  update mergeList;

}

global void finish(Database.BatchableContext BC) {

}
}




I'm unable to map fields and feed fields. 
Hi Friends, I am new to integration and my task is
How to create webhooks in salesforce ?
thanks in advance.
how to show account related opportunity releted product unique record count on account object with trigger.

account -> opportunity -> product

thanks in advance.
Hi friends,
I am new  in salesforce, if you know plz reply.
thanks in advance.

how to merge duplicate lead with batch apex based on email ?

global class LeadBatchAviodDuplicacy implements Database.Batchable<sObject> {

public String queryString;

global Database.QueryLocator start(Database.BatchableContext BC) {
     
  queryString = 'SELECT Id, Name, Email FROM Lead ';
    return Database.getQueryLocator(queryString);
}
  global void execute(Database.BatchableContext BC, List<Lead> obj) {

Map<String,List<Lead>> LeadMap = new Map<String,List<Lead>>();
  Set<String> LeadName = new Set<String>();
  Set<String> LeadEmail = new Set<String>();
  

  for(Lead a :obj){
     LeadName.add(a.Name.toLowerCase());
      if(String.isnotblank(a.Email))
     LeadEmail.add(a.Email.toLowerCase());
     
  }

  //Get All non formula fields of Lead
  String sobjectApiName = 'Lead';
  Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
  Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
  List<String> fieleList = new List<String>();
  for(String fieldName : fieldMap.keySet()){
     Schema.DescribeFieldResult fieldResult = fieldMap.get(fieldName).getDescribe();
     if(!fieldResult.isCalculated() && fieldResult.isCreateable() && !fieldResult.isExternalId()&& fieldResult.isUpdateable()){
        fieleList.add(fieldName);
     }
     /*if(fieldName != 'isPartner' && fieldName != 'IsCustomerPortal'){
        fieleList.add(fieldName);
     }*/
  }

  String fetchFields = 'SELECT '+String.join(fieleList,',')+' FROM '+sobjectApiName+' WHERE Email IN :LeadEmail';
  integer i = 1;
  for(Lead a : Database.query(fetchFields)){
     String key = a.Email;
     key = key.toLowerCase();
     if(LeadMap.containsKey(key)){
        LeadMap.get(key).add(a);
     }else {
        LeadMap.put(key,new List<Lead>{a});
     }
     i++;
  }
  System.debug('i >'+i);
System.debug('LeadMap >> '+LeadMap);
  List<Lead> mergeList;
for(String key : LeadMap.keySet()){
     System.debug('Size >>'+ LeadMap.get(key).size());
    if(LeadMap.get(key).size()>1 ){
        mergeList = new List<Lead>();
        Lead masterLead = LeadMap.get(key)[0];
        mergeList.addAll(LeadMap.get(key));
        mergeList.remove(0);
        System.debug('mergeList >> '+mergeList);
        for(Lead a : mergeList){
            Database.MergeResult mergeResult = Database.merge(masterLead, a, true);
        } 
    }
}

//if(mergeList.size()>0)
//  update mergeList;

}

global void finish(Database.BatchableContext BC) {

}
}




I'm unable to map fields and feed fields. 
Hi Friends, I am new to integration and my task is
How to create webhooks in salesforce ?
thanks in advance.