+ Start a Discussion
Brookesy--Brookesy-- 

Trigger on User object to create another record

Hiya,

 

 Im after some advice on what i think is going to be a pretty simple bit of code.  Basically i want to write a trigger on the User object that will insert a record into a separate Staff__c object with details of the user just saved. I was unsure how to pull the details of the user just saved. 

 

Here is some rough pseudo code! Unsure if it would even work with something along these lines? But il give it a try while waiting if anyone else has a better idea :)  (updated code, works even though its rough :) )

 

Just need to work out how to get it to fire only once!

 

 

trigger userToStaff on User (after insert, after update) {

 

List<Staff__c> staffToUpdate = new List<Staff__c>();

 

for (User usr : Trigger.new) {

Staff__c staff = new Staff__c (

//User_ID__c = usr.Id

First_Name__c = usr.Name

);

staffToUpdate.add(staff);

}


 

if(staffToUpdate.size()>0){

try {

insert staffToUpdate;

} catch (exception e) {

system.debug(e.getMessage());

}

}

}

 

 

 

Any advice would be great!!! :)

 

Regards

Michael 

Message Edited by Brookesy-- on 12-21-2009 02:23 AM
Message Edited by Brookesy-- on 12-21-2009 05:33 AM
Message Edited by Brookesy-- on 12-21-2009 05:35 AM
Message Edited by Brookesy-- on 12-21-2009 05:36 AM
Message Edited by Brookesy-- on 12-21-2009 06:20 AM
Best Answer chosen by Admin (Salesforce Developers) 
Brookesy--Brookesy--

Taking in all your advice i managed to get this working! :)

 

I found out also from SF that you cant do Triggers that update other objects from the User object. So i had to use a class and call it with @future. Again thanks for everyone's help!!

 

It only fires once due to after insert. 

 

 

Trigger:

 

 

trigger userToStaff on User (after insert) {

addUserToStaff.staffAdd(Trigger.newMap.keySet());

}

 

 Class:

 

 

global class addUserToStaff {

 

@future

public static void staffAdd(Set<Id> userIds) {

List<User> user = [select Id, firstname, lastname, email from user where Id IN :userIds];

List<Staff__c> staffToUpdate = new List<Staff__c>();

 

for (User usr : User) {

Staff__c staff = new Staff__c (

User_ID__c = usr.ID,

First_Name__c = usr.FirstName,

Email__c = usr.Email,

Last_Name__c = usr.LastName,

Name = usr.FirstName + ' ' + usr.LastName

);

staffToUpdate.add(staff);

}

insert staffToUpdate;

}

}

 

 

 

 

 

 

Message Edited by Brookesy-- on 12-29-2009 01:21 AM
Message Edited by Brookesy-- on 12-29-2009 01:24 AM

All Answers

chriseustacechriseustace

I just did a similar thing, keep in mind mine is not yet bulkified.

 

Do you have any experience with writing test classes?  That's the part I'm stuck on now.

 

Code below:

 

 trigger createSupplierMemberLink on Opportunity (after insert) {
    
    Opportunity oppty = trigger.new[0];
    if(oppty.Payroll_Type__c <> ''){
    
    SupplierMemberLink__c sml = new SupplierMemberLink__c();
    sml.RelatedProgram__c = oppty.Id;
    sml.SalesDate__c = oppty.CloseDate;
    sml.SalesAmount__c = oppty.Amount;
    sml.Name = oppty.Name + ' Payroll';
    
    insert sml;
}
}

mtbclimbermtbclimber

If this is the only code in your system I'm not sure how this trigger could be invoked twice. Or by "get it to fire only once" do you mean prevent creation of a new staff__c object when the same user is later updated?

 

If it's the latter then I suggest investigating the upsert operation in the apex developer guide.

 

 

Brookesy--Brookesy--

Taking in all your advice i managed to get this working! :)

 

I found out also from SF that you cant do Triggers that update other objects from the User object. So i had to use a class and call it with @future. Again thanks for everyone's help!!

 

It only fires once due to after insert. 

 

 

Trigger:

 

 

trigger userToStaff on User (after insert) {

addUserToStaff.staffAdd(Trigger.newMap.keySet());

}

 

 Class:

 

 

global class addUserToStaff {

 

@future

public static void staffAdd(Set<Id> userIds) {

List<User> user = [select Id, firstname, lastname, email from user where Id IN :userIds];

List<Staff__c> staffToUpdate = new List<Staff__c>();

 

for (User usr : User) {

Staff__c staff = new Staff__c (

User_ID__c = usr.ID,

First_Name__c = usr.FirstName,

Email__c = usr.Email,

Last_Name__c = usr.LastName,

Name = usr.FirstName + ' ' + usr.LastName

);

staffToUpdate.add(staff);

}

insert staffToUpdate;

}

}

 

 

 

 

 

 

Message Edited by Brookesy-- on 12-29-2009 01:21 AM
Message Edited by Brookesy-- on 12-29-2009 01:24 AM
This was selected as the best answer