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
Abhinav GuptaAbhinav Gupta 

Unable to update Account sobject using javascript remoting.

Hi Everyone,

 

I can easily query and play around with results using Javascript remoting, but when trying to update the record using the remoting again. It seems its rolling back my changes without any errors.

 

Here is code snippet

 

Controller Class

 

 

public global with sharing class  testremotingcontroller {
    
     
    @RemoteAction
    global static Account[] searchAccounts(String accountName) {
        accountName = accountName.replaceAll('[*]', '%');
        return [select id, name, phone, type, numberofemployees from 
             Account where name like :accountName ];
        
    }   
    
    @RemoteAction
    global static boolean updateAccount(String accountId, String phoneNum) {
        Account a = [Select Phone from Account where Id = :accountId];
        a.Phone = phoneNum;
        update a;
        // querying again to see if its fixed.
        a = [Select Phone from Account where Id = :accountId];
        // This debug always prints the updated value
        system.debug('>>> accId'+ accountId + 
                    ' phone ' +  phoneNum +
                     ' ' +a );
        return true;
    }
}

 

 

Visualforce Page

 

<apex:page controller="testremotingcontroller">
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"/>
<apex:includeScript value="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"/>

<apex:sectionHeader title="Javascript Remoting & jQuery Templates !"/>

<apex:pageBlock title="Accounts">
    <apex:pageBlockSection title="Search Accounts" columns="2">
        <apex:pageBlockSectionItem >
            Account Name :
            <input type = "text" id = "accountNameToSearch" />
            <button onclick="searchAccounts()">Get Account</button>
        </apex:pageBlockSectionItem>        
    </apex:pageBlockSection>
    <apex:pageBlockSection title="Matching Accounts !" columns="1">
    <!-- 
	Created Empty table using the CSS styles of visualforce pageBlockTable 
	This gives same look and feel 
    -->
    <table cellspacing="0" cellpadding="0" border="0" id="searchResults" class="list ">
        <colgroup span="2"></colgroup>
        <thead class="rich-table-thead">
            <tr class="headerRow ">
                <th colspan="1" scope="col" class="headerRow">Id</th>
                <th colspan="1" scope="col" class="headerRow"> Name</th>
                <th colspan="1" scope="col" class="headerRow"> Phone</th>
                <th colspan="1" scope="col" class="headerRow">Type</th>
                <th colspan="1" scope="col" class="headerRow"> Number of Employees</th>                                 
            </tr>
        </thead>
	<!-- table body left empty for populating via row template using jquery -->
        <tbody />
    </table>
    </apex:pageBlockSection>
</apex:pageBlock>

<!-- Create a named jquery template -->
<script id="resultTableRowTemplate" type="text/x-jquery-tmpl">
<tr onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" onmouseout="if (window.hiOff){hiOff(this);} " onmouseover="if (window.hiOn){hiOn(this);} " class="dataRow even  first">
    <td class="dataCell">${Id}</td>
    <td class="dataCell">${Name}</td>
    <td class="dataCell"><input type="text" value="${Phone}" /></td>
    <td class="dataCell">${Type}</td>        
    <td class="dataCell">${NumberOfEmployees}</td>
</tr>           
</script>


<script type="text/javascript">
// if you are inside some component
// use jquery nonConflict
// var t$ = jQuery.noConflict();

function searchAccounts() {
    var accountName = $('#accountNameToSearch').val();
    // clear previous results, if any
    $("#searchResults tbody").html('');
    
    // The Spring-11 gift from force.com. Javascript remoting fires here
    // Please note "abhinav" if my org wide namespace prefix
    // testremotingcontroller is the Apex controller
    // searchAccounts is Apex Controller method demarcated with @RemoteAction annotation.
    abhinav.testremotingcontroller.searchAccounts( accountName, function(result, event){            
        if (event.status && event.result) {  
          $.each(event.result, function () {                
          // for each result, apply it to template and append generated markup to the results table body.
                  var row = $("#resultTableRowTemplate" ).tmpl(this);
		  // locate the Phone field and bind this account and blur handler to it
		  row.find('td input').data('account', this).blur(
			function () {
				var orginalAcc = $(this).data('account');
				if (orginalAcc.Phone != $(this).val()) 
					updateAccount(orginalAcc, $(this).val());
			}
		  );
		  row.appendTo( "#searchResults tbody" );
              }
          );            
        } else {
           alert(event.message);
        }
    }, {escape:true});
}

function updateAccount(acc, newPhoneVal) {
	abhinav.testremotingcontroller.updateAccount( acc.Id, newPhoneVal, function(result, event){            
        if (event.status && event.result) {  		
	   alert('Account - '  + acc.Name + ' updated  !');
        } else {
           alert(event.message);
        }
    }, {escape:true});
}
</script>

</apex:page>

 I am pasting the whole code as it is without any trimming. So that one can easily reproduce this by copy pasting it directly.

 

joshbirkjoshbirk

Don't have an answer yet, but we're looking into this.

Abhinav GuptaAbhinav Gupta

Thanks Josh for update. 

cwall_sfdccwall_sfdc

The issue has been confirmed and we're working through a solution.  Expect this to be fix soon.

stephanstephan

The issue should be fixed now....

Abhinav GuptaAbhinav Gupta

Thanks Stephan, I already came to know about this via Twitter and did a blog for the same few days back( http://www.tgerm.com/2011/03/view-state-less-visualforce.html )

 

Remoting is fast and awesome !