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
Mehdi EL BilaliMehdi EL Bilali 

Unresolved Items Apex class

Hi Everyone,

I wanted to create "Unresolved Items"(Tasks or Emails) from Email Service apex class if the cc email is not associated with any contact record.

Is this be possible? or there will be some other workaround for achieving this with the email services?

Any Help will be appreciated

Thanks in advance
Blake TanonBlake Tanon
You could try something like this...  There are clear issues with this code though, you'll want to make sure that limits aren't hit and the searchlist sizes are valid for getting the leads and contacts or you'll return errors.

List<List<SObject>> searchList = new list<list<SObject>>();
String[] ccAddresses;

//add your ccAddresses

searchList = [FIND :ccAddresses IN ALL FIELDS
     RETURNING Contact (id, Name, email),
      Lead (id, name, email)];

if(searchList.size() > 0){

set<string> emailSet = new set<string>();
  
contact [] conts = ((List<contact>)searchList[0]);
for(contact c: conts){
  if(c.email != null)emailSet.add(c.email);
}
lead [] leads = ((List<lead>)searchList[1]);
for(lead l: leads){
  if(l.email != null)emailSet.add(l.email);
}

//now check to see if any of the CC emails are missing

list<task> newTasks = new list<task>();

for(string cc:ccAddresses){
  if(emailSet.contains(cc) == false){
   newTasks.add( new task(
    subject = 'follow up on email',
    ownerid = 'put in a user'
   ));
  }
}

if(newTasks.size() > 0){
  insert newTasks;
}

}