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
Pritesh JosePritesh Jose 

Email triggerring using button in contract object


My requirement is that on click of a custom button in contract object  an email needs to be generated.
 The button is not embedded in the visual force but is present at the contract header.When user goes to the contract Tab and clicks any valid contract the "Email" button comes next standard "Edit" "Delete" and clone buttons as expected for the specific contract.Have been succesfull till here.A dummy email gets triggered.

The next part of the requirement is retrieve the current contract number and pass it in the email subject .
I used SOQL to retrieve the current id but I'm getting null value "List has no rows for assignment to SObject 
An unexpected error has occurred. Your development organization has been notified.".
This indicates my SOQL query has not retrieved any value for the current id.wanted to now where I have gone wrong.
Any advice on the syntax and code to be used would be of great help.

Indiacted below is my entire apex class code below :
// below is the email triggerring apex class
public class emailsend_class{

string email_body {get {return 'this is contractemail'; } set;}
string email_subject {get;set;}

public String currentRecordId {get;set;}
public String parameterValue {get;set;}

 public Contract con{get;set;}


list<string> emails = new list<string>{'pritesh.curicad@fci.com'};

//  Below method code will retrieve the current record 

public emailsend_class(){
currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
 con = [select ContractNumber from Contract where id =: currentRecordId ];

}

 
  public Contract getContract(){
    return con; }
  
public PageReference send() {

  Messaging.singleEmailmessage email = new Messaging.singleEmailmessage();
   email.setSubject(email_subject);
   email.setPlainTextBody(email_body);
   email.setToAddresses (emails);
  Messaging.SendEmailResult[] r = Messaging.SendEmail( new Messaging.singleEmailmessage[] {email} ); 
   
   return null;
   
    }

}

The visualforce page has only the below basic code

<apex:page controller="emailsend_class" action="{!send}">
  
 
</apex:page>
Himanshu MaheshwariHimanshu Maheshwari

Hi Pritesh,

You need to use standard controller and extension instead of custom controller.

Modify your buttom Content type to Visual force page.

User-added image

Change your contructor to below code :

public emailsend_class(ApexPages.StandardController stdController) { 
    currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
    con = [select ContractNumber from Contract where id =: currentRecordId ];
}

Thanks,
Himanshu
Pritesh JosePritesh Jose
Hi Himanshu,
Thanks for your response.
I had earlier tried to use the visual forcepage as you have now suggested but the below marked content was undeditable as shown even though my visualforce page is existing hence used a work around where "content source" was kept as url and used "/apex/TEST" where TEST is the visual force pagename based on which was able to trigger the dummy email with body as"this is contractemail" per my apex code. Is there something missed in my case to have the below "content" enabled .
a)
Button with visual force content

b)In visualforce page had added the standard controller for the object and used apex class under extension and action of sending the email post update of the apex class as per suggestion  but the still the constructor did not fetch record  and got my earlier error of no record  .In this case I used my  earlier mentioned method "/apex/TEST" under URL due to the issue mentioned in pt a) . 
 
Himanshu MaheshwariHimanshu Maheshwari

HI Pritesh ,

Can you provide button code which you are using to call the VF page. First change the VF page to standard controller and then try to change the button. Vf page will start appearing in the content picklist.
 

Pritesh JosePritesh Jose
Hi Himanshu,
Thanks for you inputs. I have edited the vf page first but the Button doesn't diplay "content" :(.
Below is vf code now with standard controller. :
<apex:page standardcontroller="contract" extensions="emailsend_class" action="{!send}" >
 

</apex:page>
Below is my button code Text_Next_Email is vf page name.

User-added image




 
Himanshu MaheshwariHimanshu Maheshwari
Hi Pritesh,
Please change your Content Source from URL to Visualforce Page in your button, after that content picklist will start displaying.