Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Need a custom botton on an opportunity page to execute a task only after a set of validation checks have been preformed. Meaning the button will only function IF a list of fields and pick-lists have correct selections. Button should be unavailable for selection if these fields are not selected.

Further, botton should change the executable task if a field is populated versus non populated

How do I make this happen?

share|improve this question
    
Not exactly a duplicate but maybe you can salvage something from my answer to salesforce.stackexchange.com/questions/4627/… –  eyescream Feb 15 '13 at 17:24
add comment

3 Answers

You're going to want to create a button that uses the Javascript API to make the checks and then take action accordingly. You (STILL, after years and years of the Idea being a top voted customer Idea) can't make buttons appear/disappear on a single page layout, but the workaround is either to make the button alert the user if the action is invalid, or (much more involved) use the fields to switch the record type of the record and use 2 different page layouts, one with the button and one without. You can use a jQuery hack to hide buttons but it's not really a supported feature and I generally don't recommend it.

You'll want to look at the docs for the Javascript API. A pretty decent example of the type of thing you want to do can be found here.

share|improve this answer
    
Thanks, salesforce is missing the most basic functionality hu? I am fine with the button just showing an error message to the user if the criteria is not met. Thing is im not technical so need someone to build the code framework (I can add in the criteria selection). –  user2415 Feb 15 '13 at 5:12
    
You're not likely to get someone here to write your code for you, since this is a Q&A forum; but there are plenty of places around the web you can find cheap technical resources who are happy to bang out something like a custom button for pretty cheap. –  jkraybill Feb 15 '13 at 5:13
add comment

This may not be exactly what you're looking for, but it'll get you close and won't require a developer.

Instead of creating a button, you can create a formula field that displays a link to create the task, but only if certain criteria or met. For example

IF(
  ISBLANK(Your_Field__c)
, 'Please populate your field before taking action'
, HYPERLINK('/urlforyouraction?id=' + Id, 'Take the action')
)
share|improve this answer
add comment

Apex Approach: So here is my template for Apex via Custom Buttons as opposed to using JavaScript. It leverages Standard Controller methods as much as possible. Note that it is not possible to perform the update of the record in the same method due to security issues. Hope this helps!

enter image description here

In a valid case it shows this...

enter image description here

In an invalid case it shows this...

enter image description here

Standard Controller Class:

public with sharing class ValidationController 
{
    private ApexPages.StandardController standardController;

    public ValidationController(ApexPages.StandardController standardController)
    {
        // Configure fields to query and validate (alternative to SOQL)
        this.standardController = standardController;
        if(!Test.isRunningTest())
            this.standardController.addFields(
                new List<String> { Schema.Test__c.A_Number__c.getDescribe().getName() });   
    }

    public PageReference validate()
    {
        // Validate record
        Test__c test = (Test__c) standardController.getRecord();
        if(test.A_Number__c != 42)
            test.A_Number__c.addError('Not the answer to life the universe and everything!');

        // If no errors display confirmation message
        if(!ApexPages.hasMessages())
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'All is well, go ahead and click Update.'));

        return null;
    }

    public PageReference validateAndUpdate()
    {
        // Validate and redisplay if errors
        validate();
        if(ApexPages.hasMessages(ApexPages.Severity.Error))
            return null;

        // Update the record and call standard controller save
        Test__c test = (Test__c) standardController.getRecord();        
        test.Date__c = System.today();
        return standardController.save();
    }
}

You can view the test methods here.

Visualforce Page:

<apex:page standardController="Test__c" extensions="ValidationController" tabStyle="Test__c" action="{!validate}">
    <apex:sectionHeader title="Test" subtitle="{!Test__c.Name}"/>
    <apex:pageMessages />
    <apex:form >
        <apex:commandButton value="Update" action="{!validateAndUpdate}"/>
        <apex:commandButton value="Back" action="{!cancel}"/>
    </apex:form>
</apex:page>

You can then go to Custom Buttons on your object and add a Visualforce based custom button.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.