1

im learning Apex breaking the "trailhead Sandbox".

I have already consulted the proper way to log in to a Prod environment. n_n

I have a custom login page whit 2 fields User and Password. Using the contact standar controler i acces to the {!Contact.Email} and {!Contact.Password__c} fields and then send the information to my controler and return the welcome page..

The problem i have is in the Exception! when i type some user or password wrong the exeption send me to a white page whit the erron in it.

--APEX--

<apex:page standardController="Contact" extensions="loginControler" >
    <style>
        #center{
            width:180px;
            margin:auto;
            height:auto;
        }

        .pbBody{
            width:350px;
            margin:auto!important;
        }
    </style>
    <apex:form >
        <apex:pageBlock >
            <br/><br/>
            <apex:image url="https://login.salesforce.com/img/logo198.png" styleClass="thLogoImg"/><br/><br/>
            <div id="center">
                <h1>Welcome to Salesforce org</h1><br/>
                <h2>Second assignment. yeah!!!</h2><br/>
                <apex:pageMessages/>
                <p>User </p>
                <apex:inputField value="{! Contact.Email }"/><br/>

                <p>Password </p>
                <apex:inputField value="{! Contact.Password__c }"/><br/>

                <apex:commandButton action="{!redirect}" value="Login" />

            </div>  
        </apex:pageBlock>

    </apex:form>
</apex:page>

--Controler--

public class loginControler {

    //private List<Contact> contac;
    private Contact tracker{get; set;}

    public loginControler(ApexPages.StandardController controler){
        this.tracker = (Contact)controler.getRecord();        
    }

    public PageReference redirect(){
        try{
            Contact loginContact = [SELECT ID FROM Contact WHERE Email =: tracker.Email AND Password__c =: tracker.Password__c LIMIT 1];
            String contacid = loginContact.Id;
            PageReference pr = new PageReference('/apex/contactsV1?id=' + contacid);
            pr.setRedirect(true);
            return pr;
        } catch (DmlException e){
            ApexPages.addMessages(e);
            //ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'I already tried this option!!');
            //ApexPages.addMessage(myMsg);
            return null;
        }
    }
}
3
  • 2
    You are using DMLException, while there is no DML statement in your controller, so you will never eneter that loop, what happens when you try catch(Exception e){} or catch(QueryException e){} which will trigger if your query comes back with no results
    – Rao
    Oct 13 '17 at 18:59
  • @Rao That should be an answer.
    – sfdcfox
    Oct 13 '17 at 19:13
  • posted as an answer @sfdcfox
    – Rao
    Oct 13 '17 at 19:32
4

You are using DMLException, while there is no DML statement in your controller, so you will never eneter that loop, try catch(Exception e){} or catch(QueryException e){} which will trigger if your query comes back with no results

Refer to this doc to understand different exceptions in salesforce:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_exception_methods.htm

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.