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
re_plagasre_plagas 

Trigger : Email on clone,creating new records

Hi I need help on creating a trigger that fires email when I clone or create a record in a custom object.

Best Answer chosen by Admin (Salesforce Developers) 
super developersuper developer

Hi ,

 

we can done on Creation of record. in trigger.

Try this

 

trigger Customobjectemail on Customobject__c(After Insert)

{

for(Customobject__c co:trigger.new)

{

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 
mail.setToAddresses(co.email__c);//other wise get owner emailid.


// Specify the subject line for your email address. 
   
mail.setSubject('SUBJECT');


// Set to True if you want to BCC yourself on the email. 
   
mail.setBccSender(false);


// Optionally append the salesforce.com email signature to the email. 
   
// The email address of the user executing the Apex Code will be used. 
   
mail.setUseSignature(false);


// Specify the text content of the email. 
   
mail.setPlainTextBody('hi');//either plain text body (or) Htmlbody.
mail.setHtmlBody('<b>Hi</b>');

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}

}

 

 

All Answers

super developersuper developer

Hi ,

 

we can done on Creation of record. in trigger.

Try this

 

trigger Customobjectemail on Customobject__c(After Insert)

{

for(Customobject__c co:trigger.new)

{

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 
mail.setToAddresses(co.email__c);//other wise get owner emailid.


// Specify the subject line for your email address. 
   
mail.setSubject('SUBJECT');


// Set to True if you want to BCC yourself on the email. 
   
mail.setBccSender(false);


// Optionally append the salesforce.com email signature to the email. 
   
// The email address of the user executing the Apex Code will be used. 
   
mail.setUseSignature(false);


// Specify the text content of the email. 
   
mail.setPlainTextBody('hi');//either plain text body (or) Htmlbody.
mail.setHtmlBody('<b>Hi</b>');

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}

}

 

 

This was selected as the best answer
re_plagasre_plagas

There is error if I change my Line 11 to this:

 

mail.setToAddresses('forseen_destiny@live.com');


Error: Compile Error: Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setToAddresses(String) at line 11 column 1

 


super developersuper developer

string[] toaddress = 'sdff@hhh.com';

mail.setToAddresses(toaddress);

re_plagasre_plagas

Great thx! If I wanna set my subject with info from my custom object, how do I do that?

super developersuper developer

just place your custom fields

 

mail.setsubject('Hi'+co.name+'some text'+co.Customfield__c);

do same like this to body also.

super developersuper developer

Hi accept my solutions.

re_plagasre_plagas

Aw thanks man! I owe u a big favor!