You need to sign in to do that
Don't have an account?
Chris Mazur
Help looking Contacts
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];
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];
Contact existingContact = [SELECT Id FROM Contact WHERE Email = :KeyCloakData.email LIMIT 1];
if (existingContact != null) { // If the contact exists, use the existing Contact SFCONTACT.Id = existingContact.Id; SFCONTACT.accountId = existingContact.AccountId; } else { // If the contact does not exist, create a new Contact & assign to account New Community User SFCONTACT.accountId = SFCOMMUNITYACCOUNT_ID; }
Finally, you can use the upsert statement to update or insert the Contact based on the existence of the email:
upsert SFCONTACT Contact.fields.Email;
This will either update the existing Contact if it exists, or create a new Contact if it does not exist.