Salesforce:Apex触发检查电子邮件域是否存在(Salesforce: Apex trigger to check whether email domain exists)

编程入门 行业动态 更新时间:2024-10-15 12:33:07
Salesforce:Apex触发检查电子邮件域是否存在(Salesforce: Apex trigger to check whether email domain exists)

我想知道电子​​邮件域是否存在于数据库中。 例如:如果要插入xxx@abc.com,我想查看已存在或不存在相同域(abc.com)的电子邮件。 以下是检查整个电子邮件的代码。

trigger trig_Contact_webToContact on Contact (before insert) { final String errMsg = 'The email already exists on another Contact: '; Set< String > emailSet = new Set< String >(); for( Contact c : Trigger.new ) emailSet.add( c.Email ); Map< String, Id > duplicateContactMap = new Map< String, Id >(); for( Contact c : [select Id, Email from Contact where Email = :emailSet] ) duplicateContactMap.put( c.Email, c.Id ); for( Contact c : Trigger.new ){ Id duplicateContactId = duplicateContactMap.get( c.Email ); if( duplicateContactId != null ) c.addError( errMsg + duplicateContactId ); } }

I want to find out whether the email domain exist in the database. Eg: If xxx@abc.com is going to be inserted, I want to check email with the same domain(abc.com) already exist or not. Following is the code to check the whole email exist.

trigger trig_Contact_webToContact on Contact (before insert) { final String errMsg = 'The email already exists on another Contact: '; Set< String > emailSet = new Set< String >(); for( Contact c : Trigger.new ) emailSet.add( c.Email ); Map< String, Id > duplicateContactMap = new Map< String, Id >(); for( Contact c : [select Id, Email from Contact where Email = :emailSet] ) duplicateContactMap.put( c.Email, c.Id ); for( Contact c : Trigger.new ){ Id duplicateContactId = duplicateContactMap.get( c.Email ); if( duplicateContactId != null ) c.addError( errMsg + duplicateContactId ); } }

最满意答案

首先,只从电子邮件获取域名,但不是整个电子邮件:

Set< String > emailDomainSet = new Set< String >(); for( Contact c : Trigger.new ) { if (String.isBlank(c.Email)) continue; int indexOfAt = c.Email.indexOf('@'); if (indexOfAt > 0) emailDomainSet.add( c.Email.substring(indexOfAt) ); }

那么,在你的情况下,你需要使用query(queryString)方法。 首先编写一个SOQL字符串

string queryString = 'select name from Contact where '; string whereClause = ' '; for (string domainItem : emailDomainSet) { whereClause += ' or Email like \'%'+domainItem + '\' '; } whereClause = whereClause.substring(4);//remove first or in where clause queryString += whereClause;

然后,运行它。 代替

for( Contact c : [select Id, Email from Contact where Email = :emailSet] )

你应该把

for( Contact c : (List<Contact>)Database.query(queryString) )

First, get only domain from email, but not the whole email:

Set< String > emailDomainSet = new Set< String >(); for( Contact c : Trigger.new ) { if (String.isBlank(c.Email)) continue; int indexOfAt = c.Email.indexOf('@'); if (indexOfAt > 0) emailDomainSet.add( c.Email.substring(indexOfAt) ); }

then, in your case, you need to use query(queryString) method. Compose a SOQL string first

string queryString = 'select name from Contact where '; string whereClause = ' '; for (string domainItem : emailDomainSet) { whereClause += ' or Email like \'%'+domainItem + '\' '; } whereClause = whereClause.substring(4);//remove first or in where clause queryString += whereClause;

then, run it. Instead of

for( Contact c : [select Id, Email from Contact where Email = :emailSet] )

you should put

for( Contact c : (List<Contact>)Database.query(queryString) )

更多推荐

本文发布于:2023-07-07 21:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068436.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是否存在   电子邮件   Salesforce   Apex   trigger

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!