0

I need to make amount and mode field mandatory if corresponding checkboxes are checked.

<Body class="slds-scope" style="width: min-content">
    <apex:form id="disp">
        <apex:pageBlock>
            <div class="slds-card__body">
                <apex:outputPanel id="details">
                    <apex:dataTable style="border-collapse:collapse" value="{!wrapVisitList }" var="viswrap" id="theTable" rowClasses="rowWithBorder" border="2" styleClass="slds-table">

                        <apex:facet name="caption"><b>Create Conveyance</b></apex:facet>
                        <apex:facet name="header">Please fill details</apex:facet>
                        <apex:column>
                            <apex:facet name="header">
                                <apex:facet name="header">Select</apex:facet>
                            </apex:facet>
                            <apex:inputCheckbox value="{!viswrap.selected}" id="inputId" />
                        </apex:column>
                        <apex:column>
                            <apex:facet name="header">Account</apex:facet>
                            <apex:outputText value="{!VisWrap.ev.Account.name}" />
                        </apex:column>
                        <apex:column>
                            <apex:facet name="header">Start Date</apex:facet>
                            <apex:outputText value="{!VisWrap.ev.StartDateTime}" />
                        </apex:column>
                        <apex:column>
                            <apex:facet name="header">End Date</apex:facet>
                            <apex:outputText value="{!VisWrap.ev.EndDateTime}" />
                        </apex:column>
                        <apex:column>
                            <apex:facet name="header">Mode of Transport *</apex:facet>
                            <apex:inputfield value="{!VisWrap.ev.Modeoftransport__c}" />
                        </apex:column>
                        <apex:column>
                            <apex:facet name="header">Amount *</apex:facet>
                            <apex:inputfield value="{!VisWrap.ev.Amount__c}" id="AmountId" />
                            <div id="err51"></div>
                        </apex:column>
                        <apex:column>
                            <apex:facet name="header">Upload *</apex:facet>
                            <apex:inputFile value="{!ExpenseBody}" fileName="{!ExpenseName}" />
                        </apex:column>
                    </apex:dataTable>
                </apex:outputPanel>
            </div>
            <apex:commandButton value="Save" styleClass="slds-button slds-button_brand" action="{!processSelected}" onclick="if(true === performValidation()){return true;}else{return false;}" />
        </apex:pageBlock>
    </apex:form>
</body>

</HTML>

Controller :

public class conveyance {
public list < event > displayvisits {
    get;
    set;
}
public Blob Expensebody {
    get;
    set;
}
public String ExpenseName {
    get;
    set;
}
public Attachment a {
    get;
    set;
}

String id = ApexPages.currentPage().getParameters().get('id');

public List < wrapVisit > wrapVisitList {
    get;
    set;
}
public List < Event > selectedVisits {
    get;
    set;
}

Conveyance__c convobj = new Conveyance__c();
List < DateTime > Startdlist = new List < DateTime > ();
List < DateTime > Enddlist = new List < DateTime > ();
Decimal exp = 0;



public conveyance() {
    ExpenseBody = Blob.valueOf(' ');

    if (wrapVisitList == null) {
        wrapVisitList = new List < wrapVisit > ();
        for (Event a: [SELECT Id, Modeoftransport__c, amount__C, visitplan__c, Account.name, startdatetime, enddatetime from event where visitplan__c =: id]) {

            wrapVisitList.add(new wrapVisit(a));

        }
    }
}

public PageReference processSelected() {
    List < Event > selectedVisits = new List < Event > ();

    for (wrapVisit wrapVisitObj: wrapVisitList) {
        if (wrapVisitObj.selected == true) {
            selectedvisits.add(wrapVisitObj.ev);
            startdlist.add(wrapVisitObj.ev.startdatetime);
            Enddlist.add(wrapVisitObj.ev.Enddatetime);
            exp = exp + wrapVisitObj.ev.Amount__c;

            try {
                update selectedvisits;
            } catch (System.DMLException e) {
                ApexPages.addMessages(e);
            }

            IF(ExpenseBody != Blob.valueOf(' ')) {
                String fname = 'Expense.' + ExpenseName.substringAfterLast('.');
                insertFile(fname, this.ExpenseBody);
            }



        }
    }


    Startdlist.sort();
    Enddlist.sort();

    convobj.From_Date__c = Startdlist[0];
    convobj.To_Date__c = Enddlist[Enddlist.size() - 1];
    convobj.Expense__c = exp;
    convobj.Visit_plan__c = id;


    try {
        insert convobj;
    } catch (System.DMLException e) {
        ApexPages.addMessages(e);
    }



    PageReference p = new PageReference('/' + convobj.id);
    p.setRedirect(true);
    return p;
}

public class wrapVisit {
    public event ev {
        get;
        set;
    }
    public Boolean selected {
        get;
        set;
    }

    public wrapVisit(Event a) {
        ev = a;
        selected = false;
    }
}

public void insertFile(String fileName, Blob body) {
    ContentVersion conVer = new ContentVersion();
    conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
    conVer.PathOnClient = fileName; // The files name, extension is very important here which will help the file in preview.
    conVer.Title = fileName; // Display name of the files
    conVer.VersionData = body; // converting your binary string to Blob
    insert conVer;
    Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =: conVer.Id].ContentDocumentId;

    //Create ContentDocumentLink
    ContentDocumentLink cDe = new ContentDocumentLink();
    cDe.ContentDocumentId = conDoc;
    cDe.LinkedEntityId = id; // you can use objectId,GroupId etc
    cDe.ShareType = 'I'; // Inferred permission, checkout description of ContentDocumentLink object for more details
    cDe.Visibility = 'AllUsers';
    insert cDe;
}

}

0

This can be achieved using a simple "required" attribute addition to the Mode and Amount inputs:

                    <apex:column>
                        <apex:facet name="header">Mode of Transport *</apex:facet>
                        <apex:inputfield value="{!VisWrap.ev.Modeoftransport__c}" required={!viswarp.selected} />
                    </apex:column>
                    <apex:column>
                        <apex:facet name="header">Amount *</apex:facet>
                        <apex:inputfield value="{!VisWrap.ev.Amount__c}" id="AmountId" required={!viswarp.selected} />
                        <div id="err51"></div>
                    </apex:column>
1
  • Thanks for the comment EranV. But this did not work for me. I think javascript is the only way in which I am not so good. – Kathy M May 6 '20 at 16:45
0

Javascript validation called on submit button

        function performValidation() {

            var table = document.getElementById('j_id0:disp:j_id4:theTable:tb');
            var temp = false;
            var funcAmount = ValidateAmount();
            var funcMode = ValidateMode();
            var funcfile = Validatefile();
            var funcselect = Validateselection();

            if (funcAmount && funcMode && funcfile && funcselect  ) {
                temp = true;


            }
            if (temp == true) {

                return true;
            }
            return false;


        }
        function ValidateAmount() {
            var Inputcheckbox = true;
            var myTab = document.getElementById('j_id0:disp:j_id4:theTable:tb');

            for (var i = 0; i < myTab.rows.length; i++) {

                if (document.getElementById('j_id0:disp:j_id4:theTable:tb').rows[i].cells[0].querySelector('[type="checkbox"]').checked) {

                    if (!document.getElementById('j_id0:disp:j_id4:theTable:tb').rows[i].cells[5].querySelector('[type="text"]').value ) {
                        Inputcheckbox = true;
                        break;
                    } else {
                        Inputcheckbox = false;
                    }

                } else {

                    Inputcheckbox = false;

                }

            }
            if(Inputcheckbox === true){
                document.getElementById("err51").innerHTML = ' ';
                document.getElementById("err51").innerHTML = 'Please Enter Amount of selected visit(s)';
                document.getElementById("err51").style.color = 'red';
                return false;
            }
            else{
                return true
            }

        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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