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
Meer SalmanMeer Salman 

Call script function behind event?

Hi,

 

Is this possible to call the script functions on the events of a component or in <apex:actionsuppor>? I have the following function in a script which I want to be called on the 'onselect' event of the <apex:outputfeild>.

 

    <script type="text/javascript"> var __sfdcSessionId = '{!GETSESSIONID()}'; </script>
    <script src="../../soap/ajax/24.0/connection.js" type="text/javascript"> </script>
    <script type="text/javascript">     
       
function ShowDescription(acc) 
        {
            var queryResult=sforce.connection.query(<my query>);
            var records=queryResult.getArray('records');
            document.getElementById(acc.id.replace("acc","desc")).value=records[0].Description__c; 
        }
        
    </script> 
    <apex:pageBlock mode="inlineEdit" title="Journal Detail" id="td">        
        <apex:pageBlockSection columns="2">
            <apex:outputField id="period_date" value="{!Journal.Period__c}">
                <apex:actionSupport event="onchange" action="ShowJournalDate(period_date)" rerender="journal_date"/>
            </apex:outputField>
            <apex:outputLabel />
            <apex:outputField id="journal_date" value="{!Journal.Journal_Date__c}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>

 

Regards,

Meer Salman

bob_buzzardbob_buzzard

This doesn't really make sense in the context of an outputfield - the value can't be changed.  Events like onchange, onselect are only supported by a few HTML elements, usually those that involve user input.

 

Can you explain a little more about what you are trying to achieve?

Meer SalmanMeer Salman

I agree with you, but here I am having a <apex:pageBlock> in mode="inlineEdit", so I want to change the value of the Journal.Journal_Date__c with the change of Journal.Period__c  value. I didn't find any of the way to do it by extension so i thought to do it by script. I have the following VFPage and Class

 

*****EXTENSION*****

public class Fin_Edit_Journal
     public Fin_Journal__c Journal {get;set;}
  
     //Constructors
     public Fin_Edit_Journal()
     {
          Journal =[SELECT id, Journal_Name__c, Description__c, Period__c, Journal_Date__c FROM Fin_Journal__c WHERE id =: ApexPages.currentPage().getParameters().get('id')];                    
     }
     public Fin_Edit_Journal(ApexPages.StandardController controller)
     {
          Journal =[SELECT id, Journal_Name__c, Description__c, Period__c, Journal_Date__c FROM Fin_Journal__c WHERE id =: ApexPages.currentPage().getParameters().get('id')];                    
     }
 
     public PageReference ShowJournalDate()
     {
        Fin_Periods__c p = [SELECT id, PrdStDate__c FROM Fin_Periods__c WHERE Name =: Journal.Period__c];
        Journal.Journal_Date__c = p.PrdStDate__c;
        return null;
     }
}

 

 

*****VFPage*****

<apex:page standardController="Fin_Journal__c" extensions="Fin_Edit_Journal">
    
    <script type="text/javascript"> var __sfdcSessionId = '{!GETSESSIONID()}'; </script>
    <script src="../../soap/ajax/24.0/connection.js" type="text/javascript"> </script>
    <script type="text/javascript">             
        function ShowJournalDate(period) 
        {
            var queryResult=sforce.connection.query("SELECT id,PrdStDate__c FROM Fin_Periods__c WHERE Name ='"+period.value+"'");
            var records=queryResult.getArray('records');            
            var temp= records[0].PrdStDate__c.split("-");
            var dt= temp[1]+"/"+temp[2]+"/"+temp[0];
            document.getElementById(period.id.replace("period_date","journal_date")).value=dt; 
        }
    </script> 
    
    <apex:form id="MyForm" >
    
    <apex:pageBlock mode="inlineEdit" title="Journal Detail" id="td">        
        <apex:pageBlockSection columns="2">
            <apex:outputField id="period_date" value="{!Journal.Period__c}" onchange="ShowJournalDate(this)"/>
            <apex:outputLabel />
            <apex:outputField id="journal_date" value="{!Journal.Journal_Date__c}"/>
            <apex:outputLabel />
            <apex:outputField value="{!Journal.Card__c}"/>
            <apex:outputLabel />
            <apex:outputField value="{!Journal.Description__c}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
    
    </apex:form>
      
</apex:page>
Thanks Bob
Regards,
Meer Salman


Meer SalmanMeer Salman

I tried it some of the other ways as well but it didn't give a nice look the layout. 

 

Regards,

Meer Salman

bob_buzzardbob_buzzard

What does the 'inlineEdit' mode do?  According to the docs, that isn't one of the supported values.

Meer SalmanMeer Salman

If I am not wrong  I guess by using inlineEdit you can view the values of the fields and if want to edit you can double click on it.

 

http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_inlineEditSupport.htm

 

check the mode of the <apex:pageblocktable>

 

Please correct me if I am wrong.

 

Thanks

 

 

bob_buzzardbob_buzzard

The mode attribute for a pageblocktable only controls the display aspect as far as I'm aware.  That link is demonstrating the inlineeditsupport element, nested in the outputfield:

 

<apex:outputField value="{!contact.lastname}">
   <apex:inlineEditSupport showOnEdit="saveButton, cancelButton" hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
</apex:outputField>

 

Meer SalmanMeer Salman

I guess even if you remove inlineeditsupport element it works the same way, anyhow let make it your way, now we can understand that the user can change the value of the field in this mode. Now what I want is that when the value for the Journal.Period__c  is changed Journal.Journal_Date__c should also get equal to it.

 

I guess you understood the issue now.

 

Many Thanks

bob_buzzardbob_buzzard

If Journal.Period__c is going to have the inlineedit support, I suspect you'll need the user to save the record in order to be able to populate another field with the value.  The Salesforce javascript will be controlling this element and I don't know how easy it would be to hook on the end of that.