• Pavan Kumar S 9
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi All,

I have written a batch class with the below scenario.
if (ObjectName==Account)
{ need to query account  records
}
else if (ObjectName =Contact )
{ Query Contact records)
}
So i will get either account or contact records in scope and in execute method i will create records for other custom object means if it is account it will insert with account Id in the new custom object and if Contact contact Id in the new object .Right now it is captuirng the Id into another custom Object but the problem is whenever i am executing it is capturing the same .

So here i want to eliminate the creation of duplicates means if the Record Id(Acc or con Id already in the new custom object then it should not create new record if the record is not existing then create new record with the Acc or Con Id

My Code:
  global void execute(Database.BatchableContext BC, List<SObject> scope)
    { 
        Map<id, Custom Object> CustomMap= new Map<id,Custom Map>();
         for(Sobject obj: scope)
         {      
                Custom Object cm= new Custom Object();
                 
                 cm.Recordt_ID__c =obj.Id;
                 
                  CustomMap.put(Cm.Object_ID__c, cm);
             
          }
          if(!CustomMap.isEmpty())
        {
            Database.SaveResult[] InsertResult = Database.insert(CustomMap.values(),false);
            
        }
        
    }

So here how i will check if there is already a record with same Object Id is there then it should not insert if it is not there it should create new custom object Record.Any one please guide how to do this

Thanks in advance