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
Fatfox FFatfox F 

Nature of Data loader operations and trigger?

I have following queries:

1.) I'll be inserting records in a custom object (let's say ObjectA) using data loader. Majority of the email addresses in the csv sheet (which I'll be using for data loading) are already present in the object. This custom object can only have unique email address record. So my doubt is how will the insert operation proceed? Will the missing email address records be created in the custom object uninterrupted? (I'm aware that I'll be getting a "success" and "error" files in the end. So in my case, majority of the records will be seen in "error" file, right?)

2.) I'm also writing a before insert trigger on ObjectA. In this trigger, I've put all the records' email address which will be inserted through data loader in a Set called "NewSet" (through trigger.new). I've fetched all the records of ObjectA in a list (say ListA). Now, I've compared the email address field in this listA with NewSet (through NewSet.contains method).
The ones that are missing in the set, I've retrieved "Id" of those records in another list "todelete". And I'll be deleting them by statement "delete todelete".

So will this delete statement work in the same before insert trigger?? Also, will both the scenarios work altogether??

Thanks in advance.
Shawn Reichner 29Shawn Reichner 29
1. I would use the Upsert feature of Dataloader, so that if an record already exists it will update but not create another record if changes are to be made, and if the email doesnt exist then a new record will be created. 

2. Without seeign your code, if would be difficult to help, however from your description all sounds good except for grabbing all object A's records for ListA..if you return mor ethan 100 records, you could hit a SOQL 101 governer limit error. 

Hope this helps,

Shawn
Fatfox FFatfox F
@Shawn Relchner

Thanks for your reply.
1.) Upsert operation would require record IDs of the existing records, right?

2.) Yes, I would have hit governors limit because no. of records in listA is usually around 400-500. I've used Map (ListAMap) this time as a workaround and this is the code for the trigger:

trigger ListUpload on ObjectA__c (before insert){
 Set<String> NewSet  = new Set<String>(); 
 for(ObjectA__c a : trigger.new){
  NewSet.add(a.EmailAddress__c);
 }

 List<ObjectA__c> ListA = [Select Id, EmailAddress__c from ObjectA__c];
 
 Map<String,String> ListAMap = new Map<String,String>();
 For(ObjectA__c a: ListA){
  ListAMap.put(a.EmailAddress__c, a.Id);
 }
 
 List<ObjectA__c> todelete = new List<ObjectA__c>();
 
 For(ObjectA__c a: ListA){
  string emailid = a.EmailAddress__c;
  if(!NewSet.contains(emailid)){
   todelete.add(existinglistmap.get(emailid));
  }
 }
 
 delete todelete;
 
}


So is this going to work?? And, please let me know if insert operation will work in this case (I'm quite skeptic about upsert).

Thanks again.