+ Start a Discussion
RayAllenRayAllen 

Account attachment to trigger a workflow

Hi,

 

I would like to send out an email alert anytime anyone has added an attachment to an Account.  I found the below code on here, but it is specific to an opportunity.  Could anyhelp me modify it to work for an Account?  I tried changing the obvious (opportunity -> account), but I am not having success.  I have created the field 'review_needed' in Accounts in order to match the below code.

 

Any other hints to help me in this endeavor would be greatly appreciated!

 

Thanks!

 

Code:

}

trigger Attachment_Processor on Attachment (after insert) {

map<id, opportunity> opps = new map<id, opportunity>();
Opportunity o = null;

for(attachment a:[select parentid from attachment where parent.type = 'Opportunity' and id in :trigger.keyset()]){

o = new Opportunity(Id = a.parentid, review_needed__c = true);
opps.put(a.parentid, o);

}

update opps.values();
 
Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

You can use this code to send mail

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//provide to address
String[] toAddresses = new String[] {test@test.com};
mail.setToAddresses(toAddresses);
mail.setSubject('Test Subject');
mail.setPlainTextBody
('Mail Body');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 This is one way of  sending email,

 

Other can be you can perform a dml action insert or update on any object and send a mail using email alert in a workflow on that object.

All Answers

Shashikant SharmaShashikant Sharma

Use this  trigger

trigger Attachment_Processor on Attachment (after insert) {

    String accKey  = '001';
    Set<ID> setAccountId = new Set<ID>();
    Map<ID , ID> mapAttacthmentID_AccountID = new Map<ID , ID>();
    for(Attachment  a : trigger.new)
        {
            String strParentId = String.valueOf(a.parentid).substring(0 , 3);
            if(strParentId == accKey)
                {
                    setAccountId.add(a.parentid);
                    mapAttacthmentID_AccountID.put(a.id , a.parentid);
                }
        }
    //if this condition is true that means an attachement is associated with account so you have to send mail
    if(setAccountId.size() > 0)
      {
          //Put Send Mail logic here
      }


}

 

I hope above example will help you.

RayAllenRayAllen

Hi,

 

Please excuse my ignorance, but I am not sure what to use in place of "Put mail logic here".  I am assuming the workflow I would use in order to send the email?  How would that look?

 

I have not done ANY trigger development before and couldn't be newer to this.

 

Thanks for any additional info you can provide!

Shashikant SharmaShashikant Sharma

You can use this code to send mail

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//provide to address
String[] toAddresses = new String[] {test@test.com};
mail.setToAddresses(toAddresses);
mail.setSubject('Test Subject');
mail.setPlainTextBody
('Mail Body');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 This is one way of  sending email,

 

Other can be you can perform a dml action insert or update on any object and send a mail using email alert in a workflow on that object.

This was selected as the best answer
RayAllenRayAllen

THANK YOU!