0

In community context, I wanted to create a simple VFP (VerifyYourIdentity) that accepts the verification code sent in email and upon entering the same code, it should take to next VFP. For the same, I created a VFP and on the constructor, I am sending an email with verification code. Since there is known issue 'SingleEmailMessage does not work in constructor of Apex class Ref:https://help.salesforce.com/articleView?id=000328515&type=1&mode=1, I have used action method in the apex:page tag as suggested. As a result, the entire VFP is executing twice including the constructor, action method and even button click method. How can I overcome this issue?

I have even used Static variable (used to avoid recursive Trigger Logic) only for the constructor and action method and not even the button click method because this will take to the next VFP page and I can't use this Static variable workaround. Please advice me.

VFP code:

<apex:page id="forgeVerifyIdentity" controller="verificationcode" extensions="verificationcodeExtension" action="{!sendEmail}" title="Verify your identity"
apex:form id="verify-form-box"
      apex:outputLabel styleclass="verify-text" value="Confirmation Code" for="confCode"
      apex:inputSecret styleclass="verify-box" required="true" id="vCode" value="{!vCode}"/
      apex:commandButton styleclass="forge-button login-button login-button-disabled" id="confBtn" action="{!registerUser}" value="Save and next"
                apex:form
</apex:page>

verificationcode apex code:

public class verificationcode{
    public string inputValue;
    public static String veriCode;
    public String userName{get;set;}
    public String toLogedinEmailId;
    private static boolean run = true;
    private static boolean gen = true;

    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
     }
    }

    public static boolean genOnce(){
    if(gen){
     gen=false;
     return true;
    }else{
        return gen;
     }
    }

    public verificationcode() {
        String cd = '';
        cd = genRandomNum();
        //if ((cd != null) || (cd != '')){
        if (cd != '1'){
            System.debug('--------->'+cd);
            veriCode = cd;
            inputValue = cd;
            toLogedinEmailId = u.email;
        }
        System.Debug('--------->4:'+veriCode);
        System.Debug('--------->5:'+inputValue);
     }

    public static String genRandomNum() {
        if (!genOnce())
            return '1';
        Integer randomNumber = Integer.valueof((Math.random() * 100000));
        System.debug('randomNumber  is'+randomNumber);
        //veriCode =  String.valueOf(randomNumber);
        //System.Debug('--------->1:'+veriCode);
        return(String.valueOf(randomNumber));
    }

    public static void sendEmail() {
        if (!runOnce())
            return;
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        // Set recipients to two contact IDs.
        // Replace IDs with valid record IDs in your org.
        String userId = UserInfo.getUserId();
        message.toAddresses = new String[] { userId };
        message.optOutPolicy = 'FILTER';
        message.subject = 'Verify your email';
        message.plainTextBody = 'From Forge Community. Verification code:'+veriCode;
        Messaging.SingleEmailMessage[] messages = 
            new List<Messaging.SingleEmailMessage> {message};
                 Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
        if (results[0].success) {
            System.debug('The email was sent successfully.'+userId);
        } else {
            System.debug('The email failed to send: '
                  + results[0].errors[0].message);
        }
        System.Debug('--------->2:AtSendEmailCode');
        return;
    }    
}

verificationcodeExtension code:

public class verificationcodeExtension{

    public verificationcodeExtension(verificationcode controller) {

    }

    public string vCode {get;set;}
    public String veriCode;
    private final User usr;

    public verificationcodeExtension(ApexPages.StandardController stdController) {
        this.usr = (User)stdController.getRecord();
        }

    public pageReference registerUser(){
        String siteurl = Site.getBaseSecureUrl();
        String sitePrefix = Site.getPathPrefix();
        PageReference pageReff = new PageReference(siteUrl+sitePrefix+'/ForgeChangePassword');
        system.debug('@@@@@@@@@@@@@@@@@@@@'+pagereff);
           return pageReff;
    }
}

Your Answer

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

Browse other questions tagged or ask your own question.