I have a standard object Products where i have a custom text field SBQQ__ProductPictureID__c. I want to store there the image located in attachments(png or jpg) and delete the value in the field if the attachment is deleted.

Here is my code, where I don't know how to add the png/jpg so it can detect only those fieles in the attachments.

trigger PopulateIdValueinProduct on Attachment (after insert,after delete) {
public class ProductAttachment{
    public static updateAttachmentid(List<Attachment> atchList){
        List<Id> parentIdList = new List<Id>();
        for(Attachment att : attList) {
        List<Product2> productList = [select id,SBQQ__ProductPictureID__c,(Select id from Attachments) From Product2 Where id in :parentidList];
        for(Product2 products : productList) {
            String temp = '';
            for(Attachment att : products.Attachments) {
                temp = att.id + ',';
            products.SBQQ__ProductPictureID__c = temp;    
        update productList;


Additionally i am experiencing this error after i made the change. enter image description here


You can add ContentType filter in Query so it will only return images.

[select id,SBQQ__ProductPictureID__c,(Select id from Attachments WHERE ContentType LIKE 'image%') From Product2 Where id in :parentidList];
| improve this answer | |
  • Hi Tushar i made the changes now i am experiencing some errors. Please check the edited version of the question. Thanks – Darko Todorovski Mar 7 '18 at 12:29
  • @DarkoTodorovski you need to define method return type. Also it would be better if you ask new question instead of editing old questions. – Tushar Sharma Mar 7 '18 at 12:32

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.