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
IanM1963IanM1963 

Passing selected option as parameter into Apex from Visual Force Page

Hi,

 

I'm new to this, sorry if this has already been covered but I'm getting very dismayed by this not working.

 

What I need to do is have an action associated with a selection from my selectList on my VF page that will go off and use the selectedOption as an Id in a query.

 

For example the selectList has a list of accounts (Sites) and I want the user to select an account, fire the action that will then display that account on screen to the user.

 

I just keep getting the "System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: [] "

 

error message.

 

Here is the code from the VF page

 

 

<apex:selectList id="sites"  size="1" title="Manager">
                        <apex:selectOptions value="{!ClientSites}"></apex:selectOptions>
                        <apex:actionSupport event="onchange" action="{!saveCalendars}">
                        <apex:param name="site" value="{!theSite}" assignTo="{!theSite}" />
                        </apex:actionSupport>
                    </apex:selectList>

 

 

And here is the code I'm trying to get to work...

 

public Id theSite {get; set;}
   
    public PageReference saveCalendars()
    {
       
       
        theSite = ApexPages.currentPage().getParameters().get('site');
       
       
        Client_Site__c siteToUpdate =
        new Client_Site__c(Id = theSite);
        System.debug('This is the ID : ' + theSite);
        update siteToUpdate;
        return null;
       
       
    }

I really hope someone can help me with tis. Just cannot see why it won't work? The onlty thing I can think of is a timing thing with rendering etc.

 

Does anyone have suggestions on how I can fix this please?

 

Thanks in advance,

 

Ian

Chamil MadusankaChamil Madusanka

Hi,

 

try this approach

 

http://salesforceworld.blogspot.com/2011/06/parameter-passing-using.html

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

chamil's blog

IanM1963IanM1963

I still can't get it to work. What am I missing here?

 

Here is my script and the VF markup for the SelectList which gets record Ids from the database.

 

If I stick a simple alert in the JavaScript, it alerts the record number or Id which is what I want.

 

However, as soon as I make the JavaScript call myFunc then I cannot get any paramter back to the controller...

 

<script type="text/javascript">
function doSave(sel) {
    myFunc(sel.value);
}
</script>

<apex:actionFunction name="myFunc" action="{!saveCalendars}" rerender="">       
      <apex:param name="site" value=""  /> 
 </apex:actionFunction>

<apex:outputLabel value="Sites" for="sites"></apex:outputLabel>
                    <apex:selectList id="sites"  size="1" onchange="doSave(this);">
                        <apex:selectOptions value="{!ClientSites}"></apex:selectOptions>

Here's the controller... the method I'm calling anyway. Also I have a get, set like so:

 

public Id theSite {get; set;}

 

 

public PageReference saveCalendars() 
    {
        
       Id theSite = ApexPages.CurrentPage().getParameters().get('site');
        
        Client_Site__c siteToUpdate = 
        new Client_Site__c(Id = theSite);
        System.debug('This is the ID : ' + theSite);
        update siteToUpdate;
        return null;
    
        
    }

 

 

Can't believe I'm stuck on such a simple thing... :-(

 

 

Vlad OlanoVlad Olano

I think at this time you solved that...  But maybe someone has the same issue.

 

You just need to add a reference in the list..

 

                    <apex:selectList id="sites" value="{!theSite}" size="1" onchange="doSave(this);">
                        <apex:selectOptions value="{!ClientSites}"></apex:selectOptions>

 

public PageReference saveCalendars() 
    {  
        Client_Site__c siteToUpdate = 
        new Client_Site__c(Id = theSite);
        System.debug('This is the ID : ' + theSite);
        update siteToUpdate;
        return null;  
    }