1

There is no external id column for Attachment object, but the requirement is to insert Attachment for the parentId if there is no attachment present else use the already existing one. Please suggest how to make an upsert on attachment object base on parentId. Please provide technical details for upsert from controller referenced in some VisualForce pages.

3

You cant create fields on attachment as you said so only way i would do an upsert is to query for the attachment with existing parent Id and If found update the attachment body else do an insert.

Here is the pseudo code for same .

Map<Id,Attachment> MapParentIdByAttchment=new Map<Id,Attachment>();
for(Attachment attch:[Select Id,ParentId from Attachment where ParentId in:setparentIds]){
            MapParentIdByAttchment.put(attch.ParentId ,attch);
}
for(Parentrecord__c p:parentlist){
 if(MapParentIdByAttchment.get(p.Id)!=null){
      //update
  }else{
      //insert
   }
 }
3
  • Can you please suggest if these DML operations can be called from controller referenced in some VF page? not from apex trigger class. – Testing_SFDC Aug 12 '14 at 10:49
  • Yes it can be called from anywhere .All that you need to know is the Parent Id of the record – Mohith Shrivastava Aug 12 '14 at 11:00
  • I'm pretty much certain this won't work when you a single parent has multiple attachments, which is possible for example Contacts. – Haris Osmanagić Apr 28 '15 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.