1

I need a Trigger as described in the title. I have a Trigger for this, but it looks like it's not working. I am not a Developer, so I don't know anything about coding. Any help will be much appreciated.

Here is the code:

trigger SetTitleToAttachment on Attachment (after insert) {

    Set<Id> oppIds = new Set<Id>();

    //go through all inserted attachments
    for(Attachment att: Trigger.new){
        if(att.name == 'Countersigned Order Form' &&
            Schema.Opportunity.SObjectType == att.ParentId.getSobjectType()){
            oppIds.add(att.ParentId);
        }
    }

    List<Opportunity> oppsToUpdate = new List<Opportunity>();
    for(Id oppId : oppIds){
        oppsToUpdate.add(new Opportunity(Id = oppId, StageName = 'Closed Won'));
    }

    update oppsToUpdate;
}
1
  • Are your oppID's getting populated? Put in System.debugs
    – EricSSH
    Feb 17, 2017 at 15:41

1 Answer 1

0

I've tested your code and it works okay. Howewer there's a catch. When you insert, let's say a txt file, attachment name becomes Countersigned Order Form.txt and this is the reason code is not updating opportunity record. Just change your control like this;

if(att.name.startsWith('Countersigned Order Form') &&
            Schema.Opportunity.SObjectType == att.ParentId.getSobjectType()){
            oppIds.add(att.ParentId);
        }

And it'll work just fine.

One other basic thing to check would be if trigger is Active.

1
  • Thank you very much, tugce! That worked! :) I was thinking - "what if a different file type is attached like pdf or jpg?", but, of course, I didn't know how to change the code...
    – Ivan
    Feb 17, 2017 at 18:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .