• Chris Mazur
  • NEWBIE
  • 10 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
I'm creating a registration handler to provision new users to my Community site.  
When a user is logging in from a 3rd party SSO, I will have their email.   I want to use their email to see if they are already in salesforce as a contact.  I'm struggling with the code.  
 
When a user logs in for first time, I'll create a new Community User.
If the user already exists as a Contact, then I just link that contact to the Community User.
If the user does not already exist as a Contact then I create a new contact and link to the Community User.  
 
Account SFCOMMUNITYACCOUNT = [SELECT Id FROM account WHERE name = 'New Community User'];
ID SFCOMMUNITYACCOUNT_ID = SFCOMMUNITYACCOUNT.id;
        
// Get user details from KeyCloak
Contact SFCONTACT = new Contact();
SFCONTACT.email = KeyCloakData.email;
SFCONTACT.lastName = KeyCloakData.lastName;
SFCONTACT.firstName = KeyCloakData.firstName;
 
// if user does not exist as a Contact, create a new Contact & assign to account New Community User
SFCONTACT.accountId = SFCOMMUNITYACCOUNT_ID;
// if user does exist as a Contact, use their existing Contact info
SFACCOUNT.accountId = [SELECT Id from Account WHERE email =:KeyCloakData.email];
     
upsert SFCONTACT Contact.fields.Email; // If the contact exists, then update.  If the contact did not exist, then create it.
 
But I don't know how to write the code to lookup Contacts to see if any already are using this email.   Im pretty sure I use SELECT but and stuck.  Anyone have any suggestions.?
// if user does not exist as a Contact, create a new Contact & assign to account New Community User
SFCONTACT.accountId = SFCOMMUNITYACCOUNT_ID;
// if user does exist as a Contact, use their existing Contact info
SFACCOUNT.accountId = [SELECT Id from Account WHERE email =:KeyCloakData.email];