2

The ask is: If the user selects 'Declined', in the "Envelope Status" Select list, the inputField "Declined Reason" should be mandatory. I have basically tried everything that is available in stackexchange; but couldn't figure out whats wrong in my code. Also, putting the inputField in an outputPanel, takes away its label. How to overcome that? Here is my Page:-

<apex:page standardController="Opportunity" extensions="NewDocuSignEnvelopeExtension" tabStyle="dsfs__DocuSign_Status__c">
<apex:form >
    <apex:sectionHeader subtitle="New DocuSign Envelope" title="Create Envelope"/>
    <apex:pageBlock title="New DocuSign Envelope" mode="edit">
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!saveDocuSignRecord}"/>
            <apex:commandButton value="Cancel" action="{!cancel}" immediate="true"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Information" collapsible="false" columns="2">
            <apex:outputText label="Sender" value="{!$User.FirstName} {!$User.LastName}"/>
            <apex:selectList id="envStatus" label="Envelope Status" value="{!envelopeStatus}" multiselect="false" size="1">
                <apex:selectOption itemValue="Sent" itemLabel="Sent"/>
                <apex:selectOption itemValue="Completed" itemLabel="Completed"/>
                <apex:selectOption itemValue="Declined" itemLabel="Declined"/>
                <apex:actionSupport event="onchange" action="{!checkRequired}" rerender="declinedReasonId"/>
            </apex:selectList>
            <apex:outputText label="Sender Email" value="{!$User.Email}"/>
            <apex:inputField value="{!status.Status_Date__c}" required="true"/>
            <apex:outputText label="Subject" value="Important eSignature Request from Bright House Networks"/>
            <apex:outputPanel id="declinedReasonId">
                <apex:inputField label="Declined Reason" value="{!status.dsfs__Declined_Reason__c}" required="{!isRequired}"/>
            </apex:outputPanel>
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Related Objects" collapsible="false" columns="2">
            <apex:outputText label="Account" value="{!Opportunity.Account.Name}"/>
            <apex:outputText label="Opportunity" value="{!Opportunity.Name}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

And My Extension code:-

public class NewDocuSignEnvelopeExtension {

public NewDocuSignEnvelopeExtension(ApexPages.StandardController controller) {

}

public NewDocuSignEnvelopeExtension() {

}

public dsfs__DocuSign_Status__c status{
    get{
        if(status == null)
            status = new dsfs__DocuSign_Status__c();
        return status;
    }
    set;}
public String envelopeStatus {get;set;}
public string declined {get;set;}
public boolean isRequired {get;set;}{isRequired=false;}

public PageReference saveDocuSignRecord(){
    String opptyId = ApexPages.currentPage().getParameters().get('Id');
    Opportunity oppty = new Opportunity();
    dsfs__DocuSign_Status__c ds = new dsfs__DocuSign_Status__c();
    Id recordTypeId = Schema.SObjectType.dsfs__DocuSign_Status__c.getRecordTypeInfosByName().get('Manual').getRecordTypeId();
    oppty=[select id, name, AccountId from Opportunity where id =: opptyId];
    ds.dsfs__Sender__c = UserInfo.getName();
    ds.dsfs__Sender_Email__c = UserInfo.getUserEmail();
    ds.dsfs__Subject__c = 'Important eSignature Request from Bright House Networks';
    ds.Lightning_Abhi__Envelope_status__c = envelopeStatus;
    ds.Lightning_Abhi__Status_Date__c = status.Status_Date__c;
    ds.dsfs__Declined_Reason__c = status.dsfs__Declined_Reason__c;
    ds.dsfs__Company__c = oppty.AccountId;
    ds.dsfs__Opportunity__c = oppty.Id;
    ds.RecordTypeId = recordTypeId;
    insert ds;
    PageReference dsPage = new PageReference('/' + ds.id);
    dsPage.setRedirect(true);
    return dsPage;
}

Public List<SelectOption> getItems(){
    List<SelectOption> options = new List<SelectOption>();
    options.add(new SelectOption('Sent','Sent'));
    options.add(new SelectOption('Completed','Completed'));
    options.add(new SelectOption('Declined','Declined'));
    return options;
}

public void checkRequired(){
    if(envelopeStatus=='Declined'){
        isRequired = true;
    }
}
}
2
  • Remove isRequired=false; from the declaration of the variable and move it to constructor. Commented Aug 8, 2016 at 17:37
  • @VigneshwaranG Didn't help. Same result. The onchange event isn't firing the action. Commented Aug 9, 2016 at 14:25

2 Answers 2

2

Instead of calling into saveDocuSignRecord you would build a new method that would control any of the validation that would need to be done and then it would throw an error..

VF PAGE 
<apex:pageMessages id="feedback" escape="false" />

Controller
public void prepareDataForSave()
{
    Boolean result = true;
    String missingDataError = '';

    if(whatever you need to check)
    {
            missingDataError += '<br />  ' + 'Whatever you want your error to be';
            result = false;
    }

    if(result)
    {
        saveDocuSignRecord(result);
    }
    else
    {
        displayFeedback(ApexPages.Severity.Error, String.valueOf(missingDataError));
        missingDataError = '';
    } 
}

private void displayFeedback(ApexPages.Severity msgType, String message) 
{
    ApexPages.Message msg = new ApexPages.Message(msgType, message);
    ApexPages.addMessage(msg);
}
3
  • Hi @EricSSH. Thanks for the reply. It partially solved my problem. Now when the conditions are not met, the error is throwing properly, but when everything is fine and I'm hitting save, I'm landing to the same page (because I'm not returning the new pageReference). Tips to overcome this part. Commented Aug 8, 2016 at 20:48
  • @AbhijeetMitra Open another ticket and we could help with the whole page redirection part
    – EricSSH
    Commented Aug 8, 2016 at 20:59
  • I haven't created another ticket as I have modified your code and now the page redirection is working just fine. Commented Aug 12, 2016 at 15:41
0

@EricSSH I haven't created another ticket as I have modified your code and now the page redirection is working just fine. The VF page snippet by you, is correct. I just had to modify the Apex controller code:-

public PageReference prepareDataForSave(){
Boolean result = true;
String missingDataError = '';

if(whatever you need to check)
{
    missingDataError += '<br />  ' + 'Whatever you want your error to be';
    result = false;
}

if(result)
{
    PageReference dsPage=saveDocuSignRecord();
    dsPage.setRedirect(true);
    return dsPage;
}
else
{
    displayFeedback(ApexPages.Severity.Error, String.valueOf(missingDataError));
    missingDataError = '';
    PageReference dsPage = new PageReference('/apex/NewDocuSignEnvelope?scontrolCaching=1&id='+opptyId);
    dsPage.setRedirect(false);
    return dsPage;
}
}

private void displayFeedback(ApexPages.Severity msgType, String message){
ApexPages.Message msg = new ApexPages.Message(msgType, message);
ApexPages.addMessage(msg);
}
1
  • Awesome glad it worked out
    – EricSSH
    Commented Aug 12, 2016 at 16:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .