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
Nirmal ChristopherNirmal Christopher 

Bulkify a trigger?

Is this trigger bulkified im unable to bulk inset using data loader only the last recored in the csv file gets the field update properly

trigger urlattachmentupdate on Attachment (after insert,after update) {
list<contact>cc=new list <contact>();
set<attachment>a=new set<attachment>();
Public string attachmentid='';
Public string parentid='';

for(Attachment aa:trigger.new){
attachmentid=aa.id;
parentid=aa.parentid;
System.debug('Attchment IDDD'+attachmentid);
System.debug('ParentIDDDDDDDDDDD'+parentid);
}
cc=[Select id from contact where id =:parentid];
for(contact cc1:cc){
cc1.Member_Photo_URL__c='https://c.cs10.content.force.com/servlet/servlet.FileDownload?file='+attachmentid;
}
update cc;
}
Best Answer chosen by Nirmal Christopher
Elie.RodrigueElie.Rodrigue
You are currently storing the ids as string, those would need to be a list; or a map would be better : 

trigger urlattachmentupdate on Attachment (after insert,after update) {
     list<contact>cc=new list <contact>();

     Map<id,id> attachmentByParent = new Map<id,id>();

     for(Attachment aa:trigger.new){
 
            attachmentByParent.put(aa.parentId,aa.Id);
 
     }
     cc=[Select id from contact where id in :attachmentByParent.keySet()];
     for(contact cc1:cc){
 
            cc1.Member_Photo_URL__c='https://c.cs10.content.force.com/servlet/servlet.FileDownload?file='+attachmentByParent.get(cc1.Id);
     }
     update cc;
}

All Answers

Elie.RodrigueElie.Rodrigue
You are currently storing the ids as string, those would need to be a list; or a map would be better : 

trigger urlattachmentupdate on Attachment (after insert,after update) {
     list<contact>cc=new list <contact>();

     Map<id,id> attachmentByParent = new Map<id,id>();

     for(Attachment aa:trigger.new){
 
            attachmentByParent.put(aa.parentId,aa.Id);
 
     }
     cc=[Select id from contact where id in :attachmentByParent.keySet()];
     for(contact cc1:cc){
 
            cc1.Member_Photo_URL__c='https://c.cs10.content.force.com/servlet/servlet.FileDownload?file='+attachmentByParent.get(cc1.Id);
     }
     update cc;
}
This was selected as the best answer
Adnubis LLCAdnubis LLC
Try this one. The code you were using would only store the last attachmentId and ParentId. Instead you should store all of them in collections like below. This should do the trick and is bulkified. You did a good job keeping the Update statement outside of for loops.

trigger urlattachmentupdate on Attachment (after insert,after update) {

list<contact>cc=new list <contact>();
set<attachment>a=new set<attachment>();

public map<String,String> attachmentIdMap = new map<SString,tring>();
public set<String> parentIds = new set<String>();

for(Attachment aa:trigger.new){
  parentIds.add(aa.parentid);
  attachmentIdMap.put(aa.parentid,aa.id);
  System.debug('Attchment IDDD'+attachmentid);
  System.debug('ParentIDDDDDDDDDDD'+parentid);
}

cc=[Select id from contact where id in : parentIds];
for(contact cc1:cc){
  String attachmentid = attachmentIdMap.get(contact.id);
  cc1.Member_Photo_URL__c='https://c.cs10.content.force.com/servlet/servlet.FileDownload?file='+attachmentid;
}

update cc;
}