cancel
Showing results for 
Search instead for 
Did you mean: 

SAP C4C : Email Validation using BADI : ValidateReceipientEmail

SubhankarChak
Explorer
408

Hi Team,

I have tried a Email Validation using BADI : #ValidateReceipientEmail. Have tried to concatenate all the email in TO, CC & BCC. Below is the code snippet for your reference -

/*
Add your SAP Business ByDesign scripting language implementation for:
Enhancement Option: ValidateRecipientEmail
Operation: VALIDATE_RECIPIENT_EMAIL
 
Script file signature
----------------------------
Parameter: InputData of type RecipientEmailIn
Returns: ValidateRecipientEmailOut
Author: Subhankar Chakraborty
 
Note: 
  - To use code completion, press CTRL+J.
*/
import AP.CRM.Global;
import AP.Common.Global;
import AP.Common.GDT;
import AP.PlatinumEngineering;
 
var result : ValidateRecipientEmailOut;
var validEmail : Indicator;
var inputEmail : RecipientEmailIn;
var Email : EmailURI;
var invalidEmail : DataType::LANGUAGEINDEPENDENT_EXTENDED_Text;
 
var ToEmail = InputData.TOEmailAddress.content;
var CCEmail = InputData.CCEmailAddress.content;
var BCCEmail = InputData.BCCEmailAddress.content;
 
var resultData : collectionof DataType::LANGUAGEINDEPENDENT_EXTENDED_Text;
var allEamil : collectionof DataType::Email;
 
var HaveMultipleEmail : collectionof DataType::LANGUAGEINDEPENDENT_EXTENDED_Text;
//var HaveMultipleEmail : collectionof DataType::EmailURI;
var allEmail : collectionof DataType::LANGUAGEINDEPENDENT_EXTENDED_Text;
 
var InvalidEmails : collectionof DataType::Indicator;
 
// Split String based on "SPACE"
var toE = StringSplitter.Split(ToEmail, " ");
var ccE = StringSplitter.Split(CCEmail, " ");
var bccE = StringSplitter.Split(BCCEmail, " ");
 
HaveMultipleEmail.Add(toE);
HaveMultipleEmail.Add(ccE);
HaveMultipleEmail.Add(bccE);
 
if (HaveMultipleEmail.Where(n => n.Length() > 0).Count() > 0)
{
allEmail.Add(HaveMultipleEmail.Where(n => n.Length() > 0));
}
 
foreach (var checkAllEmail in allEmail.Where(n => n.Length() > 0))
{
// Mail.CheckEmailAddress -> This is from Platenium Engineering for validating the e-mail
//var email = TextAnalysis.GetEmailAddresses(checkAllEmail);
if (checkAllEmail.Contains("'"))
{
result.IsEmailValid = false;
invalidEmail = checkAllEmail;
break;
}
if (checkAllEmail.Contains("`"))
{
result.IsEmailValid = false;
invalidEmail = checkAllEmail;
break;
}
if (checkAllEmail.Contains("~"))
{
result.IsEmailValid = false;
invalidEmail = checkAllEmail;
break;
}
if (checkAllEmail.Contains("@"))
{
// Split based on @ in String 1 and String 2
var At_Present_Email = StringSplitter.Split(checkAllEmail, "@");
var FirstPartOfEmail = At_Present_Email.GetFirst();
var SecondPartOfEmail = At_Present_Email.GetLast();
 
if (!checkAllEmail.Contains("@"))
{
result.IsEmailValid = false;
invalidEmail = checkAllEmail;
break;
}
//split string2 at . (Dot) in string 3 and  string 4
var SplitOnDot;
var FistPartAfterSplitDot;
var SecontPartAfterSplitDot;
if (SecondPartOfEmail.Contains("."))
{
SplitOnDot = StringSplitter.Split(SecondPartOfEmail, ".");
FistPartAfterSplitDot = SplitOnDot.GetFirst();
SecontPartAfterSplitDot = SplitOnDot.GetLast();
}
else 
{ // If Email doesnot have Dot
result.IsEmailValid = false;
invalidEmail = checkAllEmail;
break;
}
//Check if the 1st Part of @ is NULL, 2nd Part of @ is NULL && 1st Part of . is NULL or 2nd Part of . Id Null
if (FirstPartOfEmail.IsInitial() || SecondPartOfEmail.IsInitial() || FistPartAfterSplitDot.IsInitial() || SecontPartAfterSplitDot.IsInitial())
{
result.IsEmailValid = false;
invalidEmail = checkAllEmail;
break;
}
var checkValidEmail = TextAnalysis.ValidateEmailAddress(checkAllEmail); // It returns the valid email and returns empty string if its not valid
if (!checkValidEmail.IsInitial())
{
result.IsEmailValid = true;
}
}
 
}
if (result.IsEmailValid == false)
{
// DONTSENT - is self defined Error Message is maintained in Enhancement -> Message Definition
raise DONTSENT.Create("E", invalidEmail);
}
return result;
 
SubhankarChak_0-1720081219944.png

 

 
 
baig121
Discoverer
Thanks for the Blog Subhankar.
View Entire Topic
Daphne_Mengwein
Product and Topic Expert
Product and Topic Expert

Thanks for sharing, @SubhankarChak ! Please consider writing a blog post on the topic.

Thanks.