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

Contact Custom List Button - Javascript, Error When Updating More Than 2 Records

I have a custom button on a list view of contacts that allows users to check off as many as they want and then click the button. The button will check off a custom field on each contact's record.


Limited success. This works, but only when I choose 1 or 2 contacts. Any more than that and I get a "sforce is not defined" error.


Any insight as to why this is happening?


{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")} var connection = sforce.connection; var contactid = {!GETRECORDIDS( $ObjectType.Contact)}; // Make sure at least one Contact is selected if (!contactid.length) { alert("Please select at least one contact to enroll."); } else { var contact_to_update = new Array(); for (var i = 0; i < contactid.length; i++) { var callList = new sforce.SObject("Contact"); // Since we'll be using the update call, we must set the id // on the new job application record. callList.Id = contactid[i]; callList.Spring_Flying_Campaign__c="TRUE"; contact_to_update.push(callList); // Now make the update API call in a try statement so we can // catch any errors. Save the resulting array so we can also // check for problems with individual records. // var callCompleted = false; try { var result = sforce.connection.update(contact_to_update); callCompleted = true; } catch(error) { alert("Failed to update Field with error: " + error); } // Now check for problems with individual records. // if (callCompleted) { for (var i = 0; i < result.length; i++) { if (!result[i].getBoolean("success")) { alert("Contact (id='" + contactid[i] + "') could not be updated with error: " + result[i].errors); } } // Finally, refresh the browser to provide confirmation // to the user that the job applications were rejected. // window.location.reload(true); } } }


Note: Modified code from several other users but have forgotten who. So, thanks to those who shared their code!


Erica KuhlErica Kuhl
I am going to move your post to the Developer Boards as I think you will get an answer quicker on this ... let me know. 
thank you very much for the code, it's very useful !