+ Start a Discussion
Rahul Gupta 137Rahul Gupta 137 

Finding contacts for certain case

hi guys ,
i need help to devolp the code for finding contact from case list .
there will a picklist of cases we will have to choose a certain case and click on custom button find which will give contacts under that case 

thanks in advance 
SamarSSamarS
Hi Rahul,

I am assuming it to be a VF page.
You may need to use here action function on change event of picklist field, through below JS script you can get the record id of case record that is selected which can be queried to get the contact associated with it.

Code Snippet I used  - 

<script> function getcaseId(elemid) {
var lkfield = document.getElementById(elemid + '_lkid').value;               --> gives u case record id
fetchCaseID(lkfield); }  
</script>


VF pape
<apex:actionFunction name="fetchCaseID" action="{!populateContact}" reRender="<contact display field> status="fetchCaseID"> <apex:param name="a" value="" assignTo="{!caseID}" /> </apex:actionFunction>
<apex:inputfield value="{!casepicklist}" onchange="getcaseId(this.id)" required="true"/>

Controller
public void populateContact()
    {   
        if(caseID!=null){
            List<case> casecontact = [select <contact> from Case where id=:caseID];
           //logic to print this contact name in VF page back
        }
    }

let me know if you have any other issues here.

thanks,
Samar
Rahul Gupta 137Rahul Gupta 137
i did the same thing   like on  if we select account i can fetch contact for that particular thing but i m unable to do it for for particular case i m unable to find contact  
apex page 

<apex:page standardController="Contact" extensions="clsContactListExample1"> <apex:form > <apex:actionStatus startText="Processing" id="idstatus"></apex:actionStatus> <apex:pageBlock title="Contact Created in last days" id="pb"> <apex:pageMessages id="msg" ></apex:pageMessages> <apex:selectList value="{!strAccId}" size="1"> <apex:selectOptions value="{!lstAcc}"> </apex:selectOptions> <apex:actionSupport event="onchange" action="{!fetchContact}" status="idstatus" reRender="pbt,msg"/> </apex:selectList> <apex:commandButton value="Fetch Contact" action="{!fetchContact}" status="idstatus" reRender="pbt,msg"/> <apex:pageBlockTable value="{!lstCon}" var="obj" id="pbt" > <apex:column value="{!obj.Id}"/> <apex:column value="{!obj.Name}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>


apex classs:
public class clsContactListExample1 
{
    public list<Contact> lstCon{get;set;}
    
    public list<selectoption> lstAcc{get;set;}
    public string strAccId{get;set;}
    
    public clsContactListExample1(ApexPages.StandardController controller) 
    {
        strAccId = '';
        lstAcc = new list<SelectOption>();
        lstAcc.add(new selectOption('','--None--'));
        for(Account obj : [select Id,Name from Account])
        {
            lstAcc.add(new selectOption(obj.Id,obj.Name));
        }
    }
    
    public void fetchContact()
    {
        lstCon = new list<Contact>();
        if(strAccId != null && strAccId.trim() != '')
        {
            lstCon = [select Id,Name from Contact where AccountId =: strAccId];     
        }
        else
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please select atleast one account.'));
        }
    }