1

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);
2

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

  • 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
1

I believe in the last line you should replace the line

result = sforce.connection.update(updateRecords);

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

0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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