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
SFDC Coder 8SFDC Coder 8 

Populate another object details on custom button click

Hi All,
I am new to salesforce.
I have custom button on account page that is get contact.
When I click on 'get contact', I want to get conatct record from contact object of the same account. (I have only one contact for each account).
How can I do this?

Please help me.

Thanks in Advance!
Best Answer chosen by SFDC Coder 8
sfdcMonkey.comsfdcMonkey.com
hi
try below sample code
<apex:page standardController="Account" extensions="contactDeatils">
  <apex:form >
  <center><apex:commandButton value="cancel" action="{!cancel}" />
      <apex:commandButton value="get contact" action="{!getContact}" />
 
  </center>
      <apex:pageBlock title="Account details">
          <apex:pageBlockSection columns="1">
              <apex:outputField value="{!Account.Name}" />
           </apex:pageBlockSection>
      </apex:pageBlock>
      
   <apex:repeat value="{!DiscountList}" var="c">

      <apex:pageBlock title="Discount details">
          <apex:pageBlockSection columns="2">
              <apex:outputField value="{!c.F1__c}" />
              <apex:outputField value="{!c.F2__c}" />
              <apex:outputField value="{!c.F3__c}" />
              
           </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:repeat>

  </apex:form>
 
</apex:page>

public class contactDeatilss{

public contact discount {get;set;}
public List<contact> DiscountList {get;set;}
string accid= ApexPages.currentPage().getParameters().get('id');

public contactDeatilss(ApexPages.StandardController controller){
  /*if(accid!=null)
       DiscountList = [Select Id,lastName,AccountId from contact where AccountId=:accid];*/
    }

public PageReference cancel() {
      URL currentURL = URL.getCurrentRequestUrl();
      system.debug('currentURL.getPath()->'+currentURL.getPath());
      PageReference newpage2 = new PageReference('/'+accid);
        newpage2.setRedirect(true);
        
        return newpage2;
   
    }
 public void getContact() {
      if(accid!=null)
    DiscountList = [Select Id,lastName,F1__c,F2__c,F3__c, AccountId from contact where AccountId=:accid];
    }      
}



i hope it helps you
let me inform if it helps you
thanks

All Answers

SFDC Coder 8SFDC Coder 8
Hi Piyush,
Yes.. I am working on Visualforce page.
I am not able write proper controller class for that.
 
SFDC Coder 8SFDC Coder 8
Hi Piyush,
Thanks for your time.
public class contactDeatils{

public contact discount {get;set;}
public List<contact> DiscountList {get;set;}
string accid= ApexPages.currentPage().getParameters().get('accid');

public contactDeatils(ApexPages.StandardController controller){
if(accid!=null)
DiscountList = [Select Id,F1__c,F2__c,AccountId from contact where AccountId=:accid];
}

public PageReference cancel() {
      URL currentURL = URL.getCurrentRequestUrl();
      system.debug('currentURL.getPath()->'+currentURL.getPath());
      PageReference newpage2 = new PageReference('/'+accid);
        newpage2.setRedirect(true);
        
        return newpage2;

        
    }

}
<apex:page standardController="Account" extensions="contactDeatils">
  <apex:form >
  <center><apex:commandButton value="cancel" action="{!cancel}" /></center>
        
   <apex:repeat value="{!ConList}" var="c">

      <apex:pageBlock title="Discount details">
          <apex:pageBlockSection columns="2">
              <apex:outputField value="{!c.F1__c}}" />
              <apex:outputField value="{!d.F2__c}}" />
              <apex:outputField value="{!d.F3__c}}" />
              
           </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:repeat>

  </apex:form>
  
</apex:page>

This is my code.
 
sfdcMonkey.comsfdcMonkey.com
hi
try below sample code
<apex:page standardController="Account" extensions="contactDeatils">
  <apex:form >
  <center><apex:commandButton value="cancel" action="{!cancel}" />
      <apex:commandButton value="get contact" action="{!getContact}" />
 
  </center>
      <apex:pageBlock title="Account details">
          <apex:pageBlockSection columns="1">
              <apex:outputField value="{!Account.Name}" />
           </apex:pageBlockSection>
      </apex:pageBlock>
      
   <apex:repeat value="{!DiscountList}" var="c">

      <apex:pageBlock title="Discount details">
          <apex:pageBlockSection columns="2">
              <apex:outputField value="{!c.F1__c}" />
              <apex:outputField value="{!c.F2__c}" />
              <apex:outputField value="{!c.F3__c}" />
              
           </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:repeat>

  </apex:form>
 
</apex:page>

public class contactDeatilss{

public contact discount {get;set;}
public List<contact> DiscountList {get;set;}
string accid= ApexPages.currentPage().getParameters().get('id');

public contactDeatilss(ApexPages.StandardController controller){
  /*if(accid!=null)
       DiscountList = [Select Id,lastName,AccountId from contact where AccountId=:accid];*/
    }

public PageReference cancel() {
      URL currentURL = URL.getCurrentRequestUrl();
      system.debug('currentURL.getPath()->'+currentURL.getPath());
      PageReference newpage2 = new PageReference('/'+accid);
        newpage2.setRedirect(true);
        
        return newpage2;
   
    }
 public void getContact() {
      if(accid!=null)
    DiscountList = [Select Id,lastName,F1__c,F2__c,F3__c, AccountId from contact where AccountId=:accid];
    }      
}



i hope it helps you
let me inform if it helps you
thanks
This was selected as the best answer