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
naga kiran 2naga kiran 2 

can anybody suggest a trigger so that it automatically creates user for accessing community portal...trigger shld be coming in custom object after a checkbox is checked?

vikas rathi91vikas rathi91
Hi Kiran,
trigger should be like as below.
trigger  createCommunityUser on custom__c ( after insert, after update){
     
   for(custom__c objcustom : trigger.new){
      if( objcustom.checkField__c == true){
     User u = new User();
     u.contactId=con.Id;  // set the contact according to your requirment
     u.username=con.Email;
     u.firstname=con.FirstName;
     u.lastname=con.LastName;
     u.email=con.Email;
     u.TimeZoneSidKey='America/Los_Angeles';
     u.LocaleSidKey = 'en_US';
     u.EmailEncodingKey='UTF-8';
     u.LanguageLocaleKey = 'en_US';
     u.alias = string.valueof(con.FirstName.substring(0,1) + con.LastName.substring(0,1));
     u.profileid = pf.Id;  // select profile according to permission of user
     insert u;
        
       }
    }
}

Hope this trigger solve your problem

if my trigger solve your problem then mark as best answer for anothers members who have same issue

thanks​
GauravGargGauravGarg
Hi Naga,

Explore below link, that is trying to create User on contact update. 
https://salesforce.stackexchange.com/questions/140705/create-user-in-trigger-of-contact

Hope this helps !!!

Thanks,
Gaurav
Skype:gaurav62990
naga kiran 2naga kiran 2
Hi @vikasrathi,

What if our contact is look up of that object and I want details from my contact object to be refrenced...can you please rewrite the code

Thanks,
Naga 
vikas rathi91vikas rathi91
trigger  createCommunityUser on custom__c ( after insert, after update){
     set<string> conId = new set<string>();
     for(custom__c objcustom : trigger.new){
      if( objcustom.checkField__c == true){
	     conId.add(objcustom.contactId) // add the lookup field Api Name  
		 }
	 }  
	 contact con = [select id, FirstName, email, LastName from contact where Id IN : conId limit 1];
     profile pf = [select id from profile where name = 'profileName' limit 1];	 // enter the profile name 
     User u = new User();
     u.contactId=con.Id;  
     u.username=con.Email;
     u.firstname=con.FirstName;
     u.lastname=con.LastName;
     u.email=con.Email;
     u.TimeZoneSidKey='America/Los_Angeles';
     u.LocaleSidKey = 'en_US';
     u.EmailEncodingKey='UTF-8';
     u.LanguageLocaleKey = 'en_US';
     u.alias = string.valueof(con.FirstName.substring(0,1) + con.LastName.substring(0,1));
     u.profileid = pf.Id;  
     insert u;
    }

Try this one If you face any proble then reply on my id vikassalesforce91@gmail.com