below is a first vf page and its controller which fetches the record fromAcount tab
VF Page-
<apex:page controller="ClassRetrieve">
<apex:panelGrid columns="2">
<apex:form >
<p></p>
<apex:pageBlock >
<apex:pageBlockTable value="{!accs}" var="a">
<apex:column headerValue="Name" >
<apex:commandLink value="{!a.name}" action="{!accountClicked}">
<apex:param name="nickName"
value="{!a.id}"
assignTo="{!recordId}"/>
</apex:commandLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:panelGrid>
</apex:page>
ITS controller-
public class ClassRetrieve {
Public string recordId{get;set;}
public List<Account> contactsInformation { get; set; }
public list<Account> accs {get;set;}
public ClassRetrieve()
{
accs = [Select Id,Name From Account];
}
public pagereference accountClicked()
{
pagereference redirect = new PageReference('/apex/recordPage?id='+recordId);
redirect.setRedirect(true);
return redirect;
}
}
whenever i click on a record,its displays a detail page and the related contact associated with the record.
VF page-
<apex:page controller="recordPageCont" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Edit" action="{!EditAccount}"/>
<apex:commandButton value="Cancel" action="{!CancelButton}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:outputField value="{!objAccount.Name}"/>
<apex:outputField value="{!objAccount.AccountNumber} "/>
<apex:param id="accountID" assignTo="{!accountID}" value="10"/>
<apex:outputField value="{!objAccount.Type} "/>
<apex:outputField value="{!objAccount.industry} "/>
<apex:outputField value="{!objAccount.Rating} "/>
<apex:outputText label="Email" id="Custom_Email__c" value=" {!objAccount.Name}@salesforce.com"></apex:outputText>
<!-- b>Email</b> {!objAccount.Name}@salesforce.com -->
<p></p> <b>Address</b> {!objAccount.Billingstreet} {!objAccount.Billingcity}{!objAccount.Billingcountry}{!Objaccount.Billingstate}{!objAccount.BillingPostalCode}
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<apex:pageBlock title="Contacts Related With {!objaccount.name}"></apex:pageBlock>
<apex:form >
<apex:pageBlock >
<apex:outputPanel id="ContactDetail">
<apex:repeat value="{! displayContacts}" var="contact">
<p><apex:inputCheckbox id="checkbox"/><apex:outputLink value="/apex/editcontact?id={!contact.id}" id="theLink">Edit</apex:outputLink> <b>Name :</b> {! contact.name} <b>Title:</b> {!contact.title} <b>Mobile</b> :{!contact.MobilePhone} <b>Email :</b>{!contact.email}</p>
</apex:repeat>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
its controller-
public class recordPageCont {
public Account objAccount{get;set;}
Public string recId{get;set;}
Public string AccountID{get;set;}
Public Contact[] displayContacts {get;set;}
public Contact cntact{get;set;}
public recordPageCont ()
{
recId = apexpages.currentpage().getparameters().get('id');
objAccount =[SELECT Name, AccountNumber ,Type , Industry , Rating,BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry,(SELECT Name , MobilePhone,Title from Contacts)
FROM account WHERE ID = :recId ];
Account[] accts =[SELECT Name, AccountNumber ,Type , Industry , Rating, (SELECT Name , MobilePhone,Title,email from Contacts)
FROM account WHERE ID = :recId ];
Account acct = accts[0];
if (!objAccount.Contacts.isEmpty())
{
displayContacts = acct.Contacts;
}}
Public pagereference Editaccount()
{
Pagereference edtaccount = new Pagereference('/apex/editaccount?id='+recid);
edtaccount.setredirect(true);
return edtaccount;
}
public pagereference cancelButton()
{
Pagereference cnclbutton = new Pagereference('/apex/accountdisplay');
cnclbutton.setredirect(true);
return cnclbutton;
}
}
whenever i edit any contact,a new page opens which allow me to edit the contacts field.I have a requirement that whever i update email field of contact , an automatic trigger should run which should copy this updated email field to my custom email field in account section.
VF to edit Contacts-
<apex:page standardController="contact" extensions="editcontactController" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:inputField value="{!cntact.name}"/>
<apex:inputField value="{!cntact.MobilePhone}"/>
<apex:inputField value="{!cntact.Title}"/>
<apex:inputField value="{!cntact.email}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!savecontact}"/>
<apex:commandButton value="Cancel" action="{!cancelbutton}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
and its controller-
public class editcontactController
{
public editcontactController() {
}
Can someone please help me...
public Contact cntact{get;set;}
public editcontactController(ApexPages.StandardController stdController)
{
this.cntact=(Contact)stdController.getrecord();
String recid = apexpages.currentpage().getparameters().get('id');
cntact=[SELECT Name , MobilePhone,Title,email from Contact where ID=:recid ];
}
public pagereference cancelbutton()
{
Pagereference cancel = new pagereference('/apex/accountdisplay');
cancel.setredirect(true);
return cancel;
}
public pagereference savecontact()
{
update cntact;
Pagereference cancel = new pagereference('/apex/accountdisplay');
cancel.setredirect(true);
return cancel;
}
}
This is how i am approaching the trigger ..but its giving me a lot of errors..can you help me modify it
trigger UpdateAccountEmails on Contact (after update)
{
set<contact>con2up = new set<contact>();
map<Id,string>con2eml = new map<Id,string>();
map<Id,Id>acct2con = new map<Id,Id>();
for(contact con:trigger.new)
if(con.email != trigger.oldmap.get(con.Id).email){
con2up.add(con.Id);
con2eml.put(con.Id, string.valueOf.con.email);
acct2con.put(con.accountId,conId);
}
list<Account>accts2up = new List<Account>();
//One would be tempted to run the following query:
// map<Id,Account>acctsMap = new map([SELECT Id, Account.Custom_Email__c FROM Account WHERE Id IN :con2up]);
// however, it's not needed because we already have the account Id's
set<Id>acctIds=acct2con.keyset;
for(Id ac:acctIds){
account a = new account(Id=ac);
//assign the new email
a.Custom_Email__c = con2eml.get(acct2con.get(ac));
//add to list
accts2up.add(a);
}
if(accts2up.isEmpty == false) update accts2up;
}