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
SFDC GuestSFDC Guest 

Help me to move trigger logic into future method in helper class

I have written trigger to create a contact and assign it community user before user is created. But I am getting mixed DML issue due to the insertion setup&non setup objects in one transation.
Please show me how to move below logic in future method of helper class. We cant pass List<User> as parameter in future method. Plz let me know how to pass ids or strings in below logic.

Here is the trigger:
 
trigger CreateContact on User (before insert) {

   List<User> listUser = new List<User>();  
   for (User usr : trigger.new)   
   {  
     if (String.isBlank(usr.contactId))   
     {  
       listUser.add(usr);  
     }  
   }  
   if (listUser.size() > 0)   
   {  
    List<Contact> newContacts = new List<Contact>();  
    Map<String, User> userNameMap = new Map<String, User>();  
     //Create Contact For Each User  
     for (User usr : listUser)   
     {  
       String contactLastName = usr.lastname;  
       userNameMap.put(contactLastName,usr);  
       Contact con = new Contact(LastName = contactLastName);  
       newContacts.add(con);  
     }  
     Insert newContacts;  
     for (Contact con : newContacts)   
     {  
       //Put Contact Id's on Contacts  
       if (userNameMap.containsKey(con.LastName))   
       {  
         userNameMap.get(con.LastName).contactId = con.Id;  
       }  
     }  
   }   
 }

 
ANUTEJANUTEJ (Salesforce Developers) 
As mentioned in other post regarding the same query could you try checking the links posted once?

 
SFDC GuestSFDC Guest
Anu, 
please dont reply if you don't know. Let someone reply. 
I know how to write helper class, but unable to do future method for this scenario.  
ANUTEJANUTEJ (Salesforce Developers) 
Yes, for that I have mentioned this link https://www.salesforcehut.com/2020/02/simple-technique-to-pass-sobject-list.html I was asking if this was helpful?

So in the future method you can do similarly as mentioned for list of contacts.