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
Dman100Dman100 

access property in controller from javascript in VF page

I have updated my controller to include an Integer property:

 

public Integer ChildAccountCount { get; set; }

 

Then I set the property in my method:

 

 

public PageReference updateChildAccountOwnership() { try { Account parent = [Select Id, OwnerId From Account where Id =: parentAcct.ParentId]; List<Account> childAccts = [Select Id, OwnerId From Account where ParentId =: parent.Id]; ChildAccountCount = [Select Count() From Account where ParentId =: parent.Id]; ChildAccountCount += ChildAccountCount + 1; parent.ownerId = acctToByParent.ownerId; DbUtil.save(parent); List<Account> lst = new List<Account>(); for (Account acct: childAccts) { acct.OwnerId = acctToByParent.OwnerId; lst.add(acct); } DbUtil.save(lst); ApexPages.Message msg = new ApexPages.message(ApexPages.Severity.INFO, 'Account ownership updated successfully.'); ApexPages.addMessage(msg); acctToByParent.OwnerId = null; parentAcct.parentId = null; return null; } catch (Exception ex) { ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage()); ApexPages.addMessage(errMsg); return null; } }

 

 

In my VF page, I have a command button that calls the above method:

 

<apex:commandButton action="{!updateChildAccountOwnership}" value="Update" status="ChildAccountsLoadingStatus" rerender="ChildAccountsPageBlock, Msg" onclick="if (!confirmation()) return false;" />

 

 

I have a confirm dialog that opens before the update processes, which is where I want to display the property value.

 

I was trying to get the property value using the following method:

 

<apex:outputLabel value="{!ChildAccountCount}" id="theCount" /> <script> var total = document.getElementById('{!$Component.theCount}').value;</script>

 

The problem appears to be that the property value is not set when the confirm dialog is executed.  I want to show how many records are going to be updated before processing the update.

 

Any ideas?  It seems like I'm close, but can't get the value until after the user confirms and then the update processes and the property is set.

Message Edited by Dman100 on 10-19-2009 07:43 PM
bob_buzzardbob_buzzard

You should be able to use the property as-is:

 

 

var total = {!ChildAccountCount};

 

as the values are inserted into the page prior to it being returned to the browser. 

 

WesNolte__cWesNolte__c

Hey

 

Bob is right, you can use the value directly, but you may have to rerender the area that contains the JS so that it get's the value eg.

 

 

<apex:commandButton action="{!updateChildAccountOwnership}" value="Update" status="ChildAccountsLoadingStatus" rerender="ChildAccountsPageBlock, Msg, theJs" onclick="if (!confirmation()) return false;" />

 

 

 

 

<apex:outputPanel id="theJs"> <script> alert('{!ChildAccountCount}'); </script> </apex:outputPanel>

 

 

 This is quite a crude but should give you the bits you need to carry on. If you're looking to use this updated apex value in a JS function you could call that function from the 'oncomplete' event attribute of the commandbutton.

 

Let me know how you get on.

 

Wes