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
JakemJakem 

Update object from Javascript

Hi everyone,  

 

I'm having a tough time figuring this out. I have a modal popup with an inputField and a javascript button that calls an actionFunction to save the object. Unfortunately I can't get the inputField values in the modal to pass to the controller so it won't update. Not sure how to fix this so I came here. Here's the code:

 

//////////
//Page
//////////

<apex:page standardController="Opportunity" extensions="extensionOpportunity" id="pgOpportunity" >

    ///JQuery
    <script src="{!URLFOR($Resource.jQueryDCS,'/js/jquery-1.5.1.min.js')}" type="text/javascript" />
    <script src="{!URLFOR($Resource.jQueryDCS,'/js/jquery-ui-1.8.13.custom.min.js')}" type="text/javascript" />
    <apex:stylesheet value="{!URLFOR($Resource.jQueryDCS,'/css/ui-lightness/jquery-ui-1.8.13.custom.css')}" />

<apex:outputPanel id="modalJQuery">

//Script to bring up modal
<script>
    
    var j$ = jQuery.noConflict();
    
    j$(document).ready(function(){
    
        j$( "#dialog-form" ).dialog({
            autoOpen: false,
            height: 325,
            width: 350,
            modal: true,
            buttons: {

                //This function calls the actionFunction
                "Save": function() {
                    afSaveOpp();
                },
                Cancel: function() {
                    j$( this ).dialog( "close" );
                }
            },
            close: function() {
                allFields.val( "" ).removeClass( "ui-state-error" );
            }
        });
    });
    
    function statusShow() {
        j$( "#dialog-form" ).dialog( "open" );
    }
    
    function statusHide() {
        j$( "#dialog-form" ).dialog( "close" );
    }
    
</script>

//The modal popup
<div id="dialog-form" title="Opportunity Edit">
<apex:form >
    <apex:pageBlock mode="maindetail">

        //Field that should be updating
        <apex:inputField value="{!opportunity.Name}" id="inName"/>
    </apex:pageBlock>
</apex:form>    
</div>
</apex:outputPanel>

<apex:outputPanel id="pnlMain">

<apex:form id="frmMain">
    
    //actionFunction that calls save method
    <apex:actionFunction id="afSaveOpp" name="afSaveOpp" action="{!save}" oncomplete="statusHide()">
    </apex:actionFunction>

</apex:form>
</apex:outputPanel>
    
</apex:page>
///////////////
//Controller
///////////////

public with sharing class extensionOpportunity {
    
    public final Opportunity o {get; set;}
    
    public extensionOpportunity(ApexPages.StandardController controller) {
        this.o = (Opportunity)controller.getRecord();
    }
    
    public void doNothing() { }
   
    public void saveOpp() {
        update o;
    }

}

 

I'd appreciate any help in understanding what's going on here.

Abhay AroraAbhay Arora

Hi,

 

Insted of using the action funtion use synchronos calls.

 

http://www.salesforce.com/us/developer/docs/ajax/Content/sforce_api_ajax_more_samples.htm

 

http://www.salesforce.com/us/developer/docs/ajax/index_Left.htm#CSHID=sforce_api_ajax_more_samples.htm|StartTopic=Content%2Fsforce_api_ajax_more_samples.htm|SkinName=webhelp

 

JakemJakem

I was able to get the update to work. The problem was that the modal I was opening stripped out the form, so I added .parent().appendTo(j$( "[id$='modal-form']" )); to the end of the modal and it now saves the object.

 

Now I want the javascript to reference the newly saved fields but it keeps getting the old field values even though the save is successful. Can't figure out what to do at this point!