You need to sign in to do that
Don't have an account?
Chelsea Lukowski
Trigger is creating duplicate records
We integrate pdf's into a junction salesforce object called PDF_Uploader__c, then I have a before insert trigger that creates a record(AS400_Report__c) from the attachment name and then moves the attachment to the newly created record. If I attach the pdf manually one at a time, the trigger works perfectly. If I use our integration system(Jitterbit) and try to insert 2 attachments, it creates two records from only one of the attachments, instead of creating two records with an attachment in each. I user a master trigger and trigger handler class to accomplish this. What am I doing wrong?
public void createAS400ReportRecord(){ List<AS400_Report__c> reportList = new List<AS400_Report__c>(); Map<Id,AS400_Report__c> reportMap = new Map<Id,AS400_Report__c>(); string attachmentNameWithoutExtension = ''; list<string> splitList = new list<string>(); Set<String>reportKeySet = new Set<String>(); List<AS400_Report__c> recordsToCreate = new List<AS400_Report__c>(); Attachment a; for(sObject sObj : Trigger.new){ a = (Attachment)sObj; if(string.isBlank(a.Name)){ continue; } if(a.ParentId.getSObjectType() == PDF_Uploader__c.getSObjectType() && a.Description == 'AS400 Documents'){ system.debug('In Loop1: ' + a.Name); splitList = a.Name.split('\\.'); //Need to get rid of .pdf attachmentNameWithoutExtension = splitList[0]; reportKeySet.add(attachmentNameWithoutExtension); system.debug('In Loop2: ' + splitList[0]); } //system.debug(a.Name); } for(sObject sObj : Trigger.new){ a = (Attachment)sObj; AS400_Report__c report = new AS400_Report__c(); report.Name = attachmentNameWithoutExtension; report.Status__c = 'New'; report.Created_Date_Time__c = date.Today(); reportList.add(report); system.debug(report.Name); } insert reportList; system.debug(reportList.size()); //2. Query the report from the set created above reportList = [SELECT Id,Name FROM AS400_Report__c Where Name = :reportKeySet]; system.debug('Report Size: ' + reportList.size()); //3. Loop through the reports and create map of key Map<String,AS400_Report__c> mapreport = new Map<String,AS400_Report__c>(); for(AS400_Report__c r : reportList){ Mapreport.put(r.Name,r); } //4.Loop thorugh attachments and use the map above to cop attachment for(sObject sObj : Trigger.new){ a = (Attachment)sObj; if(string.isBlank(a.Name)){ continue; } if(a.ParentId.getSObjectType() == PDF_Uploader__c.getSObjectType()){ splitList = a.Name.split('\\.'); //Need to get rid of .pdf attachmentNameWithoutExtension = splitList[0]; if(Mapreport.containsKey(attachmentNameWithoutExtension)){ a.ParentId = mapreport.get(attachmentNameWithoutExtension).Id; } system.debug(a.ParentId); } } }
bharath kumar 52
I have faced the same issue recently. Trust me it might be because there are 2 events of either after insert or after update. Check if something isn't right fro the integration side. In my case it was a problem with record updation because we used angular js.