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
Ashu3006Ashu3006 

The Account Name and Contat Details are not getting populated in VFP

HI Everyone,

I have created the custom controller and the visual force page ,

But when run the visualforce page i am no able to get the Account.Name and other Contact details below is the

Apex code and visualforce

 

public class sendEmail {
public String subject { get; set; }
public String body { get; set; }
private final Account account;
// Create a constructor that populates the Account object
public sendEmail() {
List<Account> account= [select Name, (SELECT Contact.Name, Contact.Email FROM Account.Contacts)
from Account] where id = :ApexPages.currentPage().getParameters().get('id')];
}
public Account getAccount() {
return Account;
}
public PageReference send() {
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
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 = 0; i < account.Contacts.size(); i++)
{
if (account.Contacts[i].Email != null)
{
addresses += ':' + account.Contacts[i].Email;
}
}
}
String[] toAddresses = addresses.split(':', 0);
// Sets the paramaters of the email
email.setSubject( subject );
email.setToAddresses( toAddresses );
email.setPlainTextBody( body );
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
return null;
}
}

/****************VisualForcePage********************************/

<apex:page controller="sendEmail">
<apex:messages />
        <apex:pageBlock title="Send an Email to Your{!account.name} Representatives">
        <p>Fill out the fields below to test how you might send an email to a user.</p>
        <br />
        <apex:dataTable value="{!account.Contacts}" var="contact" border="1">
        <apex:column >
        <apex:facet name="header">Name</apex:facet>
        {!contact.Name}
        </apex:column>
       
        <apex:column >
        <apex:facet name="header">Email</apex:facet>
        {!contact.Email}
        </apex:column>
       </apex:dataTable>

<apex:form >
<br /><br />
<apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
<br /><br />
<apex:outputLabel value="Body" for="Body"/>:<br />
<apex:inputTextarea value="{!body}" id="Body" rows="10" cols="80"/>
<br /><br /><br />
<apex:commandButton value="SendEmail" action="{!send}" />
</apex:form>
</apex:pageBlock>
</apex:page>

 

Can u pl suggets what going wrong

 

Regard

Ashu

Navatar_DbSupNavatar_DbSup

Hi,

You have to pass the accountid in the url

For example:

https://c.ap1.visual.force.com/apex/conatctdteail?id=0019000000BOQPK 


Red mark text is account id. 


Try the below modified code snippet as reference:

--------------- Apex controller--------------

public class sendEmail {

public String body { get; set; }

 

    public String subject { get; set; }

 

   private final Account account;

 

// Create a constructor that populates the Account object

public sendEmail() {

account = [select Name, (SELECT Contact.Name, Contact.Email FROM Account.Contacts)from Account where id = :ApexPages.currentPage().getParameters().get('id')];

system.debug('++++++++++++++++++++++++++++++'+account);

}

public Account getAccount() {

return account;

}

public PageReference send() {

// Define the email

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

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 = 0; i < account.Contacts.size(); i++)

{

if (account.Contacts[i].Email != null)

{

addresses += ':' + account.Contacts[i].Email;

}

}

}

String[] toAddresses = addresses.split(':', 0);

// Sets the paramaters of the email

email.setSubject( subject );

email.setToAddresses( toAddresses );

email.setPlainTextBody( body );

Messaging.SendEmailResult [] r =

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

return null;

}

}

------------- Vf page-------------------

<apex:page controller="sendEmail">

<apex:messages />

        <apex:pageBlock title="Send an Email to Your{!account.name} Representatives">

        <p>Fill out the fields below to test how you might send an email to a user.</p>

        <br />

        <apex:dataTable value="{!account.Contacts}" var="contact" border="1">

        <apex:column >

        <apex:facet name="header">Name</apex:facet>

        {!contact.Name}

        </apex:column>

      

        <apex:column >

        <apex:facet name="header">Email</apex:facet>

        {!contact.Email}

        </apex:column>

       </apex:dataTable>

 

<apex:form >

<br /><br />

<apex:inputText value="{!subject}" id="Subject" maxlength="80"/>

<br /><br />

<apex:outputLabel value="Body" for="Body"/>:<br />

<apex:inputTextarea value="{!body}" id="Body" rows="10" cols="80"/>

<br /><br /><br />

<apex:commandButton value="SendEmail" action="{!send}" />

</apex:form>

</apex:pageBlock>

</apex:page>

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.