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
BrenzoBrenzo 

Incremental Counter Not Counting Properly

I have a custom javascript button on the Opportunity object that when clicked should icnrease the value of a numerical field by '1' but instead it's simply adding '1' to the existing value. So if the number in the field is currently '1' then clicking the button changes it to '11' and clicking again changes it to '111' and so on... it's as if it is simply appending a '1' to the end of the numerical string.

Here is my code to update the counter field 'VelocifyResync':
 
{!REQUIRESCRIPT("/soap/ajax/38.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/38.0/apex.js")} 

var opp = new sforce.SObject("Opportunity");
opp.Id = '{!Opportunity.Id}';
var VelocifyResync = '{!Opportunity.Velocify_Resync__c}';

// assign values to fields on opportunity object

opp.Velocify_Resync__c = VelocifyResync + 1;

//save the change
var result = sforce.connection.update([opp]);

if(result[0].getBoolean("success")) { 
window.location.reload();;
} else { 
alert('Error : '+result);
}
Appreciate any help here! 
Best Answer chosen by Brenzo
Rohit B ☁Rohit B ☁
Try this :-
 
{!REQUIRESCRIPT("/soap/ajax/38.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/38.0/apex.js")} 

var opp = new sforce.SObject("Opportunity");
opp.Id = '{!Opportunity.Id}';
opp.Velocify_Resync__c = '{!Opportunity.Velocify_Resync__c}';

// assign values to fields on opportunity object
if(opp.Velocify_Resync__c == "" || opp.Velocify_Resync__c == null) {
opp.Velocify_Resync__c = 0;
} else {
opp.Velocify_Resync__c = parseInt(opp.Velocify_Resync__c) + 1;
}

//save the change
var result = sforce.connection.update([opp]);

if(result[0].getBoolean("success")) { 
window.location.reload();;
} else { 
alert('Error : '+result);
}

 

All Answers

LBKLBK
Hi Brenzo,

A var is text by default in Javascript.

Replace line 6 of your code with the following code and you will be good.
 
var VelocifyResync = Number('{!Opportunity.Velocify_Resync__c}');

 
Rohit B ☁Rohit B ☁
Hi Brenzo,

Try this :-
 
{!REQUIRESCRIPT("/soap/ajax/38.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/38.0/apex.js")} 

var opp = new sforce.SObject("Opportunity");
opp.Id = '{!Opportunity.Id}';
var VelocifyResync = '{!Opportunity.Velocify_Resync__c}';

// assign values to fields on opportunity object

opp.Velocify_Resync__c = parseInt(VelocifyResync) + 1;

//save the change
var result = sforce.connection.update([opp]);

if(result[0].getBoolean("success")) { 
window.location.reload();;
} else { 
alert('Error : '+result);
}

It should work for you.. :)
BrenzoBrenzo
Great, both of those appear to be working, but now I've got a second issue...

Some of records have a. blank/null value in the counter field (I've since updated this field to have a default of '0' moving forward, but that doesn't help me retroactively.)

Is there an easy way to put. some if/then conditional logic in place so that if the value in that field is blank or null it'll update it to '0' otherwise increase count by '1'. Here is what I attempted:
 
{!REQUIRESCRIPT("/soap/ajax/38.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/38.0/apex.js")} 

var opp = new sforce.SObject("Opportunity");
opp.Id = '{!Opportunity.Id}';
opp.Velocify_Resync__c = '{!Opportunity.Velocify_Resync__c}';

// assign values to fields on opportunity object

if(opp.Velocify_Resync__c = "")
{opp.Velocify_Resync__c = 0;}
else{
opp.Velocify_Resync__c = parseInt(opp.Velocify_Resync__c) + 1;}


//save the change
var result = sforce.connection.update([opp]);

if(result[0].getBoolean("success")) { 
window.location.reload();;
} else { 
alert('Error : '+result);
}

Thanks again in advance.
Rohit B ☁Rohit B ☁
Try this :-
 
{!REQUIRESCRIPT("/soap/ajax/38.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/38.0/apex.js")} 

var opp = new sforce.SObject("Opportunity");
opp.Id = '{!Opportunity.Id}';
opp.Velocify_Resync__c = '{!Opportunity.Velocify_Resync__c}';

// assign values to fields on opportunity object
if(opp.Velocify_Resync__c == "" || opp.Velocify_Resync__c == null) {
opp.Velocify_Resync__c = 0;
} else {
opp.Velocify_Resync__c = parseInt(opp.Velocify_Resync__c) + 1;
}

//save the change
var result = sforce.connection.update([opp]);

if(result[0].getBoolean("success")) { 
window.location.reload();;
} else { 
alert('Error : '+result);
}

 
This was selected as the best answer
BrenzoBrenzo
Fantastic, that worked! Thanks so much for everyone's help with this.