0

We have a custom object, and the Contact object is linked to it. Using a trigger on this custom object, I want a User record to be created using Contact data. Below is my code, and I am getting this error:

Apex trigger creatingportaluser caused an unexpected exception, contact your administrator: creatingportaluser: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Username, Last Name, Email, Nickname]: [Username, Last Name, Email, Nickname]: Trigger.creatingportaluser: line 18, column 1

trigger creatingportaluser on Assignment__c (after insert) {


    for(assignment__c objcustom : trigger.new){
          if( objcustom.submit__c == true){
         User u = new User();
         u.ContactId=objcustom.resource__r.Id;  
         u.username=objcustom.resource__r.Email;
         u.firstname=objcustom.resource__r.FirstName;
         u.lastname=objcustom.resource__r.LastName;
         u.email=objcustom.resource__r.Email;
         u.TimeZoneSidKey='America/Los_Angeles';
         u.LocaleSidKey = 'en_US';
         u.EmailEncodingKey='UTF-8';
         u.LanguageLocaleKey = 'en_US';
         u.alias = 'thething';
         u.profileid = '00e28000001ZSJH'; 
         insert u;

           }
        }
}
2

1 Answer 1

4

A trigger context does not pull in relationship fields by default.

From your code it looks like you are doing this from a trigger so you will have to query the records and use that data

I also removed the insert in the for loop which is an anti pattern and will get you into trouble. The map will ensure only one user is created per resource within this trigger context

trigger creatingportaluser on Assignment__c (after insert) {

    Assignment__c tmp = [Select Id, ...., resource__c, resource__r.email, ...
                             From Assignment__c Where Id In :trigger.New];

    Map<Id,User> usersTBI = New Map<Id,User>();

    for(assignment__c objcustom : tmp){
          if( objcustom.submit__c == true){
             User u = new User();
             u.ContactId=objcustom.resource__c; //No need to use the Id from the relationship here 
             u.username=objcustom.resource__r.Email;
             u.firstname=objcustom.resource__r.FirstName;
             u.lastname=objcustom.resource__r.LastName;
             u.email=objcustom.resource__r.Email;
             u.TimeZoneSidKey='America/Los_Angeles';
             u.LocaleSidKey = 'en_US';
             u.EmailEncodingKey='UTF-8';
             u.LanguageLocaleKey = 'en_US';
             u.alias = 'thething'; //This must be unique so will fail the next time it runs or if there are multiple records in the trigger
             u.profileid = '00e28000001ZSJH'; //You should be querying for this and not hardcoding the value

             usersTBI.put(objcustom.Resource__c,u);

           }
        }

   insert usersTBI.values();
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .