+ Start a Discussion
Amanda ReamAmanda Ream 

Create a trigger for new user

I am trying to create a trigger that creates a new user record when a custom object picklist is set to 'in progress'. I am getting an error that says "Illegal variable declaration: newUser.FirstName" Can anyone help me out and tell me what this error means? Here is the whole trigger, the bolded line is causing the error:

trigger AutoUserCreate on User (after update) {
    List<User> UserList = new List<User>();
    for( Support_Request__c sr : Trigger.new) {
       if (sr.status__c == 'in progress'){
    User newUser = new User ();
    string newUser.FirstName = Support_Request__c.First_Name__c;
    string newUser.LastName = Support_Request__c.Last_Name__c;
    insert UserList;
    }
   }
}
TechnozChampTechnozChamp
Amanda the trigger is on user, the for loop seems to be on other object. And teh trigger will throw many errors.Please follow proper guidelines.
 
praveen murugesanpraveen murugesan
Hi Amanda,

Change the trigger like this.

trigger AutoUserCreate on User (after update) {
    List<User> UserList = new List<User>();
    for( Support_Request__c sr : Trigger.new) {
       if (sr.status__c == 'in progress'){
    User newUser = new User ();
    newUser.FirstName = Support_Request__c.First_Name__c;
    newUser.LastName = Support_Request__c.Last_Name__c;//insert all required fields.

    insert UserList;
    }
   }
}

Thanks.
Amanda ReamAmanda Ream
Thanks Praveen but when I remove the word string, I get a different error: "Illegal Assignment from Schema.SObjectfield to String." Any ideas on that error?
praveen murugesanpraveen murugesan
Hi Amanda,

You need to write a trigger in custom obj. which is having that picklist. I hope its in  Support_Request__c.

Please write a trigger in Support_Request__c like this.
 
trigger AutoUserCreate on Support_Request__c (after update)
{
	List<User> UserList = new List<User>();
    for( Support_Request__c sr : Trigger.new)
    {
        if (sr.status__c == 'in progress')
        {
			User newUser = new User();
			newUser.FirstName = sr.First_Name__c;
			newUser.LastName = sr.Last_Name__c;.			
			newUser.EmailEncodingKey = 'ISO-8859-1';
			newUser.TimeZoneSidKey = 'Europe/London';
			newUser.LocaleSidKey = 'en_GB';
			newUser.LanguageLocaleKey = 'en_US';
			//insert all required fields
			UserList.add(newUser);
		}		
    }
	insert UserList;
}
And sorry for the dealy.

Thanks.

Praveen Murugesan.