You need to sign in to do that
Don't have an account?

Urgent Help: javascript custom button issue
Hi All ,
This is the second time , I am posting this issue . But No one has been able to provide any concrete solution. Please help,
I have created a custom with javascript functionality and its working fine . I want when the same button is pressed twice or thrice it should give alert message " Invalid action". I tried using lot of things but its not working. I know it looks very easy but pls help me out in this issue. TBelow is the script:-
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var leadstatus = '{!Lead.Status}';
if (leadstatus != "Z-Program Approved")
{
alert('An Elite name cannot be assigned unless the Lead Status is equal Z-Program Approved');
}
else{
// Now calling the webservice to create a new Id
var callback = "" + sforce.apex.execute("CreateliteId","createlite", {LeadId:"{!Lead.Id}"});
// If create and update was ok - just reload Lead page to see the id of the elite partner
if(callback == 'OK' ){
alert('The Elite Id has been assigned');
window.location.reload();
}
else{
alert('Error: ' + callback);
}}
Try this: Add "?" before count string like below.
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var url = window.location.href;
alert(url);
var clickCounter = "0";
alert(clickCounter);
var count = getQueryString("count");
alert(count);
if(typeof count != 'undefined')
clickCounter = count;
alert(clickCounter);
var leadstatus = '{!Lead.Status}';
if(clickCounter == "0"){
alert("Executing code");
var leadstatus = '{!Lead.Status}';
if (leadstatus != "Z-Program Approved"){
alert('An Elite name cannot be assigned unless the Lead Status is equal Z-Program Approved');
window.location = url + "?&count=0";
}else{
// Now calling the webservice to create a new Id
var callback = "" + sforce.apex.execute("CreateliteId","createlite", {LeadId:"{!Lead.Id}"});
// If create and update was ok - just reload Lead page to see the id of the elite partner
if(callback == 'OK' ){
alert('The Elite Id has been assigned');
window.location = url + "?&count=1"; //Edit
}else{
alert('Error: ' + callback);
window.location = url + "?&count=0";
}
}
}else{
alert('Error Message');
}
function getQueryString(sVar) {
urlStr = window.location.search.substring(1);
sv = urlStr.split("&");
for (i=0;i< sv.length;i++) {
ft = sv [i].split("=");
if (ft[0] == sVar) {
return ft[1];
}
}
}
All Answers
Try adding a query string to the url when reloading the page. Then, look for that string. If the string is found and value is 1 then dont run the code.
Ex:
var url = window.location.href;
var clickCounter = getQueryString("count");
var leadstatus = '{!Lead.Status}';
if(clickCounter == null){
if (leadstatus != "Z-Program Approved")
{
alert('An Elite name cannot be assigned unless the Lead Status is equal Z-Program Approved');
}else{
// Now calling the webservice to create a new Id
var callback = "" + sforce.apex.execute("CreateliteId","createlite", {LeadId:"{!Lead.Id}"});
// If create and update was ok - just reload Lead page to see the id of the elite partner
if(callback == 'OK' ){
alert('The Elite Id has been assigned');
window.location.href = url + "&count=1";
}else{
alert('Error: ' + callback);
}}
}
function getQueryString(sVar) {
urlStr = window.location.search.substring(1);
sv = urlStr.split("&");
for (i=0;i< sv.length;i++) {
ft = sv [i].split("=");
if (ft[0] == sVar) {
return ft[1];
}
}
}
Hope this helps!
Hi ,
thanks for the reply . But it is giving error in alert box :
A problem with onclick javascript for this button or link was encountered:
cannot call method 'execute' of undefined
The only thing I can see is that var clickCounter could be undefined if count is not in the url. Try changing the lines of code in green.
var url = window.location.href;
var clickCounter = "" + getQueryString("count");
var leadstatus = '{!Lead.Status}';
if(clickCounter == ""){
if (leadstatus != "Z-Program Approved")
{
alert('An Elite name cannot be assigned unless the Lead Status is equal Z-Program Approved');
}else{
// Now calling the webservice to create a new Id
var callback = "" + sforce.apex.execute("CreateliteId","createlite", {LeadId:"{!Lead.Id}"});
// If create and update was ok - just reload Lead page to see the id of the elite partner
if(callback == 'OK' ){
alert('The Elite Id has been assigned');
window.location.href = url + "&count=1";
}else{
alert('Error: ' + callback);
}}
}else{
alert('Error Message')
}
function getQueryString(sVar) {
urlStr = window.location.search.substring(1);
sv = urlStr.split("&");
for (i=0;i< sv.length;i++) {
ft = sv [i].split("=");
if (ft[0] == sVar) {
return ft[1];
}
}
}
Hope this helps!
Hey ,
at every click it is giving alert dialogue box with the message "alert message". That is , it is only executing ELSE condition not IF condition.
Regards
Add the following code to see the value of clickCounter before the if:
var url = window.location.href;
alert(url); //New line
var clickCounter = "0" + getQueryString("count");//Edit
alert(clickCounter); //New line
var leadstatus = '{!Lead.Status}';
if(clickCounter == "0"){//Edit
if (leadstatus != "Z-Program Approved")
{
alert('An Elite name cannot be assigned unless the Lead Status is equal Z-Program Approved');
}else{
// Now calling the webservice to create a new Id
var callback = "" + sforce.apex.execute("CreateliteId","createlite", {LeadId:"{!Lead.Id}"});
// If create and update was ok - just reload Lead page to see the id of the elite partner
if(callback == 'OK' ){
alert('The Elite Id has been assigned');
window.location = url + "&count=1";
}else{
alert('Error: ' + callback);
}}
}else{
alert('Error Message')
}
function getQueryString(sVar) {
urlStr = window.location.search.substring(1);
sv = urlStr.split("&");
for (i=0;i< sv.length;i++) {
ft = sv [i].split("=");
if (ft[0] == sVar) {
return ft[1];
}
}
}
Hi ,
It is baack to back giving three alert boxes :-
1.)some salesforce URL
2.) 0undefined
3.) alert message .
Again not executing IF condition and only executing alert messages
Ok Now try this: The pop-ups will let us know what values we have. Make the change, click the button once, then once again... Let me know the results
var url = window.location.href;
alert(url);//pop-up for debugin
var clickCounter = "0";
alert(clickCounter); //pop-up for debugin
var count = getQueryString("count");
alert(count);//pop-up for debugin
if(typeof count != 'undefined')
clickCounter = count;
alert(clickCounter); //pop-up for debugin
var leadstatus = '{!Lead.Status}';
if(clickCounter == "0"){
if (leadstatus != "Z-Program Approved")
{
alert('An Elite name cannot be assigned unless the Lead Status is equal Z-Program Approved');
}else{
// Now calling the webservice to create a new Id
var callback = "" + sforce.apex.execute("CreateliteId","createlite", {LeadId:"{!Lead.Id}"});
// If create and update was ok - just reload Lead page to see the id of the elite partner
if(callback == 'OK' ){
alert('The Elite Id has been assigned');
window.location = url + "&count=1"; //Edit
}else{
alert('Error: ' + callback);
}}
}else{
alert('Error Message')
}
function getQueryString(sVar) {
urlStr = window.location.search.substring(1);
sv = urlStr.split("&");
for (i=0;i< sv.length;i++) {
ft = sv [i].split("=");
if (ft[0] == sVar) {
return ft[1];
}
}
}
Hi ,
It is baack to back giving five alert boxes :-
1.)some salesforce URL
2.) 0
3.)undefined
4.) 0
5.)A problem with onclick javascript for this button or link was encountered:
cannot call method 'execute' of undefined.
Remember to add these to the top of the code.
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
hey ,
In this order it is giving alert boxes :-
1.) Some salesforce URL
2.)0
3.) UNDEFINED
4.)0
5.) The alert ID has been assigned.
i.e this time it is executing IF condition but when I press the button twice or thrice , it is again assigning ELITE ID . it is not giving error " Invalid action".
I made some small modifications to the code and test it and seems to be working fine.
Also, notice that this will only work if you don't leave the current page and click the button again. If you leave and come the button will execute the code again.
Try this code and let me know the results.
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var url = window.location.href;
alert(url);
var clickCounter = "0";
alert(clickCounter);
var count = getQueryString("count");
alert(count);
if(typeof count != 'undefined')
clickCounter = count;
alert(clickCounter);
var leadstatus = '{!Lead.Status}';
if(clickCounter == "0"){
alert("Executing code");
var leadstatus = '{!Lead.Status}';
if (leadstatus != "Z-Program Approved"){
alert('An Elite name cannot be assigned unless the Lead Status is equal Z-Program Approved');
window.location = url + "&count=0";
}else{
// Now calling the webservice to create a new Id
var callback = "" + sforce.apex.execute("CreateliteId","createlite", {LeadId:"{!Lead.Id}"});
// If create and update was ok - just reload Lead page to see the id of the elite partner
if(callback == 'OK' ){
alert('The Elite Id has been assigned');
window.location = url + "&count=1"; //Edit
}else{
alert('Error: ' + callback);
window.location = url + "&count=0";
}
}
}else{
alert('Error Message');
}
function getQueryString(sVar) {
urlStr = window.location.search.substring(1);
sv = urlStr.split("&");
for (i=0;i< sv.length;i++) {
ft = sv [i].split("=");
if (ft[0] == sVar) {
return ft[1];
}
}
}
Hey ,
Its again the same. Again sam alert boxes
Did you click the button twice, trice? Did the values in the alert boxes where the same both times?
The alerts are supposed to be.
First Time:
url
0
undefined
0
Executing Code
The Elite Id has been assigned
Secord Time, Third Time, ...
url
0
1
1
Error Message
Are you getting this result, or you are getting the "Executing Code" alert everytime you click the button?
This is what I get every time:-
First Time:
url
0
undefined
0
Executing Code
The Elite Id has been assigned
After this , it automatically takes me to new page where it is mentioned " URL no longer exists"..
This process repeats on every click
Try this: Add "?" before count string like below.
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var url = window.location.href;
alert(url);
var clickCounter = "0";
alert(clickCounter);
var count = getQueryString("count");
alert(count);
if(typeof count != 'undefined')
clickCounter = count;
alert(clickCounter);
var leadstatus = '{!Lead.Status}';
if(clickCounter == "0"){
alert("Executing code");
var leadstatus = '{!Lead.Status}';
if (leadstatus != "Z-Program Approved"){
alert('An Elite name cannot be assigned unless the Lead Status is equal Z-Program Approved');
window.location = url + "?&count=0";
}else{
// Now calling the webservice to create a new Id
var callback = "" + sforce.apex.execute("CreateliteId","createlite", {LeadId:"{!Lead.Id}"});
// If create and update was ok - just reload Lead page to see the id of the elite partner
if(callback == 'OK' ){
alert('The Elite Id has been assigned');
window.location = url + "?&count=1"; //Edit
}else{
alert('Error: ' + callback);
window.location = url + "?&count=0";
}
}
}else{
alert('Error Message');
}
function getQueryString(sVar) {
urlStr = window.location.search.substring(1);
sv = urlStr.split("&");
for (i=0;i< sv.length;i++) {
ft = sv [i].split("=");
if (ft[0] == sVar) {
return ft[1];
}
}
}
Hey ,
It worked..Thanks a lot for your time . You saved me . I sincerely appreciate your efforts.You ROCK..!!!
Regards