3

After some serious help, I've achieved what I was hoping to achieve in my code. However, now I need to add one more piece. Essentially, I need to test if the User being added has an email address that contains the string "@companyname.com" and if so, to assign it to a specific account.

Right now, the code will automatically assign every user that specific account. To achieve my selective objective this is what I have tried:

  1. Pattern and Matcher
  2. IF(CONTAINS
  3. LIKE %companyname%
  4. Tearing my hair out

So far none of the above methods have been successful.

Here is the code in question.

public class UserContact {   
//Create the class and grab the IDs needed
 public static void createUserContact(set<ID> recordIDs){
//Grab all the required fields from user, essential to test class
    list<user> users = [SELECT ID, Firstname, Lastname, email, name, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey 
                        FROM user
                        WHERE ID in :recordIDs];  

    String accountName = 'Company Internal';
    "LIMIT 1" to avoid more than 1 record returning
     Account Accountx = [SELECT ID
                            FROM account
                           where Name = :accountName
                           LIMIT 1];      
     //Need to create an 'if' statement to test if the contact has a company internal email address, and if so,
     //to assign it the company internal account.
    //Create a list to hold the contacts being created
    List<Contact> contactsToCreate = new List<Contact>();
     //Iterate through each field we want to automatically populate
    for (user x: users){
        Contact userCon = new contact(
        AccountID = AccountX.id,
        FirstName = x.FirstName,
        LastName = x.LastName,
        email = x.Email);
        contactsToCreate.add(userCon);

    insert contactsToCreate;
     }
}
3

Did you try,

    string email= 'firstname123@gmail.com';


system.debug(email.endsWith('@gmail.com'));

This returns true when email contains the specific domain.

  • I did not... In that scenario (excuse my noobness) would 'string' be a variable, or literally the word 'string'? I tried something similar, using 'user.email' in place of 'string' and it gave me an error that I could not reference a static object like that. – Daniel Sassone Mar 29 '16 at 18:12
  • @DanielSassone It's literally the word String. It's calling the static method endsWith() on the String class. Here's a link to the string class documentation that goes over all of the methods you can call. – Derek F Mar 29 '16 at 18:15
  • @DerekF The method is not static. – Keith C Mar 29 '16 at 18:17
  • Thank you! I tried using this, and while the tests passed, even if the newly created user did not have an email that fit the criteria, it would still assign it the account name "Company Internal". I placed 'if(string.endsWith('@companyname.com')){' Around "String accountname = 'Company Internal';" and the Accountx query. – Daniel Sassone Mar 29 '16 at 18:19
  • @Daniel Sassone I have edited my answer , Hope that helps – Anurag A Mar 29 '16 at 18:27
2
String accountName = 'Company Name';
String accountNameInternal = 'Company Internal';
"LIMIT 1" to avoid more than 1 record returning
 Account Accountx = [SELECT ID
                        FROM account
                       where Name = :accountNameInternal
                       LIMIT 1];
Account AccountCompanyName = [SELECT ID
                        FROM account
                       where Name = :accountName
                       LIMIT 1];
for (user x: users){
    Contact userCon = new contact(        
    AccountID = x.email.endsWith('@companyname.com')?AccountCompanyName.id:AccountX.id,
    FirstName = x.FirstName,
    LastName = x.LastName,
    email = x.Email);
    contactsToCreate.add(userCon);
}
insert contactsToCreate; // Move the DML statement out of for loop.

Here AccountCompanyName is the specific Account and AccountX is the generic Account. You should query for both account before hand itself.

Hope it helps.

  • Thanks for the response! I'll be trying this next, and in this case 'AccountCompanyName' is going to be the actual name of the account, not a variable named 'AccountCompanyName', right? – Daniel Sassone Mar 29 '16 at 18:36
  • Updated my answer to include the account queries as well. – Vigneshwaran G Mar 29 '16 at 18:43

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.