Goal: A user should expect to increment a custom object's (number type) field by 1 each time the custom button is clicked.

Initial block:


    //initializing the custom object
        var updateMediaPlacement = new sforce.SObject("Media_Placement__c");

        updateMediaPlacement.Id = "{!Media_Placement__c.Id}";

    result = sforce.connection.update(updateMediaPlacement);

This may be an obvious exercise to some, but I overcomplicated it initially and after trawling through Google, wanted to document my solution for javascript/Salesforce beginners:


var mpArray = [];

    var updateMP = new sforce.SObject("Media_Placement__c");

    updateMP.Id = "{!Media_Placement__c.Id}";

    var incorrectCount = "{!Media_Placement__c.Incorrect_Name_Count__c}"


    //Intializing a number data object
    var newNumber = Number(0);


    //Ensuring 1 is initialized as a number when added to the incorrectCount SObject field
    incorrectCount = newNumber + 1;


    //Adding the existing value in the field; if field is null SF passes in 0 digit value
    updateMP.Incorrect_Name_Count__c = {!Media_Placement__c.Incorrect_Name_Count__c} + incorrectCount;


    result = sforce.connection.update(mpArray);

//don't forget error handling-- for another thread

This by itself is likely overcomplicated, but I wanted to ensure that I wasn't concatenating strings but actually performing a mathematical operation since Javascript is infamously loosely typed.

Hope this helps!

  • WHat was the issue?
    – Eric
    Mar 5 '17 at 6:33
  • @Eric runtime kept returning 'unexpected number' or 'unknown' value issues. The other issue is that Javascript would convert the number field's stored value into a string, and so the '+' operator would concatenate the values instead of add them. I addressed this by initializing a new number object and adding that to the existing field value and then assigning it back to the field
    – GPP
    Mar 5 '17 at 20:17

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.