+ Start a Discussion
Esmeralda Canoog 3Esmeralda Canoog 3 

Add onclick javascript "custom button" in visualforce page.

I have a custom button created in the object called CM and this button execute javascript, content source is onclick javascript: Name of the button is Print and "Print_CM" here is a VF page. See below

CM_Status__c ,'Cancelled')} && !{!ISPICKVAL( Credit_Memo__c.SFCM_Status__c ,'Reversed')} && !{!ISPICKVAL( Credit_Memo__c.SFCM_Status__c ,'Disapproved')}) 

window.location.href = "https://" + window.location.host + "/apex/Print_CM?id=" + "{!Credit_Memo__c.Id}";
} else {
alert("Error: This Credit Memo cannot be printed.") 
}

Now I'm trying to add this button in my existing VF page and let's call it "CM View".

I'm trying to add it as  <apex:commandbutton but not sure how to do it. I know that I need to add something from the existing controller to call it in the CM View VF page, but I'm not sure how to do it well.

Any help will be much appreciated.

Thanks in advance guys :)
Best Answer chosen by Esmeralda Canoog 3
Prakash NawalePrakash Nawale
Hi,

Add below code in your VF page and replace Credit_Memo__c api name with your variable created in controller class.
<script>

function executelogic(){
if({!Credit_Memo__c.CM_Status__c =='Cancelled' && {!Credit_Memo__c.SFCM_Status__c!=Reversed'} && {!Credit_Memo__c.SFCM_Status__c!=Disapproved'}) 

window.location.href = "https://" + window.location.host + "/apex/Print_CM?id=" + "{!Credit_Memo__c.Id}";
return false;
} else {
alert("Error: This Credit Memo cannot be printed.") 
return false;
}

}
</script>

In case if you have any concerns, I am avilable on https://www.uberconference.com/nawaleprakash so we can do screen share and resolve your issue.

Thanks

All Answers

Prakash NawalePrakash Nawale
hi You can Add on click event of button.

<apex:commandbutton onclick="return executelogic()" .../>
<script>

function executelogic(){
CM_Status__c ,'Cancelled')} && !{!ISPICKVAL( Credit_Memo__c.SFCM_Status__c ,'Reversed')} && !{!ISPICKVAL( Credit_Memo__c.SFCM_Status__c ,'Disapproved')}) 

window.location.href = "https://" + window.location.host + "/apex/Print_CM?id=" + "{!Credit_Memo__c.Id}";
return false;
} else {
alert("Error: This Credit Memo cannot be printed.") 
return false;
}

}


</script>
Esmeralda Canoog 3Esmeralda Canoog 3
Hi Prakash,

Tried it but it says 
Incorrect parameter type for function 'ISPICKVAL()'. Expected Picklist, received Text. Thanks
 
Prakash NawalePrakash Nawale
Hi Esmeralda,

The ISPICKVAL() is only used for picklists data type field to compare value not for text data type field value. I am not sure which type of above fields, can you please verify field data types and then apply is ISPICKVAL() for only picklists.

 
Esmeralda Canoog 3Esmeralda Canoog 3
Credit_Memo__c.SFCM_Status__c is a picklist. 
Prakash NawalePrakash Nawale
okay, then remove ispickvalue() for CM_Status__c and compare like CM_Status__c=='Cancelled"........
Esmeralda Canoog 3Esmeralda Canoog 3
CM Status is a picklist as well. it should be Credit_Memo__c.SFCM_Status__c . Thanks
Esmeralda Canoog 3Esmeralda Canoog 3
Let me post the onlcik javascript again. See below:
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 

if(!{!ISPICKVAL( Credit_Memo__c.SFCM_Status__c ,'Error')} && !{!ISPICKVAL( Credit_Memo__c.SFCM_Status__c ,'Cancelled')} && !{!ISPICKVAL( Credit_Memo__c.SFCM_Status__c ,'Reversed')} && !{!ISPICKVAL( Credit_Memo__c.SFCM_Status__c ,'Disapproved')}) 

window.location.href = "https://" + window.location.host + "/apex/Print_CM?id=" + "{!Credit_Memo__c.Id}"; 
} else { 
alert("Error: This Credit Memo cannot be printed.") 
}
Prakash NawalePrakash Nawale
okay, thanks, please let me know in case if you need any other help and mark this as best answer.

Thank you.
Esmeralda Canoog 3Esmeralda Canoog 3
Its not yet resolved. I'm still getting the same error. :(
 
Prakash NawalePrakash Nawale
Hi,

Add below code in your VF page and replace Credit_Memo__c api name with your variable created in controller class.
<script>

function executelogic(){
if({!Credit_Memo__c.CM_Status__c =='Cancelled' && {!Credit_Memo__c.SFCM_Status__c!=Reversed'} && {!Credit_Memo__c.SFCM_Status__c!=Disapproved'}) 

window.location.href = "https://" + window.location.host + "/apex/Print_CM?id=" + "{!Credit_Memo__c.Id}";
return false;
} else {
alert("Error: This Credit Memo cannot be printed.") 
return false;
}

}
</script>

In case if you have any concerns, I am avilable on https://www.uberconference.com/nawaleprakash so we can do screen share and resolve your issue.

Thanks
This was selected as the best answer
Esmeralda Canoog 3Esmeralda Canoog 3
OMG. It's working. Thanks you so much but here's the thing, I just tried it for one value for the meantime. See below:

<script>

function executelogic(){
if({!Credit_Memo__c.SFCM_Status__c =='Cancelled'})

window.location.href = "https://" + window.location.host + "/apex/Print_CM?id=" + "{!Credit_Memo__c.Id}";
return false;
} else {
alert("Error: This Credit Memo cannot be printed.") 
return false;
}

}
</script>

This is working,but there's a problem when I tried to make it work with the "reversed status" or when I include other values. Do you know how to add multiple values? tried 2 value for the mean time below but its not working tried what you have posted a while ago but its not working. It only works with one value: Below is not working as well (2 values)

<script>

function executelogic(){
if({!Credit_Memo__c.SFCM_Status__c =='Cancelled'} && {!Credit_Memo__c.SFCM_Status__c == 'Reversed'})

window.location.href = "https://" + window.location.host + "/apex/Print_CM?id=" + "{!Credit_Memo__c.Id}";
return false;
} else {
alert("Error: This Credit Memo cannot be printed.") 
return false;
}

}
</script>
 
Prakash NawalePrakash Nawale
Hi ,

You need to use or "||" operator in if conidtion to work for both value.
if({!Credit_Memo__c.SFCM_Status__c =='Cancelled'} ||  {!Credit_Memo__c.SFCM_Status__c == 'Reversed'})
Esmeralda Canoog 3Esmeralda Canoog 3
It's working now. Thanks for your help. I really appreciate it. :)