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
niven sfniven sf 

Update checkbox in Contact object if an user is exists with Community User Profile with the same email of Contact which want to be update checkbox

Gaurish Gopal GoelGaurish Gopal Goel
Hi Niven,

You should write an Apex Trigger on User object on Insert and Update event. Query all the contacts in your trigger and then search the Email address of the inserted/updated User. If an email matches with a contact's email then mark the checkbox of that contact.

Please mark this answer as the solution to help others. Please let me know if you need help with writing the trigger.

Thanks,
Gaurish
niven sfniven sf
Hello Gaurish,

  Do you have any code for this send me please.
Gaurish Gopal GoelGaurish Gopal Goel
I assume that you have created a custom field on Contact object. The field name is Checkbox__c. Below is the code for trigger.
trigger checkCommunityUser on User(after insert, after update)
{
	Map<String,Contact> contactEmailMap = new Map<String,Contact>();
	List<Contact> contactEmails = [SELECT Id,Checkbox__c,Email 
								     FROM Contact 
								     WHERE Email != null 
								     LIMIT 50000];
									 
	if(contactEmails != null && contactEmails.size() > 0)
	{
		for(Contact con: contactEmails)
		{
			contactEmailMap.put(con.Email, con);
		}
	}
	List<Contact> contactToUpdate = new List<Contact>();
	for(User u: Trigger.New)
	{
		//Change the community profile name to the exact profile name which you want.
		if(u.Profile.Name == 'COMMUNITY' && contactEmailMap.values().contains(u.Email))
		{
			contactToUpdate.add(new Contact(Id = contactEmailMap.get(u.Email).Id, Checkbox__c = True));
		}
	}
	if(contactToUpdate.size() > 0)
	{
		update contactToUpdate;
	}
}

If this answer solves your problem then mark it as the solution to help others. Thanks.
niven sfniven sf
Hello Gaurish,

I am getting error in line 20 that is 'Method does not exist or incorrect signature: void contains(String) from the type List<Contact>'
Gaurish Gopal GoelGaurish Gopal Goel
Hi Niven,

Change values() to Keyset() at line number 20.
Gaurish Gopal GoelGaurish Gopal Goel
trigger checkCommunityUser on User(after insert, after update)
{
	Map<String,Contact> contactEmailMap = new Map<String,Contact>();
	List<Contact> contactEmails = [SELECT Id,Checkbox__c,Email 
								     FROM Contact 
								     WHERE Email != null 
								     LIMIT 50000];
									 
	if(contactEmails != null && contactEmails.size() > 0)
	{
		for(Contact con: contactEmails)
		{
			contactEmailMap.put(con.Email, con);
		}
	}
	List<Contact> contactToUpdate = new List<Contact>();
	for(User u: Trigger.New)
	{
		//Change the community profile name to the exact profile name which you want.
		if(u.Profile.Name == 'COMMUNITY' && contactEmailMap.keyset().contains(u.Email))
		{
			contactToUpdate.add(new Contact(Id = contactEmailMap.get(u.Email).Id, Checkbox__c = True));
		}
	}
	if(contactToUpdate.size() > 0)
	{
		update contactToUpdate;
	}
}

 
niven sfniven sf
It is not working to me, checkbox is not updated.
Gaurish Gopal GoelGaurish Gopal Goel
You must be doing something wrong. The code is working fine for me.

Have you changed the Community name in the IF condition?
niven sfniven sf
Hello Gaurish,

 Yes, I have changed the community name.
Gaurish Gopal GoelGaurish Gopal Goel
Did you insert or update any user? You will have to update a user whose email address matches with the contact's email.
niven sfniven sf
I have to update an user which is having same email address in Contact
Gaurish Gopal GoelGaurish Gopal Goel
Yes, because the trigger will only execute when you will insert/update a User record.
niven sfniven sf
ya but it does not works . I have check for insert user also does not works
Gaurish Gopal GoelGaurish Gopal Goel
Please share your trigger code, so that I can take a look. If it is working fine for me then it should also work for you.