0

I have a button on the Contacts' Related list on the Accounts Page Layout; when this button is clicked I want it to update a checkbox field on the Contact to true.

I created this code, which compiles without error but it does not fire:

var selectedRecords = {!GETRECORDIDS( $ObjectType.Contact )}; 

if( selectedRecords.length > 0 ) { 
    var ContactsToUpdate = []; 
    selectedRecords.forEach( 
        function( recId ) { 
            var Contact = new sforce.SObject( "Contact" ); 
            Contact.Id = recId; 
            Contact.On_Cadence__c = true; 

            ContactsToUpdate.push( "Contact" ); 
        } 
    ); 

    sforce.connection.update( ContactsToUpdate ); 

    location.reload();
}  else {
    alert( "Please select atleast one record." ); 
}
2
  • Javascript isn't compiled, it's interpreted. That means that you won't see any errors until you try to run it. A good place to start looking would be in the console of your browser's developer tools (all the current browsers that I use have F12 as the shortcut key to open the dev tools).
    – Derek F
    Mar 3, 2017 at 20:54
  • Try ContactsToUpdate.push( Contact ); . Also in javascript sometimes true has to be referenced as 'true'. Play with these 2...
    – o-lexi
    Mar 3, 2017 at 21:02

1 Answer 1

1

ContactsToUpdate.push() should take the ContactObj variable instead of hadcoded "Contact"

{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} 
var selectedRecords = {!GETRECORDIDS($ObjectType.Contact)}; 

if( selectedRecords.length > 0 ) 
{ 
    var ContactsToUpdate = []; 

    for (var i = 0; i < selectedRecords.length; i++) {
        var ContactObj = new sforce.SObject( "Contact" ); 
        ContactObj.Id = selectedRecords[i]; 
        ContactObj.On_Cadence__c = true; 
        ContactsToUpdate.push(ContactObj); 
    }

    sforce.connection.update( ContactsToUpdate ); 
    location.reload();  
}  
else 
{
    alert( "Please select atleast one record." ); 
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .