0

Is there an easy way to add a pop-up alert message to my Apex Code that would notify the user (upon save incomplete) they need to check for fields that weren't completed?

Here is my Apex code:

public class IntakeExtension {
    public ApexPages.StandardController ctrl;
    public IntakeExtension(ApexPages.StandardController controller) {
             this.ctrl = controller;
             Opportunity opp = (Opportunity)controller.getRecord();
             opp.StageName = 'Assessment';
             opp.CloseDate = Date.today();
             opp.Name = 'OppName';
             
        }

   public PageReference save() {
    // Returns PageReference only on success
    if(this.ctrl.save() != null) {
        return Page.Congratulations;
    }
    // There was an error.
    return null;
   }
}

1 Answer 1

1

As apex is the language you use in the backend you can send error from apex to your frontend which can be visualforce page or lightning component or lightning web component.

handling errors in lightning you can check Error Handling Best Practices for Lightning and Apex

Handing errors from apex in Lightning component controller in Documentation:

 public with sharing class SimpleErrorController {

    static final List<String> BAD_WORDS = new List<String> {
        'bad',
        'words',
        'here'
    };
    
    @AuraEnabled
    public static String helloOrThrowAnError(String name) {

        // Make sure we're not seeing something naughty
        for(String badWordStem : BAD_WORDS) {
            if(name.containsIgnoreCase(badWordStem)) {
                // How rude! Gracefully return an error...
                throw new AuraHandledException('NSFW name detected.');
            }
        }
        
        // No bad word found, so...
        return ('Hello ' + name + '!');
    }
    
}

Controller:

({
    "callServer" : function(cmp) {
        var action = cmp.get("c.helloOrThrowAnError");
        action.setParams({ name : "bad" });

        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                console.log("From server: " + response.getReturnValue());
            }
            else if (state === "INCOMPLETE") {
                // do something
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        // log the error passed in to AuraHandledException
                        console.log("Error message: " + 
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });

        $A.enqueueAction(action);
    }
})

For apex and visualforce implementation you can check this link that has the below implementation:

public with sharing class ErrorMessageInVfController {

    public Account acc{get;set;}

    public ErrorMessageInVfController(ApexPages.StandardController controller) {

        acc = new Account();

    }

 

    public void save(){

      if(acc.name == '' || acc.name == null)

       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account name'));

 

      if(acc.AccountNumber == '' || acc.AccountNumber == null)

       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account number'));

 

      if(acc.phone == '' || acc.phone == null)

       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Account phone'));

 

      if(acc.site == '' || acc.site == null)

       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Please enter Account site'));

 

      if(acc.industry == '' || acc.industry == null)

       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter Account industry'));

 

    }

}

<apex:page standardController="Account" extensions="ErrorMessageInVfController">

 <apex:form >

   <apex:pageblock >

      <apex:pageMessages id="showmsg"></apex:pageMessages>

         <apex:panelGrid columns="2">

           Account Name: <apex:inputText value="{!acc.name}"/>

           Account Number: <apex:inputText value="{!acc.AccountNumber}"/>

           Account Phone: <apex:inputText value="{!acc.phone}"/>

           Account Site: <apex:inputText value="{!acc.site}"/>

           Account Industry: <apex:inputText value="{!acc.industry}"/>

           <apex:commandButton value="Update" action="{!save}" style="width:90px" rerender="showmsg"/>

         </apex:panelGrid>

    </apex:pageblock>

 </apex:form>

</apex:page>

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.