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
Jacob Elliott 8Jacob Elliott 8 

Visualforce javascript function shows toast message pending field validation

Hi all! Any help will be greatly appreciated! 

I have a VF page that is a list of records(Medical Bills) and the user can use inline edit to say whether or not we should pay(Disburse__c) the bill and give a reason why(Note/ Disbursement_Note__c). 

If the user decides to select no to pay(Disburse__c) then the Note field becomes required and if they try to save without leaving a note I would like to populate a Toast Message using  sforce.one.showToast().

The javascript function will need to iterate through a list of the medical bills, see if there is a Pay/Disburse value of no and a Note with the Value of Null and if so, display the toast message. 

Here is the VF Page:
 
<apex:page standardController="litify_pm__Matter__c" extensions="GetMedicals" lightningStylesheets="true">  

    <html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

    <head>
        
<script>
     function showToast() {
        
        var myList = new Array();
        myList = ‘{!lstMedicals}’;

        boolean showError = false;

        for(Medical_Bill__c med : myList){
            if(med.Disburse__c == 'No' || med.Disbursement_Note__c == null){
                showError = true;
            }
        }

        if(showError){
            
            sforce.one.showToast({
                "title": "Error!",
                "message": "Error.",
                "type": "success"
            });
        }
        else {
            sforce.one.showToast({
                "title": "Success!",
                "message": "Success.",
                "type": "success"
            });
        }
    } 
</script>

    </head>

    <apex:pageMessages id="showmsg"></apex:pageMessages>
     <apex:form >  
         
    <apex:pageBlock mode="inlineEdit">  
        <div id="header" class="headerborder">
            <p id="title">DT Worksheet</p>
        </div>
        <apex:pageBlockButtons location="bottom">  
               <apex:commandButton id="saveButton" value="Save" rerender="showmsg" onclick="showToast();"/>  
               <apex:commandButton id="cancelButton" value="Cancel" rerender="showmsg"/>  
        </apex:pageBlockButtons> 
    
        

        <div class="tableborder">
    <apex:pageBlockTable value="{!lstMedicals}" var="med">
        
        <apex:column headerValue="Medical Bill Number" headerClass="headerStyle">
                <apex:actionRegion >
                      <apex:outputLink value="/{!med.Id}" styleClass="link">
                            {!med.Name}
                      </apex:outputLink>
                </apex:actionRegion>  
        </apex:column>

            <apex:column headerValue="Provider" headerClass="headerStyle">
                <apex:actionRegion >
                      <apex:outputField value="{!med.Provider_Name__c}">
                      </apex:outputField>
                </apex:actionRegion>  
        </apex:column>

        <apex:column headerValue="Pay?" headerClass="headerStyle">
                <apex:actionRegion >
                      <apex:outputField value="{!med.Disburse__c}">
                              <apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" id="pay"/>
                      </apex:outputField>
                </apex:actionRegion>  
        </apex:column>

        <apex:column headerValue="Notes" headerClass="headerStyle">
                <apex:actionRegion >
                      <apex:outputField value="{!med.Disbursement_Note__c}">
                              <apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" id="note"/>
                      </apex:outputField>
                </apex:actionRegion> 
        </apex:column>
    </apex:pageBlockTable>
</div>
 </apex:pageBlock>  
</apex:form>  

</apex:page>

Here is the extension, if relevant:
public with sharing class GetMedicals {
    public List<Medical_Bill__c> lstMedicals{get;set;}

    public GetMedicals() {

    }

     public GetMedicals(ApexPages.StandardController controller)
     {
               lstMedicals = [Select Id, Name, Provider_Name__c, Disburse__c, Disbursement_Note__c From Medical_Bill__c LIMIT 10 ];
    }

    public pagereference saveme()
    {
    try
    {
        update lstMedicals;
    }   
    catch(Exception e)
    {
        System.debug('Exception occurred '+String.valueOf(e));
    }
    return NULL;
    }       
}

​​​​​​​
Best Answer chosen by Jacob Elliott 8
Jacob Elliott 8Jacob Elliott 8
I ended up using a validation rule on the object and using apex to display that same error when the dml update failed.

Page:
<apex:page>
<apex:form id="form">  
    
    <apex:pageBlock mode="inlineEdit" id="pageblock1">  
        <div id="header" class="headerborder">
            <p id="title">DT Worksheet</p>
        </div>
        <apex:pageBlockButtons location="bottom">  
               <apex:commandButton id="saveButton" value="Save" rerender="" action="{!saveme}"/>  
               <apex:commandButton id="cancelButton" value="Cancel" rerender=""/>  
        </apex:pageBlockButtons> 
    
        

        <div class="tableborder" id="border">
    <apex:pageBlockTable value="{!lstMedicals}" var="med" id="table">
        
        <apex:column headerValue="Medical Bill Number" headerClass="headerStyle">
                <apex:actionRegion >
                      <apex:outputLink value="/{!med.Id}" styleClass="link">
                            {!med.Name}
                      </apex:outputLink>
                </apex:actionRegion>  
        </apex:column>

            <apex:column headerValue="Provider" headerClass="headerStyle">
                <apex:actionRegion >
                      <apex:outputField value="{!med.Provider_Name__c}">
                      </apex:outputField>
                </apex:actionRegion>  
        </apex:column>

        <apex:column headerValue="Pay?" headerClass="headerStyle" id="column1">
                <apex:actionRegion id="actionRegion1">
                      <apex:outputField value="{!med.Disburse__c}" id="pay">
                              <apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton"/>
                      </apex:outputField>
                </apex:actionRegion>  
        </apex:column>

        <apex:column headerValue="Notes" headerClass="headerStyle">
                <apex:actionRegion >
                      <apex:outputField value="{!med.Disbursement_Note__c}">
                              <apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" id="note"/>
                      </apex:outputField>
                </apex:actionRegion> 
        </apex:column>
    </apex:pageBlockTable>
</div>
 </apex:pageBlock>  
</apex:form>  

    </html>

</apex:page>

Extension:
public with sharing class GetMedicals {
    public List<Medical_Bill__c> lstMedicals{get;set;}

    public GetMedicals() {

    }

     public GetMedicals(ApexPages.StandardController controller)
     {
               lstMedicals = [Select Id, Name, Provider_Name__c, Disburse__c, Disbursement_Note__c From Medical_Bill__c LIMIT 10 ];
    }

    public pagereference saveme()
    {
    try
    {
        update lstMedicals;
        
    }   
    catch(DmlException ex)
    {
        ApexPages.addMessages(ex);

    }
    return NULL;
    }       
}