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
kcnmaheshkcnmahesh 

regarding sending mail using visualforce page

I have one requirement....

 

we need to create one button called "Trak"

If we press that button on an account record an email has to be sent to the related contacts.

And Sales order object is there..,

Lookup relation with Account object

In that mail we need to send Salesorder details

 

Plz its an urgent requirement.....Help me frndz

 

 

kcnmaheshkcnmahesh

I wrote some code but its not working properly.....

 

<apex:page controller="OrderSummary" showHeader="false" action="{!QuerySalesOrders}">
    <apex:form >
          <apex:pageBlock >
            <apex:pageBlockSection >      
                            <apex:pageBlockSectionItem >
                                <apex:image id="Logo1" value=" {!URLFOR($Resource.CompanyLogo)}" />
                        </apex:pageBlockSectionItem>      
                        </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Send Mail" action="{!SendEmai}" />    
                <apex:commandButton value="Cancel" action="{!CancelEmai}" />              
                 </apex:pageBlockButtons>
       
                                <apex:pageBlockSection title="Account Orders" columns="1"  id="pbs1" >
                                <c:AccountDataComponent />
                                </apex:pageBlockSection>
                            <apex:pageBlockSection >      
                            <apex:pageBlockSectionItem >
                                <apex:image id="Logo2" value="{!URLFOR($Resource.CompanyFooter)}"  />
                        </apex:pageBlockSectionItem>      
                        </apex:pageBlockSection>
            </apex:pageBlock>    
                                        
    </apex:form>
</apex:page>

 

global class OrderSummary{
    Id accId ;
    private final Account account;
    public OrderSummary(){
        account = [select Name, (SELECT Contact.Name, Contact.Email FROM Account.Contacts where Contact.Email != null)
                    from Account where id = :ApexPages.currentPage().getParameters().get('id')];
    }
    public List<Sales_Order__c> listSalesOrders {get;set;}
    
    /*
        The following method is used for displaying records in the view page
    */
    public pageReference QuerySalesOrders(){
        accId = Apexpages.currentPage().getParameters().get('id');
        System.debug('Account id :'+accId);
        /*List<Sales_Order__c> orders = new List<Sales_Order__c>();
        orders = [ select id,Sales_Order_Status__c from Sales_Order__c where Account_Name__c =: accId];
        String status = '';
        if(orders.Sales_Order_Status__c != null)
        {
        String[] order = orders.Sales_Order_Status__c.split(';');
        for(String s:order)
        {
        if(status == null)
        status = '\''+s+'\'';
        else
        status += '\''+s+'\'';
        }
        }*/
        listSalesOrders = new List<Sales_Order__c> ();
              
               
        listSalesOrders = [select Customer_PO_No__c, Sales_Order_Status__c, WIP_Status__c,
                           Despatch_Date__c, Revised_Despatch_Date__c, Revision_Details__c
                           from Sales_Order__c where Account_Name__c =: accId AND (Balance_Amount__c!=0 OR Balance_Quantity__c!=0) ];
        return null;
    }
    
    /*
        Get REcords for the component
    */
     global List<Sales_Order__c> getSalesOrders(){
        accId = Apexpages.currentPage().getParameters().get('id');
        System.debug('Account id :'+accId);
        listSalesOrders = new List<Sales_Order__c> ();
        
        listSalesOrders = [select Customer_PO_No__c, Sales_Order_Status__c, WIP_Status__c,
                           Despatch_Date__c, Revised_Despatch_Date__c, Revision_Details__c
                           from Sales_Order__c where Account_Name__c =: accId AND (Balance_Amount__c!=0 OR Balance_Quantity__c!=0) ];
        return listSalesOrders;
    }
    public Account getAccount() {
        return account;
    }
    
   /*
        The following method is used for sending Mails
   */
    public pageReference SendEmai(){
         Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
         EmailTemplate template = [select id from EmailTemplate where name ='OrderSummary'];
         String addresses;
            if (account.Contacts[0].Email != null)
            {
                addresses = account.Contacts[0].Email;
                // Loop through the whole list of contacts and their emails
                for (Integer i = 1; i < account.Contacts.size(); i++)
                {
                    if (account.Contacts[i].Email != null)
                    {
                        addresses += ':' + account.Contacts[i].Email;
                    }
                }
            }
                String[] toAddresses = addresses.split(':', 0);
                email.setToAddresses( toAddresses );
                email.setTemplateId(template.id);
                //email.setTargetObjectId('003O0000006bObU');
                try{
                     Messaging.SendEmailResult [] r =  Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
                }catch(Exception e){
                    System.debug('Error while sending Mail :'+e.getMessage());
                }
               
        
        return new PageReference('/'+accId);
    }
    public pageReference CancelEmai(){
        return new PageReference('/'+accId);
    }

}