Help with Custom Button to increases a number field by 1 when clicked - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Doug HeindelDoug Heindel 

Help with Custom Button to increases a number field by 1 when clicked

Hi,

I have a Custom Button that our Sales Reps use to submit a lead to an Account Executive after once they schedule a meeting.

I would like to have a number field called Times_Submitted__c increase by 1 everytime they submit the lead to an Account Executive. This would then track the number of times a Sales Rep submits a particular lead to one of our Account Executives.

Thanks in advance for any insight.
Best Answer chosen by Moderator (salesforce.com) 
Jayant JoshiJayant Joshi
Doug,
 
You can try this:
 
{!REQUIRESCRIPT("/soap/ajax/21.0/connection.js")}
var meetingdate = '{!Lead.Meeting_Date__c}';
 
if(meetingdate == ''){
alert('Please populate Meeting Date');
}
else{
var accountexecutive = '{!Lead.Account_Executive__c}';
 
if(accountexecutive == ''){
alert('Please populate Account Executive');
}
else{
var update_Lead = new sforce.SObject("Lead")
update_Lead.Id = '{!Lead.Id}';
update_Lead.Status = "Meeting Scheduled";
update_Lead.Submitted__c = "True";
update_Lead.Rejected__c = "False";
update_Lead.Rejected_Reason__c = " ";
 
var count = {!Lead.Times_Submitted__c};

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
var newRecords = [];
var c = new sforce.SObject("Lead");
c.id ="{!Lead.Id}";
c.Submitted_On__c = new Date();
 
c.Times_Submitted__c= count +1;
newRecords.push(c);
result = sforce.connection.update(newRecords);
window.location.reload();
var result = sforce.connection.update([update_Lead]);
if (result[0].success=='false') {
alert(result[0].errors.message);
} else {
location.reload(true);
}
}
}
 
I have added two more statements marked in bold.
 

All Answers

Aaron DeRanAaron DeRan
It will require either Apex code or Javascript to achieve this.

Possible resource for Apex method:
http://boards.developerforce.com/t5/General-Development/Simple-Button-help/m-p/157806

Possible resource for Javascript method:
http://boards.developerforce.com/t5/General-Development/Update-a-field-value-with-a-new-custom-button/m-p/67282
Doug HeindelDoug Heindel
Thanks phiberoptik.

I am using the JavaScript below currently.

{!REQUIRESCRIPT("/soap/ajax/21.0/connection.js")}
var meetingdate = '{!Lead.Meeting_Date__c}';
 
if(meetingdate == ''){
alert('Please populate Meeting Date');
}
else{
var accountexecutive = '{!Lead.Account_Executive__c}';
 
if(accountexecutive == ''){
alert('Please populate Account Executive');
}
else{
var update_Lead = new sforce.SObject("Lead")
update_Lead.Id = '{!Lead.Id}';
update_Lead.Status = "Meeting Scheduled";
update_Lead.Submitted__c = "True";
update_Lead.Rejected__c = "False";
update_Lead.Rejected_Reason__c = " ";
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
var newRecords = [];
var c = new sforce.SObject("Lead");
c.id ="{!Lead.Id}";
c.Submitted_On__c = new Date();
newRecords.push(c);
result = sforce.connection.update(newRecords);
window.location.reload();
var result = sforce.connection.update([update_Lead]);
if (result[0].success=='false') {
alert(result[0].errors.message);
} else {
location.reload(true);
}
}
}


...Just not sure how to add that function to it.
Doug HeindelDoug Heindel
Thought adding this would do it...

update_Lead.Times_Submitted__c = "{!Lead.Times_Submitted__c} +1";

but it is failing :/
Jayant JoshiJayant Joshi
Doug,
 
You can try this:
 
{!REQUIRESCRIPT("/soap/ajax/21.0/connection.js")}
var meetingdate = '{!Lead.Meeting_Date__c}';
 
if(meetingdate == ''){
alert('Please populate Meeting Date');
}
else{
var accountexecutive = '{!Lead.Account_Executive__c}';
 
if(accountexecutive == ''){
alert('Please populate Account Executive');
}
else{
var update_Lead = new sforce.SObject("Lead")
update_Lead.Id = '{!Lead.Id}';
update_Lead.Status = "Meeting Scheduled";
update_Lead.Submitted__c = "True";
update_Lead.Rejected__c = "False";
update_Lead.Rejected_Reason__c = " ";
 
var count = {!Lead.Times_Submitted__c};

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
var newRecords = [];
var c = new sforce.SObject("Lead");
c.id ="{!Lead.Id}";
c.Submitted_On__c = new Date();
 
c.Times_Submitted__c= count +1;
newRecords.push(c);
result = sforce.connection.update(newRecords);
window.location.reload();
var result = sforce.connection.update([update_Lead]);
if (result[0].success=='false') {
alert(result[0].errors.message);
} else {
location.reload(true);
}
}
}
 
I have added two more statements marked in bold.
 
This was selected as the best answer
Jayant JoshiJayant Joshi
Doug,

Was that helpful? I tried this in my org and it works for me.
Doug HeindelDoug Heindel
Jayant,

Yes that is it :) You are not only wise but generous as well.

Thank you very much.
Jayant JoshiJayant Joshi
Glad to help Doug :)
Doug HeindelDoug Heindel
Jayant,

I have another question that I think you might be able to help me with...

I also want to display an error if a checkbox labeled "Meeting_Confirmed__c" is not check.

I added the bold below to my JavaScript but it failed. Can you see what I might be doing wrong? Is using 'False' corrent?



{!REQUIRESCRIPT("/soap/ajax/21.0/connection.js")}
var meetingconfirmed = '{!Lead.Meeting_Confirmed__c}';
 
if(meetingconfirmed == 'False'){
alert('Please wait for confirmation before submitting to an Account Executive');
}
else{
var meetingdate = '{!Lead.Meeting_Date__c}';
 
if(meetingdate == ''){
alert('Please populate Meeting Date');
}
else{
var accountexecutive = '{!Lead.Account_Executive__c}';
 
if(accountexecutive == ''){
alert('Please populate Account Executive');
}
else{
var update_Lead = new sforce.SObject("Lead")
update_Lead.Id = '{!Lead.Id}';
update_Lead.Status = "Meeting Scheduled";
update_Lead.Submitted__c = "True";
update_Lead.Rejected__c = "False";
update_Lead.Rejected_Reason__c = " ";
var count = {!Lead.Times_Submitted__c};
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
var newRecords = [];
var c = new sforce.SObject("Lead");
c.id ="{!Lead.Id}";
c.Submitted_On__c = new Date(); c.Times_Submitted__c= count +1;
newRecords.push(c);
result = sforce.connection.update(newRecords);
window.location.reload();
var result = sforce.connection.update([update_Lead]);
if (result[0].success=='false') {
alert(result[0].errors.message);
} else {
location.reload(true);
}
}
}
}
Jayant JoshiJayant Joshi
Doug,
 
Is Meeting_Confirmed__c a checkbox field?
 
try using this:
 
if(meetingconfirmed == false){
       alert('Please wait for confirmation before submitting to an Account Executive');
}
Doug HeindelDoug Heindel
Yes, it is a checkbox.

That worked :) So I just do not need the apostrophes for True/False statements. Good to know.

Thanks again. I owe you one.

Jayant JoshiJayant Joshi
Doug,
 
Yeah, you do not need the apostrophes for checkbox field values. You have to use true or false in your statement.