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
ChopaChopa 

Passing Argument From VFPage to Controller

Hi,

My goal is this: In a custom Case layout, when a user clicks on an image, a lookup window shows up and the user can select one of the items displayed in it. These items have a child releationship with the Case object. Upon selection, the lookup window closes and a record is inserted. The record contains the CaseID and the childID that is assigned to the item selected in the lookup window.

To achieve this, I am using 2 vfpages, one of them contains the image that will be displayed in the Case layout. The other is my lookup page.

Here's the code of my "image" vfpage:

<apex:page StandardController="Case" extensions="RWW_Case_PunchClock_Controller" id="myApexPageId">
   
    <apex:form >             
        <apex:actionFunction name="punchIn" action="{!punchIn}" rerender="myApexPageId">
           <apex:param name="vCaseChargeTypeId" assignTo="{!oCaseChargeTypeId}" value=""/>
        </apex:actionFunction>
        <img id="imgClockPunch" src="{!$Resource.PunchIn}" onclick="javascript&colon; openLookup()"/>
    </apex:form>
   
    <script language = "javascript">
         var win;   
       
        function openLookup()
        {
            win = window.open('/apex/rww_case_charge_type_lookup', 'Select a charge type', 'height=500, width=300');
            win.focus();
        }
       
        function setSelectedChargeType(myVal)
        {           
            vCaseChargeTypeId = myVal;
            punchIn();
            win.close();
        }      
     
    </script>
</apex:page>

Here's my Image Controller code:

public class RWW_Case_PunchClock_Controller
{
    public string oCaseId {get; private set; }
    public string oCaseChargeTypeId { get; set; }   
       
    public RWW_Case_PunchClock_Controller(ApexPages.StandardController controller)
    {
        Case objCase = (Case)controller.getRecord();
        oCaseId = objCase.Id;        
    }

    public RWW_Case_PunchClock_Controller()
    {       
    }
    
    public PageReference punchIn()
    {
        Case_Charge_Type__c[] objChargeType = [Select id, Billable__c, Rate__c from Case_Charge_Type__c where Id =: oCaseChargeTypeId];

        Case_Time_Detail__c objTimeDetail = new Case_Time_Detail__c(Case__c = oCaseId, Billable__c = objChargeType[0].Billable__c,Rate__c = objChargeType[0].Rate__c,
                                                                    Start_Date__c = DateTime.Now(), Case_Charge_Type__c = objChargeType[0].Id);
        insert objTimeDetail;
        oPunchState = '';
        return null;
    }    
}



Here's my "lookup" vfpage code:

<apex:page Controller="RWW_Case_Charge_Type_Controller" showHeader="false" id="fullPage">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!ChargeTypes}" var="items">
                    <apex:column headerValue="Charge Type Name" >
                        <apex:commandLink value="{!items.Name}" action="{!setId}" >
                            <apex:param name="selectedChargeTypeId" assignTo="{!selectedId}" value="{!items.Id}" />
                        </apex:commandLink>
                    </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
    <script language="javascript">
        function SendValueToParent()
        {                    
            window.top.opener.setSelectedChargeType("{!selectedId}");
            close();
        }
       
        if("{!selectedId}" != null && "{!selectedId}" != "")
        {
            SendValueToParent();
        }
    </script>
</apex:page>

Here's my Lookup Controller code:

public class RWW_Case_Charge_Type_Controller
{
    public List<Case_Charge_Type__c> ChargeTypes { public get; private set; }
    public string selectedId{ get; set; }
    
    public RWW_Case_Charge_Type_Controller()
    {
        ChargeTypes = [Select id, name from Case_Charge_Type__c];
    }
    
    public PageReference setId()
    {
    selectedId = ApexPages.currentPage().getParameters().get('selectedChargeTypeId');
    return null;
    }
}


The selected id is retrieved succesfully from my image vfpage. My problem is that even if I assign its value to my javascript-controller variable, the value is not given properly to the controller when invoking my punchIn method. In fact, the controller is not receiving the value at all, like it was never set.

I also tried to use a static variable inside a global class to pass my selectedChildId from my lookup controller to my "image controller". But again, the static variable is not set according to my Image controller.

Also, I am using a custom lookup page instead of invoking the built-in javascript method "openLookup" because in the future, I intend to add some validations/code to the onClose/onOpen event of the lookup page.

My question: Do you know why my selected "Child Id" is not being passed to my imageController once I have retrieved it in my vfpage ?

Thanks for reading my post

Best Answer chosen by Admin (Salesforce Developers) 
ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

Hi Chopra,

I guess the system is not being able to catch the hidden field in the js function. You can try one more trick.
Place the <apex:inputHidden> above your img and pass the id of the input Hidden to your openlookup function
i.e.
<apex:inputHidden id="hidChildId" value="{!oCaseChargeTypeId}" />
   <img id="imgClockPunch" src="{!$Resource.PunchIn}" onclick="javascript&colon; openLookup('{!$Component.hidChildId}')"/>

declare a global js variable

var hidChild; // global variable
function openLookup(input)
        {
            hidChild = input;
            win = window.open('/apex/rww_case_charge_type_lookup', 'Select a charge type', 'height=500, width=300');
            win.focus();
        }
       
        function setSelectedChargeType(myVal)
        {  
            alert(document.getElementById(hidChild));   // to check if proper field is being refered         
            document.getElementById(hidChild).value = myVal;

            hidChild = '';
            punchIn();
            win.close();
        }    

 

 

Hope this works for you.

All Answers

ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

Hi Chopra,

Try using an 'apex:inputhidden' field instead of 'apex:param' and the set value of the hidden field in your 'setSelectedChargeType' function

i.e. add

<apex:inputHidden id="hidChildId" value="{!oCaseChargeTypeId}" />

<script>

      var objHidChildId = document.getElementById{'{!$Component.hidChildId}'};

</script>

 

        function setSelectedChargeType(myVal)
        {           
            objHidChildId.value = myVal;
            punchIn();
            win.close();
        }

 

Hope this helps.  

ChopaChopa

Hello Ipsota and thanks for your reply.

I have tried your suggestion but unfortunatly, it is not working.

It seems the line  "objHidChildId.value = myVal;" is the source of the error because I called alert() before and after that line of code and only the "before" one showed up. I also triple checked the variables' name (I used the names provided in your example).

 

Is there some js file I need to include to be able to use this approach ? or is the fact that my controller is an extension and not a standard controller changing anything ?

 

Thanks for your help, I really appreciate it.

 

Chopa

 

ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

Hi Chopra,

I guess the system is not being able to catch the hidden field in the js function. You can try one more trick.
Place the <apex:inputHidden> above your img and pass the id of the input Hidden to your openlookup function
i.e.
<apex:inputHidden id="hidChildId" value="{!oCaseChargeTypeId}" />
   <img id="imgClockPunch" src="{!$Resource.PunchIn}" onclick="javascript&colon; openLookup('{!$Component.hidChildId}')"/>

declare a global js variable

var hidChild; // global variable
function openLookup(input)
        {
            hidChild = input;
            win = window.open('/apex/rww_case_charge_type_lookup', 'Select a charge type', 'height=500, width=300');
            win.focus();
        }
       
        function setSelectedChargeType(myVal)
        {  
            alert(document.getElementById(hidChild));   // to check if proper field is being refered         
            document.getElementById(hidChild).value = myVal;

            hidChild = '';
            punchIn();
            win.close();
        }    

 

 

Hope this works for you.

This was selected as the best answer
ChopaChopa

Thanks a lot, you solved my problem.