You need to sign in to do that
Don't have an account?
Avinash Ravisankar 13
Trigger does not display error msg on screen
I have a before insert trigger on an object called Request that has to throw an error when a condition gets satisified. The trigger works, but it does not display the error on the vf page itself. It goes into the application to display the same. Please help out.
Apex Trigger trigger ReqValidation on pba__Request__c (before insert) { list<pba__Request__c> relist = [select id,pba__Contact__c,status__c, createddate from pba__Request__c]; for(pba__Request__c r :trigger.new){ for(pba__Request__c req:relist){ if(r.pba__Contact__c == req.pba__Contact__c){ if((req.status__c == 'Active' || req.status__c == 'Pending')&& (req.createddate.date().daysbetween(System.today()))<7) { r.adderror('Close the Active or Pending request'); } } } } }
VF Page <apex:page standardController="contact" extensions="FormController" showHeader="false" sidebar="false" > <apex:form style="background-color:black"> <apex:pageMessages /> <body> <center> <apex:panelGrid columns="1" > <apex:inputfield styleclass="in" value="{!con.FirstName}" html-placeholder=" First Name∗" style="width: 360px; height: 40px" /> <apex:inputField styleclass="in" value="{!con.LastName}" html-placeholder=" Last Name∗" style="width: 360px; height: 40px" /> </apex:panelGrid> <apex:panelGrid columns="2" > <apex:inputField styleclass="in" value="{!con.Country_Code__c}" style="width: 80px; height: 40px" /> <apex:inputField styleclass="in" value="{!con.Phone}" html-placeholder=" Mobile Number∗" style="width: 280px; height: 40px" /> </apex:panelGrid> <apex:panelGrid columns="1" > <apex:inputField styleclass="in" value="{!con.email}" required="true" html-placeholder=" Email Address∗" style="width: 360px; height: 40px" /> <apex:inputField styleclass="in" value="{!con.Nationality__c}" html-placeholder=" Nationality∗" style="width: 360px; height: 40px"/> <apex:inputField styleclass="in" value="{!con.Preferred_Time_to_Call__c}" style="width: 360px; height: 40px"/> <div class="g-recaptcha" data-sitekey='6LfeAxEUAAAAABhd_5yXn6X6nq355jbF0nvtEoQD'></div> <script src='https://www.google.com/recaptcha/api.js'></script> <br/> </apex:panelGrid> <apex:panelGrid columns="3"> <apex:outputLabel value="AGENT" styleClass="in" style="font-size:12px; margin-left: 1cm; font-family:Verdana"/> <apex:selectradio value="{!con.Agent__c}" > <apex:selectOption itemValue="Yes" itemLabel="Yes"> </apex:selectOption> <apex:selectOption itemValue="Yes" itemLabel="No"> </apex:selectOption> </apex:selectradio></apex:panelGrid> <apex:commandButton style=" margin-right: 7cm; background:#c00;width:75px;height:30px;color:white;font-size:12pt;" action="{!submit}" value="Submit" /></center> <p></p><br/><br/> <script src='https://www.google.com/recaptcha/api.js'></script> </body> </apex:form> <script src='https://www.google.com/recaptcha/api.js'></script> <style> h1 { color:white; font-size:18pt; } p { color:black; font-size:18pt; } .in { font-size:10pt; color:white; width:82%; background-color: #000000; } label { display: block; width: 150px; color:#ffffff; font-family:"Verdana" } input[type=checkbox] { border: 0px; width: 2%; height: 2em; background-color: #ffa500; } </style> </apex:page>
Apex Class public class FormController { public string email ; //public string Nationality__c; //public string Preferred_Time_to_Call__c; public Apexpages.StandardController controller; public Apexpages.StandardController control; public contact con {get;set;} public FormController (ApexPages.StandardController stdController) { this.con = (contact)stdController.getRecord(); this.controller = stdController; } public class ReCaptchaController { public contact cont {get;set;} public ReCaptchaController.ReCaptchaController(ApexPages.StandardController control){ this.cont = (contact)control.getRecord(); // this.stdController= control; } public String FirstName{ get; set; } public String LastName{ get; set; } public String email{ get; set; } public String Phone{ get; set; } // public string Nationality__c{ get; set; } // public String Preferred_Time_to_Call__c{ get; set; } private String baseUrl = 'https://www.google.com/recaptcha/api/siteverify'; private String secret = '6LfeAxEUAAAAALR8nFYrEBlv8SVx8TpxujF4hqqk'; public String sitekey { get{return '6LfeAxEUAAAAABhd_5yXn6X6nq355jbF0nvtEoQD';} } public String response { get { return ApexPages.currentPage().getParameters().get('g-recaptcha-response'); } } // this method is called when the button is clicked public PageReference doVerify () { String responseBody = makeRequest(baseUrl, 'secret=' + secret + '&response='+ response ); String success = getValueFromJson(responseBody, 'success'); if(success.equalsIgnoreCase('true')){ Contact con = new COntact( LastName = LastName , FirstName= FirstName, email=email); try{ insert con; pagereference p = new pagereference('https://cs86.salesforce.com/003/o'+con.id); return p; } Catch(exception e){ ApexPages.Message errorMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Unexpected error while creating contact'); ApexPages.addMessage(errorMsg); return null; } }else{ ApexPages.Message errorMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Please verify the captcha'); ApexPages.addMessage(errorMsg); return null; } } /** * Make request to verify captcha * @return response message from google */ private String makeRequest(string url, string body) { HttpResponse response = null; HttpRequest req = new HttpRequest(); req.setEndpoint(url); req.setMethod('POST'); req.setBody (body); try { Http http = new Http(); response = http.send(req); return response.getBody(); } catch(System.Exception e) { System.debug('ERROR: ' + e); } return '{"success":false}'; } /** * to get value of the given json string * @params * - strJson json string given * - field json key to get the value from * @return string value */ public string getValueFromJson ( String strJson, String field ){ JSONParser parser = JSON.createParser(strJson); while (parser.nextToken() != null) { if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)) { if(parser.getText() == field){ // Get the value. parser.nextToken(); return parser.getText(); } } } return null; } } // ================================================= // public pagereference submit(){ try { System.debug('Entered into save'); if(con.FirstName == null || con.LastName == null || con.Nationality__c == 'Nationality*' || con.Email == null || con.Country_Code__c == 'Code*' ||con.Phone == null || con.Preferred_Time_to_Call__c == 'Preferred time to Call*') { ApexPages.addMessage(new Apexpages.Message(ApexPages.severity.error,'Please fill all mandatory fields.')); return null; } } catch(DMLException e) { return null; } string conname = con.FirstName+' '+con.LastName; system.debug('conname====>'+conname ); integer flag =1; list<contact> conlist = [select name,email from contact ]; for(contact c:conlist){ if(c.name == conname && c.email == con.email){ pba__Request__c r = new pba__Request__c(); r.pba__Contact__c = c.id; r.status__c = 'Pending'; insert r; flag++; } } system.debug('flag==='+flag); if(flag==1) { insert con; pba__Request__c r = new pba__Request__c(); r.pba__Contact__c = con.id; r.status__c = 'Pending'; insert r; } // ApexPages.Message errorMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Please verify the captcha'); // ApexPages.addMessage(errorMsg); //return null; pagereference pg = new pagereference('http://omniyat-omniyat.cs86.force.com/thankyou'); //setredirect(true); return pg; //return pg; } }