0

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;
}
2
  • Can someone please help? – Ankita Singh Jun 1 '15 at 10:07
  • Instead of pasting 200 lines of code and asking for someone to identify your problem, I think you might have more luck if you spend some time actually trying to narrow down to a specific issue, Ankita. Just a suggestion - it's kind of an overwhelming question as it is right now. – Brian Mansfield Jun 1 '15 at 14:16
2

I don't have enough reputation to add comment to clarify more about your requirement.

If your requirement is to update Account field when the Contact Email is changed from this Visualforce Page, then you can do this in the savecontact() method itself instead of implementing it in trigger.

public pagereference savecontact()
{
    update cntact;
    List<Account> accounts = [SELECT id, Custom_Email__c FROM Account WHERE Id =: cntact.AccountId LIMIT 1];
    if(accounts != NULL && accounts.size() > 0) {
        accounts[0].Custom_Email__c = cntact.Email;
        update accounts;
    }
    Pagereference cancel = new pagereference('/apex/accountdisplay');
    cancel.setredirect(true);
    return cancel;
}

If your requirement is to update Account field whenever any associated Contact email is changed, then you can go with below trigger.

trigger UpdateAccountEmails on Contact (after update){
    // Used to store Account Id and the Contact Email which is changed
    Map<id,String> mapAccountIdContactEmail = new Map<Id,String>();
    for(Contact c : Trigger.new){
        if(trigger.oldMap.get(c.Id).Email != c.Email)//email has been changed
            mapAccountIdContactEmail.put(c.AccountId,c.Email);        
    }
    List<Account> accounts = new List<Account>();
    // Query Accounts from the map key set
    for(Account a : [SELECT Id,Custom_Email__c FROM Account WHERE Id IN : mapAccountIdContactEmail.keySet()]){
        //assign the new email
        a.Custom_Email__c = mapAccountIdContactEmail.get(a.Id);
        //extract the account into our list
        accounts.add(a);
    }
    update accounts;
}

Hope it helps.

9
  • :@vigneshwarang - I have modified my trigger above..i am getting the error Compile Error: Variable does not exist: string.valueOf.con.email at line 13 column 27 at con2eml.put(con.Id, string.valueOf.con.email); – Ankita Singh Jun 2 '15 at 9:20
  • 1
    Please change to con2eml.put(con.id,con.email); – Vigneshwaran G Jun 2 '15 at 9:28
  • Thank for your reply..I did change it before ur reply..but now its giving me another error at con2up.add(con.Id); Errror is - Compile Error: Incompatible element type Id for collection of Contact at line 12 column 7 – Ankita Singh Jun 2 '15 at 9:36
  • 1
    Please change to con2up.add(con); – Vigneshwaran G Jun 2 '15 at 10:12
  • Thanks a lot for your help..and sorry for asking u so many solutions...But its giving me error after error..now error coming at set<Id>acctIds=acct2con.keyset; error is Compile Error: Initial term of field expression must be a concrete SObject: Map<Id,Id> at line 26 column 20 – Ankita Singh Jun 2 '15 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.