[SalesForce] Need some help with a custom button or button override

Hopefully I can explain my question clearly here.

I want to change the behavior of this button that appears on our Opportunity page:
New Payment Button

The Opportunity object has a custom rollup field that counts the number of products associated with the opportunity. If the number of products associated with the opportunity = 0, then pressing the "New Payment" button should pop-up a message box that says something like "Eror: Cannot process a payment for an opportunity with no associated products".

The Payment object is managed, from the PaymentConnect package by Linvio. (Not sure if this impacts what I'm trying to do)

I can edit the button to override its functionality or create a custom button, but I'm having trouble. I don't want to change the overall functionality of the button. I just want to add that simple check before allowing it to continue.

Is it possible for me to write a simple override for this button that does the check I mentioned above before it punts it to the (managed) VisualForce page that the button now goes to?

I tried to create my own VisualForce page to duplicate the functionality of the one that the button currently directs to, but since it is in a managed namespace I cannot do it.

Any ideas or help would be much appreciated.

Best Answer

Try creating a new Javascript button, type should be "list button" (last option, without checkboxes). Call it "New Payment".

Implement your check there, something like

if({!Opportunity.Product_Rollup__c} == 0){
    alert('Uh oh');
} else {
    location.href = '(url to make new Payments here)';
}

Finally go to the Opportunity page layout (all of them if you have several?), edit related lists, untick the checkbox next to standard "New" button and add your custom one.

As this is managed and in future the package devs might decide they need new parameters passed to this VF page or something crazy like that I'd advise you to try not to make the url /apex/SomePage... and even not /a07/e?someParams (which format is OK for normal "new" pages) but rather try using URLFOR function.

P.S. Make no mistake - I'd recommend preventing saving by adding a Validation rule or some other "hard check" (trigger?) to the object anyway. It's a bit of security by obscurity, Salesforce power user might be able to hand-craft the link, access the Payments tab and hit "New" there or he'll use a mobile device with an app ;) This is valid visual client-side helper/reminder but such logic should be replicated server-side.


EDIT to include comparison of old & new URLs:

Original URL:

https://(instance).salesforce.com/setup/ui/recordtypeselect.jsp?
ent=01Id0000000jpap&
retURL=%2F006K00000069AnT&
save_new_url=%2Fa0B%2Fe%3FCF00Nd0000004bboJ%3DTest%2BPerson-Conference%2BRegistration-%252410-2013-01-18%26CF00Nd0000004bboJ_lkid%3D006K00000069AnT%26scontrolCaching%3D1%26retURL%3D%252F006K00000069AnT

Custom "new":

https://(instance).salesforce.com/setup/ui/recordtypeselect.jsp?
ent=01Id0000000jpap&
retURL=%2F006K00000069AnT&
save_new_url=%2Fa0B%2Fe%3FretURL%3D%252F006K00000069AnT

So the only difference is in the content of form prepopulation to be performed when user cicks "Save & New". Let's urldecode it:

/a0B/e?
CF00Nd0000004bboJ=Test+Person-Conference+Registration-%2410-2013-01-18&
CF00Nd0000004bboJ_lkid=006K00000069AnT&
scontrolCaching=1&
retURL=%2F006K00000069AnT

So - the package developers just made sure that Opportunity Id & Name will be prefilled in "Save & New" too. But this works for me already (I have custom object under Account, not Opportunity and the Account Name is prefilled on subsequent "save & new" without problems).

Long story short - experiment but I doubt you need it ;)

Related Topic