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
swain 10swain 10 

can anyone please give the code example how to query two unrelated object in one apex class and conditionally populate a field in the 3rd object using the fetched data ?

can anyone please give the code example how to query two unrelated object in one apex class and conditionally populate a field in the 3rd object using the fetched data ?
I mean how to use 3 object in one apex class(fetching data from 2 object and and mapping the fetched information to 3rd object fields).
All object are unrelated.
Please help
Best Answer chosen by swain 10
NatsuNatsu
Hi Swain,

1. Use the following format to use multiple extentions:
<apex:page standardController="Expense_Line_Item__c" extensions="policyController,SaveAndNew" >
</apex:page>

2. You can use the following sample to accompalish your task:
//vf page
<apex:page standardController="Expense__c" extensions="Policy" docType="html-5.0">
    <apex:form>
    <apex:outputPanel id="p1">
        Designation <apex:selectList value="{!designation}" size="1">
            <apex:selectOptions value="{!options}" />
        	<apex:actionSupport event="onchange" action="{!populateAmount}" reRender="p1" />
        </apex:selectList>
        Amount <apex:outputText value="{!amount}" /> 
    </apex:outputPanel>
    </apex:form>
</apex:page>

//controller
public class Policy {
    public Map<String,Decimal> amountMap{get;set;}
    public String designation{get;set;}
    public list<SelectOption> options{get;set;}
    public Decimal amount{get;set;}
    public Policy(ApexPages.StandardController controller){
       options=new list<SelectOption>();
       options.add(new SelectOption('A','A'));
       options.add(new SelectOption('B','B'));
        options.add(new SelectOption('C','C'));
       options.add(new SelectOption('D','D'));
      amountMap=new Map<String,Decimal>();
        amountMap.put('A',10);
         amountMap.put('B',20);
         amountMap.put('C',30);
         amountMap.put('D',40);
        //You can construct this map by querying policy__c object and populate it with Designation vs Amount
    }
    public PageReference populateAmount(){
        amount=amountMap.get(designation);
        return null;
    }

}


 

All Answers

NatsuNatsu
Can you provide more information to your problem with a scenario, the type of conditions and the desired update required on the third object?
swain 10swain 10
Hii Natsu,
        There is 1 object policy__c which consist policy information like designation and eligible amount for each designation in the form of records(1 record in policy object indicate 1 designation ) so it is treated as master data.
There is 2nd object Expense__c and 3rd object ExpenseLineitem__c which are in parent child relationship like quote and quotelineitem.
So the main scenerio is that we have to auto populate Amount of corresponding designation to a amount field present in ExpenseLineItem__c by fetching from Policy__c object.
Condition: when ever a picklist field in ExpenseLineItem__c Changes and there is a designation field in Expense__c (which designation is selected on that basic it will check the policy__c and fetch the particular record amount).

So to meet this requirement by using onchange function i am calling javascript and through script to apex:supportfunction calling to apex where we will write the main logic.
Field should be autopopulated at the same time when piclist value changes without whole page refreshment.
Please help.

VisualForce:

apex:page standardController="Expense_Line_Item__c" extensions="policyController" EXtensions="SaveAndNew" showHeader="true" >
 <apex:slds />  
 <div class="slds-scope">   
<script>
     function fetchAmount()
     {
     alert('Function Working');
     CallApexMethod();
     }
    
 </script>
         <apex:form >
         <apex:actionFunction name="CallApexMethod"  onComplete="alert('After apex method') ;" reRender="ren"/>
        <apex:outputPanel id="t1">
       
            <apex:pageBlock >                
                <apex:pageBlockSection columns="2" title="Information"> 
                    <apex:inputField value="{!Expense_Line_Item__c.Expense__c}" />
                    <apex:inputField value="{!Expense_Line_Item__c.Date__c}" />
                    <apex:inputField value="{!Expense_Line_Item__c.Name}" />
                    <apex:inputField value="{!Expense_Line_Item__c.Bill_Available__c}" />
                    <apex:inputField value="{!Expense_Line_Item__c.Merchant__c}" />
                </apex:pageBlockSection>
                <apex:pageBlockSection columns="2" title="Expense Details">
                    <apex:inputField value="{!Expense_Line_Item__c.Approved__c}" />
                    <apex:inputField value="{!Expense_Line_Item__c.Amount__c}" id="testAmount" />
                    <apex:inputField value="{!Expense_Line_Item__c.Cost_Head__c}" >
                        <apex:actionSupport event="onchange" rerender="t1" />            
                    <apex:inputField value="{!Expense_Line_Item__c.Related_to_Campaign__c}" rendered="{!IF( Expense_Line_Item__c.Cost_Head__c== 'Campaign',true,false  )}" />
                    <apex:inputField value="{!Expense_Line_Item__c.Related_to_Opportunity__c}" rendered="{!IF( Expense_Line_Item__c.Cost_Head__c== 'Opportunity',true,false  )}" />
                    <apex:inputField value="{!Expense_Line_Item__c.Project__c}" rendered="{!IF( Expense_Line_Item__c.Cost_Head__c== 'Project',true,false  )}" />                                                            
                </apex:inputField>
                    <apex:inputField value="{!Expense_Line_Item__c.Payment_Type__c}" />
                    <apex:inputField value="{!Expense_Line_Item__c.Comments__c}" />
                    <apex:outputPanel id="ren">
                    <apex:inputField value="{!Expense_Line_Item__c.Expense_Head__c}"  id="fetch1"  onchange="fetchAmount();">
                       <apex:actionSupport event="onchange" rerender="t1" />
                     
                    </apex:inputField>
                    </apex:outputPanel>
                </apex:pageBlockSection>
                
                 <!--  <apex:pageBlockTable value="{!Expense_Line_Item__c}" var="item" columns="2" >
               
      <apex:column> <apex:outputLabel >  <apex:inputField label="City"  value="{!item.City__c}" rendered="{!IF( Expense_Line_Item__c.Expense_Head__c== 'Travel',true,false )}" /></apex:outputLabel>
                   <apex:inputField label="Mode of travel" value="{!item.Mode_Of_Travel__c}" rendered="{!IF(Expense_Line_Item__c.Expense_Head__c== 'Travel',true,false  )}" />
                  <apex:inputField label="Location from" value="{!item.Location_From__c}" rendered="{!IF( Expense_Line_Item__c.Expense_Head__c== 'Travel',true,false  )}" /> 
                                 <apex:inputField label="Location to" value="{!item.Location_To__c}" rendered="{!IF( Expense_Line_Item__c.Expense_Head__c== 'Travel',true,false  )}" /> </apex:column>
       <apex:column>       
                   <apex:inputField value="{!item.Sub_Expense__c}" rendered="{!IF( Expense_Line_Item__c.Expense_Head__c== 'Food',true,false  )}" /> </apex:column>
         <apex:column>           <apex:inputField value="{!item.Sub_Expense__c}" rendered="{!IF(Expense_Line_Item__c.Expense_Head__c== 'Entertainment',true,false  )}" /> </apex:column>
          <apex:column>          <apex:inputField value="{!item.Sub_Expense__c}" rendered="{!IF(Expense_Line_Item__c.Expense_Head__c== 'Lodging',true,false  )}" /> </apex:column>
           <apex:column> <apex:inputField value="{!item.Sub_Expense__c}" rendered="{!IF( Expense_Line_Item__c.Expense_Head__c== 'Others',true,false  )}" /> </apex:column>
                                                              
               </apex:pageBlockTable>-->
              
               <apex:pageBlockSection columns="2">
               
                    <apex:inputField value="{!Expense_Line_Item__c.City__c}" rendered="{!IF( Expense_Line_Item__c.Expense_Head__c== 'Travel',true,false )}" />
                    <apex:inputField value="{!Expense_Line_Item__c.Mode_Of_Travel__c}" rendered="{!IF( Expense_Line_Item__c.Expense_Head__c== 'Travel',true,false  )}" />
                    <apex:inputField value="{!Expense_Line_Item__c.Location_From__c}" rendered="{!IF( Expense_Line_Item__c.Expense_Head__c== 'Travel',true,false  )}" />
                    <apex:inputField value="{!Expense_Line_Item__c.Location_To__c}" rendered="{!IF( Expense_Line_Item__c.Expense_Head__c== 'Travel',true,false  )}" />
              
                   <apex:inputField value="{!Expense_Line_Item__c.Sub_Expense__c}" rendered="{!IF( Expense_Line_Item__c.Expense_Head__c== 'Food',true,false  )}" />
                    <apex:inputField value="{!Expense_Line_Item__c.Sub_Expense__c}" rendered="{!IF( Expense_Line_Item__c.Expense_Head__c== 'Entertainment',true,false  )}" />
                    <apex:inputField value="{!Expense_Line_Item__c.Sub_Expense__c}" rendered="{!IF( Expense_Line_Item__c.Expense_Head__c== 'Lodging',true,false  )}" />
                    <apex:inputField value="{!Expense_Line_Item__c.Sub_Expense__c}" rendered="{!IF( Expense_Line_Item__c.Expense_Head__c== 'Others',true,false  )}" />
                                                              
               </apex:pageBlockSection>             
            </apex:pageBlock>
            <div align="center" draggable="false" >
            <apex:commandButton action="{!Save}" value="Save" />
            <apex:commandButton action="{!Cancel}" value="Cancel"/>
            <apex:commandButton action="{!saveNew}" value="Save and New"/>
                       </div> 
        </apex:outputPanel>
    </apex:form>
    </div>
</apex:page>

Apex Class:(For checking purpose I am just want to give a static amount but its not linking to the vf also)

public with sharing class policyController 
{

    public policyController(ApexPages.StandardController controller) 
    {

    }


     public List<Policy__c> pol{get;set;}
     public List<Expense__c> exp{get;set;}
     

     public  PageReference polCon()
      {
         list<Expense_Line_Item__c>  expl= new list<Expense_Line_Item__c>();
         pol = [select Name , Cash_Advance__c ,Flight_Eligible__c , Per_Diem__c ,
                        Local_Hotel__c , Local_Coveyance__c  from Policy__c ];
         
         exp= [select Name , Designation__c from Expense__c];
         
         for(Expense_Line_Item__c expli:expl)
         {
         
          expli.Amount__c=1000 ;
         
         }
         
          return null;
      }

}
NatsuNatsu
Hi Swain,

1. Use the following format to use multiple extentions:
<apex:page standardController="Expense_Line_Item__c" extensions="policyController,SaveAndNew" >
</apex:page>

2. You can use the following sample to accompalish your task:
//vf page
<apex:page standardController="Expense__c" extensions="Policy" docType="html-5.0">
    <apex:form>
    <apex:outputPanel id="p1">
        Designation <apex:selectList value="{!designation}" size="1">
            <apex:selectOptions value="{!options}" />
        	<apex:actionSupport event="onchange" action="{!populateAmount}" reRender="p1" />
        </apex:selectList>
        Amount <apex:outputText value="{!amount}" /> 
    </apex:outputPanel>
    </apex:form>
</apex:page>

//controller
public class Policy {
    public Map<String,Decimal> amountMap{get;set;}
    public String designation{get;set;}
    public list<SelectOption> options{get;set;}
    public Decimal amount{get;set;}
    public Policy(ApexPages.StandardController controller){
       options=new list<SelectOption>();
       options.add(new SelectOption('A','A'));
       options.add(new SelectOption('B','B'));
        options.add(new SelectOption('C','C'));
       options.add(new SelectOption('D','D'));
      amountMap=new Map<String,Decimal>();
        amountMap.put('A',10);
         amountMap.put('B',20);
         amountMap.put('C',30);
         amountMap.put('D',40);
        //You can construct this map by querying policy__c object and populate it with Designation vs Amount
    }
    public PageReference populateAmount(){
        amount=amountMap.get(designation);
        return null;
    }

}


 
This was selected as the best answer