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
kiwitrotterkiwitrotter 

Problem validating email address in before insert lead trigger's test class.

Hi,

 

Just wondering if anyone can help me. I'm trying to validate the email address field of a lead in my before insert trigger. In my test class I'm simply providing a url as the email address of the test lead and trying to insert it. Thisi s then causing the following validation error to appear in the logs:

 

---

10:23:53.268 (13268597000)|DML_BEGIN|[33]|Op:Insert|Type:Lead|Rows:1
10:23:53.270 (13270063000)|DML_END|[33]
10:23:53.270 (13270191000)|VF_PAGE_MESSAGE|Email: invalid email address: http://www.dodgycompany.com
10:23:53.270 (13270274000)|EXCEPTION_THROWN|[33]|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Email: invalid email address: http://www.dodgycompany.com: [Email]
10:23:53.272 (13272426000)|USER_DEBUG|[36]|DEBUG|Email: invalid email address: http://www.dodgycompany.com
10:23:53.272 (13272706000)|EXCEPTION_THROWN|[37]|System.ListException: List index out of bounds: 1
10:23:53.272 (13272884000)|FATAL_ERROR|System.ListException: List index out of bounds: 1

Class.TestDeDupeLead.testForValidEmailAddress: line 37, column 1
10:23:53.272 (13272898000)|FATAL_ERROR|System.ListException: List index out of bounds: 1
------

 

I don't understand why my attempt to insert a test lead with an invalid email address is failing. In my before insert trigger I have code in place to validate the lead's email address. I thought that my before trigger should run before any salesforce field validation takes place however it appears some kind of other field validation is happening before my 'before insert' trigger.

 

Can anyone shed any light on this? Your help is greatly appreciated!

 

Many thanks in advance!

MoggyMoggy

HI, it might be helpful to have the code of that trigger before trying to guess whats wrong

 

"10:23:53.270 (13270191000)|VF_PAGE_MESSAGE|Email: invalid email address: http://www.dodgycompany.com"

that looks like that you get this error already from the VF-page (I am not familar with VF yet but if that is an email field than the validation here already fails,before it reaches any trigger

ibtesamibtesam

Salesforce provides default email validation in the format *@*.*

that is throwing the error before the insert is done.

Even if you manually go to a lead record and enter the email as http://www.dodgycompany.com you will see the error there.

Vinit_KumarVinit_Kumar

Agreed with ibtesam,if you declare a field as Email in salesforce ,saleforce levies a system validation that it should contain '@' somewhere in the values.As you email value does contain '@',hence the error message.

kiwitrotterkiwitrotter

Hi ibtesam,

 

Thanks for letting me know that. So how can I validate against current leads coming in from a web form being created with a url as the email address? This is currently what's happening for us and what I'm trying to do in my before insert lead trigger is validate the email address using a regular expression and if the email address isn't valid then I use leadname.addError to prevent the lead from being created.

 

Just going by Salesforce documentation on 'Triggers & Order of Execution' (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm) I couldn't understand why my before trigger wasn't firing first as it appears that validation is run after the before triggers.

 

Thanks ibtesam.

kiwitrotterkiwitrotter

Thanks Vinit_Kumar,

 

If you see my latest post to ibtesam, I need to find a way to trap leads coming in from our web form that contain invalid email addresses (e.g url's as the email address) and prevent these leads from being created. I have code in place in my before insert trigger, however, this Salesforce default email validation that kicks in before my before insert lead trigger is preventing me from handling this. Do you know is there some workaround that I might be able to apply?

 

Thanks Vinit_Kumar.

MoggyMoggy

Hi kiwitrotter,

 

the issue is not that any validation is take in place before your trigger is triggered.

The issue here is that you pass this lead to your page with that not valid email and the validation of the page then causes the error

 

I just setup a web to lead and a trigger which works fine, you will need to to find a way to delete that lead before insert if you need to not create it

otherwise you will need to place a fake email inside and then delete it with a workflow after creation

 

when I have the standard web to lead form and i enter as email www.dodgycompany.com

the following trigger will replace that dodgy email with a fake email without any issues

 

trigger myemail on Lead(before insert){

       for(Lead aLead:trigger.new){

             if(!aLead.Email.contains('@'){

                  aLead.Email='wasnot@valid.com';

             }

       }

}

 

so you will need to alter your trigger somehow before passing that lead to the page

MoggyMoggy

you could also build in a short java script inside the web to lead form to validate the email before the customer clicks submit

kiwitrotterkiwitrotter

Hi Moggy,

 

Thanks for your help and advice. Ok so, here's a sample of my trigger code and test class code:

 

-------------
TRIGGER CODE:
-------------
String emailRegex = '([a-zA-Z0-9_\\-\\.]+)@((\\[a-z]{1,3}\\.[a-z]{1,3}\\.[a-z]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})';
Pattern MyPattern = Pattern.compile(emailRegex);
Matcher MyMatcher = MyPattern.matcher(newLead.Email);
                            
if(!MyMatcher.matches()){
    System.debug('This lead is not well formatted!');
    newLead.addError('This lead is not well formatted!');
}


----------------
TEST CLASS CODE:
----------------
    
Lead l2 = new Lead(LastName = 'Dodgy', FirstName = 'Well', Company = 'Dodgy Company', Email = 'http://www.dodgycompany.com');
        
try{
    insert l2;
} catch (DmlException e){
    System.assert(e.getDmlMessage(0).indexOf('This lead is not well formatted!') > -1);
}

 

Now if I submit a lead with an invalid email address with a standard web-to-lead form, the lead doesn't get created, my trigger validation runs and the Salesforce notification email that I receive contains the addError 'This lead is not well formatted'.

 

On our lead submission form on our website we do have javascript validating the email address field so if I try to submit a url or invalid email address I am prevented from doing so by this.

 

My problem lies in the fact that I can't seem to get my test class code to run because of the Salesforce default validation on the email address field. Ideally I would like to be able to do the validation of the email address field myself, and prevent the notification email from Salesforce being sent out. I'm not sure if this is possible though. But as it stands I can't deploy my code to the live system as I cannot get my test code to work.

 

Thanks again for your help on this.

MoggyMoggy

HI,

 

that infact can't work as you try to insert a lead email on the email standard field which is internal validated by default.

 

First of all try to test on a valid email to get some coverage.

 

For the not valid email test you will need to get the apex class to send a generated web form, but this I have never done before
it should be possible through apex to simulate a webform action submit which would be the only way for your test class to test the not valid email address.

 

Maybe here can someone else help HOW TO GENERATE A filled WEBFORM and sent it through apex witnin a test class

you will need to copy the link from your web to lead form into that code <form action="https// NA12.salesforce.com/servlet........">

 

if i find a sample i will post

maybe something like that 

http://boards.developerforce.com/t5/Apex-Code-Development/Need-Help-Creating-Test-Method-for-Web2Lead-Extension/td-p/242173