function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nikunj VadiNikunj Vadi 

Email trigger on notes and attachment

Hey ,

              i am new to salesforce, i am having problem for creating trigger for email . i have created support page  and given option to add notes and attachment (related list). if any one attach any file i want to notify user and me through email . as far as i know you cant create workflow for that, so please help me.

Best Answer chosen by Admin (Salesforce Developers) 
Karthik SalesforceKarthik Salesforce

Hi ,

 

   First you create a Checkbox(Custom field) in your object, write a trigger on attechment object(write the trigger on    attechment first go to any object than click edit their we have option called New trigger click the New buttton,in the URl just paste it as "entity=Attachment" or by using Eclipse IDE also we can write the trigger)

 

 

pleace check the below code

 

   trigger AttechmentsendEmail on Attachment (before insert) 
{
   list<Attachment> att=new list<Attachment> ();
   list <Merchandise__c > Marc=new list <Merchandise__c >();
   for(Attachment at: trigger.new )
   {
      Merchandise__c mar = new Merchandise__c(Id = at.parentId);
      mar.Field_Update__c=true;
      Marc.add(mar);
    }
    
   update Marc;

 

   once you attech any new attechment automatically the field will be update,than write a workflow on the custom object once the field update the Email will fire.

  by using the pagelayout you can hide the Field in your Edit page

   

 

All Answers

ShaTShaT

Hi,

 

You can use below code to send mail from trigger

 

Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
                efa.setFileName('attachment.pdf');
                efa.setBody(' Your attached file');

                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                String[] toAddresses = new string[]{' user email id '}
                email.setSubject( subject );
                email.setToAddresses(toAddresses);
                email.setPlainTextBody(Msg);
                email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

 

                // Sends the email


                Messaging.SendEmailResult [] r =
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

 

 

 

Thanks

shailu

Nikunj VadiNikunj Vadi

hey,

thanx for your reply .  what should i defiene in setfilename and setbody ?  

Karthik SalesforceKarthik Salesforce

Hi ,

 

   First you create a Checkbox(Custom field) in your object, write a trigger on attechment object(write the trigger on    attechment first go to any object than click edit their we have option called New trigger click the New buttton,in the URl just paste it as "entity=Attachment" or by using Eclipse IDE also we can write the trigger)

 

 

pleace check the below code

 

   trigger AttechmentsendEmail on Attachment (before insert) 
{
   list<Attachment> att=new list<Attachment> ();
   list <Merchandise__c > Marc=new list <Merchandise__c >();
   for(Attachment at: trigger.new )
   {
      Merchandise__c mar = new Merchandise__c(Id = at.parentId);
      mar.Field_Update__c=true;
      Marc.add(mar);
    }
    
   update Marc;

 

   once you attech any new attechment automatically the field will be update,than write a workflow on the custom object once the field update the Email will fire.

  by using the pagelayout you can hide the Field in your Edit page

   

 

This was selected as the best answer
Nikunj VadiNikunj Vadi

Hey saitaj;

          i am getting error invalid sobject attachement and i read somewhere you cant excute trigger on attachment object  . can you tell me what could be the problem ?

Karthik SalesforceKarthik Salesforce

Hi Nikunj,

 

   We have option to write the trigger on Attechment object,

 

   If your useing eclipse(or) force.com IDE directly we can write the trigger on attechment object

 

 

   or

 

  Go to Setup --> Create--> Objects-> Click on any object -->Trigger-->New 

New trigger window will open 

 

The URL look like the this

https://ap1.salesforce.com/setup/build/editApexTrigger.apexp?retURL=%2F01I90000000nGmj&entity=01I90000000nGmj

 

change the url as given below copy  and Execute the code

 

https://ap1.salesforce.com/setup/build/editApexTrigger.apexp?retURL=%2F01I90000000nGmj&entity=Attachment

 

here i changed entity=Attachment