+ Start a Discussion
Ajay Kumar 583Ajay Kumar 583 

How to Sovle this?

Hi,
On Lead Record, email field must contain the first word of Lead Company field after the character ‘@’.

Thanks,
Best Answer chosen by Ajay Kumar 583
Ajay K DubediAjay K Dubedi
Hi Ajay,
Try this trigger handler and also inactive validation rule if you have created on Email field:
 
public class LeadTrigger_handler {
    public static void chaeckEmail(List<Lead> leadList) {
        try 
        {
            for(Lead l : leadList) {
                List<String> filterLogicSplittedbySpace = l.Company.split(' ');
                string temp = '@' + filterLogicSplittedbySpace[0].toLowercase();
                if(!l.Email.contains(temp)) {
                    l.addError('Invalid Email...');
                }
            }
        }
        catch(Exception ex)
        {
            System.debug('Exception on Line Number ' + ex.getLineNumber() + ' Message ---- ' + ex.getMessage());
        }
    }
}



Thanks,
Ajay Dubedi

All Answers

Ajay K DubediAjay K Dubedi
Hi Ajay,
You can do this with the help of trigger or validation rule:

Try this trigger:
 
trigger LeadTrigger on Lead (before insert, before update) {
    if((trigger.IsInsert && trigger.IsBefore) || (trigger.IsUpdate && trigger.IsBefore)) {
        LeadTrigger_handler.chaeckEmail(trigger.new);
    }
}


Handler class:
 
public class LeadTrigger_handler {
    public static void chaeckEmail(List<Lead> leadList) {
        try 
        {
            for(Lead l : leadList) {
                if(!l.Email.contains('@' + l.Name)) {
                    l.addError('Invalid Email...');
                }
            }
        }
        catch(Exception ex)
        {
            System.debug('Exception on Line Number ' + ex.getLineNumber() + ' Message ---- ' + ex.getMessage());
        }
    }
}



Also try this validation rul:

Error Condition Formula : NOT(CONTAINS(Email , '@'+Company))

Error Message : Invalid Email...

Apply this rule on Email field of Lead Object.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Ajay Kumar 583Ajay Kumar 583
Hi Ajay,

It wont work as per my logic.

For example  if Lead company Name is :   Indian Inc
One needs to  provide  Indian word in the Email field after the @ character.

 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Ajay,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger LeadEmailCompanyName on Lead (before insert, before update) {
    
    for(Lead ld : trigger.new) {
        String s2 = ld.Company.substringBefore(' ');
        if(ld.Email=='' || ld.Email==null || !ld.Email.contains('@' + s2)) {
            ld.addError('Email should contain Company name after @');
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi Ajay,
Try this trigger handler and also inactive validation rule if you have created on Email field:
 
public class LeadTrigger_handler {
    public static void chaeckEmail(List<Lead> leadList) {
        try 
        {
            for(Lead l : leadList) {
                List<String> filterLogicSplittedbySpace = l.Company.split(' ');
                string temp = '@' + filterLogicSplittedbySpace[0].toLowercase();
                if(!l.Email.contains(temp)) {
                    l.addError('Invalid Email...');
                }
            }
        }
        catch(Exception ex)
        {
            System.debug('Exception on Line Number ' + ex.getLineNumber() + ' Message ---- ' + ex.getMessage());
        }
    }
}



Thanks,
Ajay Dubedi
This was selected as the best answer
Ajay Kumar 583Ajay Kumar 583
Hi Khan and Ajay,

Its not working. 

Thanks
Ajay K DubediAjay K Dubedi
Hi Ajay,
Add dubug after every line of your code and check the result.
In my org this working fine.
And also try this logic:
for(Lead l : leadList) {
                string temp = l.Email.substringAfter('@');
                system.debug('temp-----' + temp);
                string temp2 = temp.substringBefore('.');
                system.debug('temp2-----' + temp2);
                if(!l.Company.containsIgnoreCase(temp2)) {
                    l.addError('Invalid Email...');
                }
}
Thanks,
Ajay Dubedi
Ajay Kumar 583Ajay Kumar 583
Thanks Ajay,

It Worked