Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hello Guys I got this code on my custom object in a button I want to update some fields. However I keep getting "A problem with the OnClick JavaScript for this button or link was encountered:

updateRecords is not defined"

How do I fix this. Below is my code:

{!requireScript("/soap/ajax/34.0/connection.js")}

var updatedRecords = new Array();
var jobRequisition = new sforce.SObject("Job_Requisition__c");
jobRequisition.id = "{!Job_Requisition__c.Id}";
jobRequisition.requisition__c = "Something";

updatedRecords .push(jobRequisition);

result = sforce.connection.update(updateRecords);
share|improve this question
    
Hi Nelson, if your problem is resolved, please accept an answer to close it. Thanks! – Rob Sep 20 '15 at 9:38
up vote 2 down vote accepted

Watch out for your variable names - you're trying to update updateRecords but your variable is called updatedRecords.

share|improve this answer
    
Hello Rob, I have made the change and now my code runs and I put an alert to show the value of the updated field but it does not look like it is persisting. What am I doing wrong? – Nelson Chisoko Sep 18 '15 at 13:15
1  
Hi Nelson, I would suggest you take a look at the update example from developer.salesforce.com/docs/atlas.en-us.ajax.meta/ajax/… to see how you can debug this issue. If that doesn't help, you'll be better off closing this question and opening a new one for the persistence issue. – Rob Sep 18 '15 at 13:46

I believe in the last line you should replace the line

result = sforce.connection.update(updateRecords);

with result = sforce.connection.update(updatedRecords);

share|improve this answer

When you update record with Ajax Toolkit then the returned result is an JS object like this result{id:'001i000001d89wbAAA', success:'true', }in case success. You get the Id of the record updated. If you want to get the field value you have to Query again with this returned Id. The method is like thissObject[] result = connection.retrieve(string fieldList, string sObjectType, ID ids[]);

Here is an example. I am first creating a Account record with myName and then updating it with a phone no. To get the updated phone no, I have retrieved the Account with the returned Id. Then you can get the phone no like this qRresult[0].Phone. Hope this helps.

<apex:page showHeader="true" sidebar="true">
    <script type="text/javascript">
        var __sfdcSessionId = '{!GETSESSIONID()}';
    </script>
    <script src="../../soap/ajax/34.0/connection.js" type="text/javascript"></script>
    <script type="text/javascript">
        //create an account
        var account = new sforce.SObject("Account");
        account.Name = "myName";
        result = sforce.connection.create([account]);

        //update that account
        account.id = result[0].id;
        account.Phone = "1234567890";
        result = sforce.connection.update([account]);
        console.log("result" +result);
        var qRresult;
        if (result[0].getBoolean("success")) {
          console.log("account with id " + result[0].id + " updated");
          qRresult = sforce.connection.retrieve("Name,Phone", "Account", [result[0].id]);
          console.log("result" +qRresult);
          console.log("result" +qRresult[0].Phone);
        } else {
          console.log("failed to update account " + result[0]);
        }   
    </script>

</apex:page>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.