2

I am trying to create a trigger that will update or delete a specific field in salesforce Products object accordingly. If there is image in the attachment to add the id and delete the id if there is no image. I wrote another question here but I didn't manage to get what I was aiming for so I was told to write it in another question. So here it is. Trying to fix this errors.

I am getting the following errors. I created a trigger called PopulateIdValueinProduct with the following code.

trigger PopulateIdValueinProduct on Attachment (after insert,after delete) {
    if(Trigger.isInsert())
        ProductAttachment.updateAttachmentId(Trigger.new);
    if(Trigger.isDelete())
        ProductAttachment.updateAttachmentId(Trigger.old);
}

and an apex class ProductAttachment with the following code.

public class ProductAttachment {
    public updateAttachmentId(List<Attachment> attList){
        List<Id> parentIdList = new List<Id>();
        for(Attachment att : attList) {
            parentIdList.add(att.Parentid);         
        }
        List<Product2> productList = [select id,SBQQ__ProductPictureID__c,(Select id from Attachments WHERE ContentType LIKE 'image%') 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;
    }
}

and I am getting this errors.

Name                        Line    Problem
PopulateIdValueInProduct    2       Variable does not exist: Trigger
PopulateIdValueInProduct    3       Method does not exist or incorrect signature: fogtest.ProductAttachment.updateAttachmentId(List)
PopulateIdValueInProduct    4       Variable does not exist: Trigger
PopulateIdValueInProduct    5       Method does not exist or incorrect signature: fogtest.ProductAttachment.updateAttachmentId(List)
ProductAttachment           2       Invalid constructor name: updateAttachmentId
  • 1
    Trigger.isInsert and Trigger.isUpdate. Those are variables, not methods – kurunve Mar 7 '18 at 13:25
  • I get that @kurunve but what should be the proper syntax for my code? I found this solutions online and made some edits and can't work it around :( – Darko Todorovski Mar 7 '18 at 13:31
4

Your trigger doesn't compile because your isInsert and isDelete checks are properties, not methods.

Instead of:

if (trigger.isInsert())

Use:

if (trigger.isInsert)

Your class/method don't compile because you don't specify a return type, telling the compiler you want to define a constructor.

Instead of:

public class MyClass
{
    public myMethod(/*parameters*/)
    {
        // implementation
    }
}

Use:

public with sharing class MyClass
{
    public void myMethod(/*parameters*/)
    {
        // implementation
    }
}

You should always explicitly specify with sharing unless you have a good reason not to. Also, in addition to specifying a return type, you likely want to make the method static: public static void myMethod.

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.