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
Steve HuseSteve Huse 

Can I disable the function of a custom button with an IF/conditional statement?

Hi All,

 

I have a custom 'create case' button in opportunities that creates a new case from an opportunity, passing some of the opportunity field data across into the case as it does so.

 

The button code looks like this:

 

/500/e?retURL=%2F500%2Fo

&cas4_lkid={!Account.Id}

&cas4={!Account.Name}

&CF00N30000009y6pH_lkid={!Opportunity.Id}

&CF00N30000009y6pH={!Opportunity.Name}

&cas3_lkid={! NULLVALUE (Case.ContactId, Contact.Id )}

&cas3={! NULLVALUE (Case.Contact, Contact.Name )}

&cas5={!Opportunity.Type}

&cas14={!Opportunity.Name}

&cas15={!Opportunity.Requirement__c}

&retURL=/{!Opportunity.Id}

&saveURL=/{!Opportunity.Id}

 

Am I able to disable the function of this custom button if the opportunity stage IS NOT 'closed won'?  Ideally I'd like an alert pop-up box to appear if the 'create case' button is clicked when the opportunity IS NOT 'closed won' but for the button to work as intended and create the new case if the opportunity IS 'closed won'.

 

Any help would be really appreciated.

 

Thanks to all.

 

Steve 

Best Answer chosen by Admin (Salesforce Developers) 
Rakesh Aggarwal.ax1406Rakesh Aggarwal.ax1406

This should work for you...

 

if ("{!Opportunity.StageName}" == 'Closed Won') {       location.href = "/500/e?retURL=%2F500%2Fo&cas4_lkid={!Account.Id}&cas4={!Account.Name}&CF00N30000009y6pH_lkid={!Opportunity.Id}&CF00N30000009y6pH={!Opportunity.Name}&cas3_lkid={! NULLVALUE (Case.ContactId, Contact.Id )}&cas3={! NULLVALUE (Case.Contact, Contact.Name )}&cas5={!Opportunity.Type}&cas14={!Opportunity.Name}retURL=/{!Opportunity.Id}&saveURL=/{!Opportunity.Id}";
} else {      alert('only closed won opps can be ...'); }

 

Thanks

Rakesh Aggarwal

www.rakeshaggarwal.com

All Answers

matermortsmatermorts
Interesting. I've never tried an if statement in a button. But I don't think you'll get everything you want via conventional declarative methods. Alternatively, you could have one opportunity record type (and page layout) for opportunities that are pre- closed/won. And then have a workflow rule change the record type, and thereby changing the page layout, when the opportunity changes to a closed/won stage. The closed/won page layout would be the only one with your custom "create case" button. So unless the opportunity is closed/won, users just won't see the button. Obviously, this won't produce the warning you want to display. And if you go this route, you should probably have another workflow to change the record type back to something else if the stage reverts to something other than closed/won (or have a validation rule limiting this change).

Additionally, you might want to have a validation rule on the case object to limit a user from creating a case from scratch (i.e. from the cases tab), related to an opportunity that has a non- closed/won stage. Maybe you already have this covered.

Cheers!
Let us know the outcome.
Noam.dganiNoam.dgani

I dont think there is a need for record types here.

 

Configure the custom button the following way:

 

1.behaviour = execute javascript

2.Content source = onclick Javascript

 

paste the following into the code editor:

if ("{!Opportunity.StageName}" == 'Closed Won')
{
      alert('Its fine');
}
else
{
     alert('only closed won opps can be something something');
}

 instead of the 'its fine' alert - do you logic.

etc.

 

if this solves the issue for you, please mark it as resolved

 

Steve HuseSteve Huse

Hi Noam,

 

Thanks for your help with this ... I found some similar JS code for this type of validation, but not being too conversant with JS wasn't sure how my original URL based code would appear as JS.

 

Are you able to help with this?  What would the JS code look like for the button to make the 'closed won' / not 'closed won' check, and then pass the required opportunity fields across to the new case?

 

If you can give me a couple of pointers, I should be able to work it out.

 

Many thanks again.

 

Steve

Rakesh Aggarwal.ax1406Rakesh Aggarwal.ax1406

This should work for you...

 

if ("{!Opportunity.StageName}" == 'Closed Won') {       location.href = "/500/e?retURL=%2F500%2Fo&cas4_lkid={!Account.Id}&cas4={!Account.Name}&CF00N30000009y6pH_lkid={!Opportunity.Id}&CF00N30000009y6pH={!Opportunity.Name}&cas3_lkid={! NULLVALUE (Case.ContactId, Contact.Id )}&cas3={! NULLVALUE (Case.Contact, Contact.Name )}&cas5={!Opportunity.Type}&cas14={!Opportunity.Name}retURL=/{!Opportunity.Id}&saveURL=/{!Opportunity.Id}";
} else {      alert('only closed won opps can be ...'); }

 

Thanks

Rakesh Aggarwal

www.rakeshaggarwal.com

This was selected as the best answer
Steve HuseSteve Huse

Thanks Rakesh ... with the exception of a missing '&' symbol, your solution was perfect.

 

Much appreciated!

 

Regards

 

Steve