• Daniel Sassone
  • NEWBIE
  • 0 Points
  • Member since 2016
  • Intern
  • Courion

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies

Hello!  I'm very new to Apex and programming in general.

 

I'm creating a test class, but every time I run the test I get an error with my ProfileID.  I tried just taking it out, but it's a required field.  I tried assigning a random value to it directly (ie: u.ProfileID = '00Ee0000000CodE';)  but that gives me an error that the ID is invalid.  So I'm assuming I need to reference the ID.  I have no idea how to do that however, so any help would be greatly appreciated!

 

Here is my code.

@isTest
private class UserContactTest {
    testmethod static void userCreationTest(){
        User u = new User();
        u.Firstname = 'mike';
        u.LastName = 'Smith';
        u.email = 'msmith@company.com';
        u.Alias = 'alias';
        u.CommunityNickname = 'commnick';
        u.EmailEncodingKey = 'UTF-8';
        u.Username = 'msmith@company.com';
        u.TimeZoneSidKey = 'America/Denver';
        u.LocaleSidKey = 'en_US';
        u.EmailEncodingKey = 'UTF-8';
        u.LanguageLocaleKey = 'en_US';
        //u.ProfileId = [SELECT id FROM ]; //not really sure what I'm doing here
        insert u;
    }
}

 

Hello!
I am very new to both Salesforce and Apex, so go easy on me!

I am trying to create an Apex trigger, which creates a Contact record and automatically populates some of the fields, when someone creates a User with '@companyname.com' in the email address.

I was able to do this with Process Builder somewhat, however the only field I can't seem to populate that way, is the Account Name.  So I'm attempting to do it in Apex, though my programming knowledge is limited to Python and HTML.  Here is the code I was able to piece together, which unfortunately is still very much a rough draft.

trigger NewContactOnUser on User (after insert) {
    List<Contact> Contact_List = new List<Contact>();
    for (User New_User: trigger.new){
       
        //Conditional statement to check if new contact has @companyname.com in email field
        Contact contact_variable = new_contact();
        String new_contact_string = contact_variable.emailvalue__c;
        if (new_contact_string.contains('@companyname.com')){
  
         Contact New_Contact = new.Contact();
         New_Contact.FirstName = New_User.FirstName;
         New_Contact.LastName = New_User.LastName;
         New_Contact.Email = New_User.Email;
                contacts.add(New_Contact);
        }
    }
    insert Contact_List;
}

So I suppose my question is, how would I eventually get it to populate the Account Name field?  I did '(after insert)' because I think I need the ID for the user after it is created to then get the account name.

 

Thanks for any and all help!

Hello!  I'm very new to Apex and programming in general.

 

I'm creating a test class, but every time I run the test I get an error with my ProfileID.  I tried just taking it out, but it's a required field.  I tried assigning a random value to it directly (ie: u.ProfileID = '00Ee0000000CodE';)  but that gives me an error that the ID is invalid.  So I'm assuming I need to reference the ID.  I have no idea how to do that however, so any help would be greatly appreciated!

 

Here is my code.

@isTest
private class UserContactTest {
    testmethod static void userCreationTest(){
        User u = new User();
        u.Firstname = 'mike';
        u.LastName = 'Smith';
        u.email = 'msmith@company.com';
        u.Alias = 'alias';
        u.CommunityNickname = 'commnick';
        u.EmailEncodingKey = 'UTF-8';
        u.Username = 'msmith@company.com';
        u.TimeZoneSidKey = 'America/Denver';
        u.LocaleSidKey = 'en_US';
        u.EmailEncodingKey = 'UTF-8';
        u.LanguageLocaleKey = 'en_US';
        //u.ProfileId = [SELECT id FROM ]; //not really sure what I'm doing here
        insert u;
    }
}

 

Hello!
I am very new to both Salesforce and Apex, so go easy on me!

I am trying to create an Apex trigger, which creates a Contact record and automatically populates some of the fields, when someone creates a User with '@companyname.com' in the email address.

I was able to do this with Process Builder somewhat, however the only field I can't seem to populate that way, is the Account Name.  So I'm attempting to do it in Apex, though my programming knowledge is limited to Python and HTML.  Here is the code I was able to piece together, which unfortunately is still very much a rough draft.

trigger NewContactOnUser on User (after insert) {
    List<Contact> Contact_List = new List<Contact>();
    for (User New_User: trigger.new){
       
        //Conditional statement to check if new contact has @companyname.com in email field
        Contact contact_variable = new_contact();
        String new_contact_string = contact_variable.emailvalue__c;
        if (new_contact_string.contains('@companyname.com')){
  
         Contact New_Contact = new.Contact();
         New_Contact.FirstName = New_User.FirstName;
         New_Contact.LastName = New_User.LastName;
         New_Contact.Email = New_User.Email;
                contacts.add(New_Contact);
        }
    }
    insert Contact_List;
}

So I suppose my question is, how would I eventually get it to populate the Account Name field?  I did '(after insert)' because I think I need the ID for the user after it is created to then get the account name.

 

Thanks for any and all help!