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
teja swi 10teja swi 10 

How can i send a mail attachment pdf when i clicked on a button using parameters passing in the url

This is my code ....  I created a button in Vf when i click on that button my page should convert in to pdf and send a mail with that pdf for that i have written below code ... but my problem is how can i give values to the page , my page name is Stre 
  public void sendpdf()
          {
           string cname = ApexPages.currentPage().getParameters().get('pId');
           string dname = ApexPages.currentPage().getParameters().get('cId');
           string ename = ApexPages.currentPage().getParameters().get('chId');
           string fname = ApexPages.currentPage().getParameters().get('chstnum');
           string gname = ApexPages.currentPage().getParameters().get('chendnum');
           string hname = ApexPages.currentPage().getParameters().get('commamount');  
          string iname = ApexPages.currentPage().getParameters().get('pcreated');      
           string jname = ApexPages.currentPage().getParameters().get('pcfailed');      
           string kname = ApexPages.currentPage().getParameters().get('totalcom');      
              Messaging.SingleEmailMessage email1=new Messaging.SingleEmailMessage();
              String[] toadd=new string[]{'teja.sfdc99@gmail.com'};
              email1.setToAddresses(toadd);
              pageReference p=page.Stre;
              Blob b=p.getContentAsPdf();
              Messaging.EmailFileAttachment ef=new  Messaging.EmailFileAttachment();
              ef.SetFileName('Check Run PDF');
              ef.SetBody(b);
              Messaging.EmailFileAttachment[] attachments=new  Messaging.EmailFileAttachment[]{ef};
              email1.setFileAttachments(attachments);
              email1.setSubject('PDF Attachment');
              email1.SetPlainTextBody('This is sample test PDF');
              Messaging.Email[] emails=new Messaging.Email[]{email1};
              Messaging.SendEmail(emails);
              
              }

sample page with parametes is https://c.cs1.visual.force.com/apex/Stre?pId=Phoenix%20Health%20Plans%20(Formerly%20Abrazo)&cId=14/03/2015&chId=369175.0&chstnum=22912&chendnum=22949&commamount=369175.0&pcreated=3240&pcfailed=399&totalcom=4324

how can i pass this in my code please tell me i have taken the values as strings in the code but how can i pass it in pageReference p=page.Stre?.......


please help me with this 

 

 
Marek Kosar_Marek Kosar_
Hello,

try this:
Pagereference p = page.Stre;
p.getParameters().put('pId','cname');
(etc..for other params)
....
Blob b=p.getContentAsPdf();
....

 
teja swi 10teja swi 10
Hi Marek , Thanks for ur reponse i tried ur logic but am getting error like this  Exception common.page.PageInterruptException, Cyclical server-side forwards detected: /apex/stre?inline=1 

 public void sendpdf()
          {
           string cname = ApexPages.currentPage().getParameters().get('pId');
           string dname = ApexPages.currentPage().getParameters().get('cId');
           string ename = ApexPages.currentPage().getParameters().get('chId');
           string fname = ApexPages.currentPage().getParameters().get('chstnum');
           string gname = ApexPages.currentPage().getParameters().get('chendnum');
           string hname = ApexPages.currentPage().getParameters().get('commamount');  
          string iname = ApexPages.currentPage().getParameters().get('pcreated');      
           string jname = ApexPages.currentPage().getParameters().get('pcfailed');      
           string kname = ApexPages.currentPage().getParameters().get('totalcom');      
              Messaging.SingleEmailMessage email1=new Messaging.SingleEmailMessage();
              String[] toadd=new string[]{'teja.sfdc99@gmail.com'};
              email1.setToAddresses(toadd);
              pageReference p=page.Stre;
              p.getParameters().put('pId','cname');
              p.getParameters().put('cId','dname');
              p.getParameters().put('chId','ename');
            p.getParameters().put('chstnum','fname');
            p.getParameters().put('chendnum','gname');
            p.getParameters().put('commamount','hname');
            p.getParameters().put('pcreated','iname');
            p.getParameters().put('pcfailed','jname');
            p.getParameters().put('totalcom','kname');
            Blob b=p.getContentAsPdf();
              Messaging.EmailFileAttachment ef=new  Messaging.EmailFileAttachment();
              ef.SetFileName('Check Run PDF');
              ef.SetBody(b);
              Messaging.EmailFileAttachment[] attachments=new  Messaging.EmailFileAttachment[]{ef};
              email1.setFileAttachments(attachments);
              email1.setSubject('PDF Attachment');
              email1.SetPlainTextBody('This is sample test PDF');
              Messaging.Email[] emails=new Messaging.Email[]{email1};
              Messaging.SendEmail(emails);
              
              }
teja swi 10teja swi 10
am using 35.0 for this class
 
Marek Kosar_Marek Kosar_
Another option is:
PageReference p = new PageReference('/apex/Stre?pId='+cname+'&cId='+dname+'&chId..........

 
teja swi 10teja swi 10
still getting error like this Exception common.page.PageInterruptException, Cyclical server-side forwards detected: /apex/Stre?inline=1  
 
teja swi 10teja swi 10
Hi sumit i had gone through that but am using 35.0 so how can i solve this pls help me 
 
Sumit Kumar Singh 9Sumit Kumar Singh 9
Try to degrade the Apex Class version and see, if it solves your problem
teja swi 10teja swi 10
i degraded it too but no use
Sumit Kumar Singh 9Sumit Kumar Singh 9

Try to degrade the Apex Class version to 25  and see, if it solves your problem.
If not, take backup of the class, delete the old class and try to create a new class in version 25 directly  and paste the code, see if it solves your problem.

Steps - Navigate to Apex Class -> click on "Version Settings" tab  and choose 'version' 25.

T hanks,
Sumit KumarSingh
 

teja swi 10teja swi 10
i tried it but not working
Sumit Kumar Singh 9Sumit Kumar Singh 9
Move out this part To  Class constructor from the method -
string cname = ApexPages.currentPage().getParameters().get('pId');
string dname = ApexPages.currentPage().getParameters().get('cId');
string ename = ApexPages.currentPage().getParameters().get('chId');
string fname = ApexPages.currentPage().getParameters().get('chstnum');
string gname = ApexPages.currentPage().getParameters().get('chendnum');
string hname = ApexPages.currentPage().getParameters().get('commamount');  
string iname = ApexPages.currentPage().getParameters().get('pcreated');      
string jname = ApexPages.currentPage().getParameters().get('pcfailed');      
string kname = ApexPages.currentPage().getParameters().get('totalcom')
Kumara Guru 7Kumara Guru 7

VF Page:-
<apex:page controller="salesfor">
<apex:form id="sf">
<apex:pageBlock >
<apex:pageblockSection title="Send Mail through SalesForce"> 
<apex:outputLabel value="Send to"/>
<apex:inputText value="{!sendto}"/>
<apex:outputLabel value="Subject"/>
<apex:inputText value="{!subject}"/>
<apex:outputLabel value="Body"/>
<apex:inputTextarea value="{!body}"/> 
<apex:inputFile value="{!attbody}" fileName="{!attfile}" />
<apex:commandButton value="Attach & Send" action="{!upload}" reRender="sf"/>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Apex:-

public class salesfor 
{
  public string subject{get;set;}
  public string body{get;set;}
  public blob attbody{get;set;}
  public string attfile{get;set;}
  public string sendto{get;set;}
  public pagereference upload ()
  {
    Messaging.SingleEmailMessage  em = new Messaging.SingleEmailMessage();
    String[] s = new String[]{subject};
    em.setSubject('Sample');
    em.setToAddresses(s);
    em.setPlainTextBody('Sending Mail thru Salesforce');
    
    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setFileName(attfile);
    efa.setBody(attbody);
    em.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
    Messaging.sendEmailResult[] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {em});
    return null;
     
}
}

Could you please anyone help me out. I am getting error like " apex:inputFile can not be used in conjunction with an action component, apex:commandButton or apex:commandLink that specifies a rerender or oncomplete attribute "