You need to sign in to do that
Don't have an account?

hello friends, i want to create and update a record in object when user is created or update. Iam unable to update. Please look at my code and help
Iam unable to create look Relationship on user object as it doesnot allow. I have created a Relationship on User database object.
1. Create a User lookup field on the Heroku User Database object.
- I have created it
2. Whenever the User is inserted create a copy of the user in Heroku User Database object and also update when the user is updated.
- when user is inserted its creating a copy but Iam unable to update when user is updated. Please look at my code
trigger UpdateHerokuUser on User (after insert, after update) {
set<id> setid = new set<id>();
set<id> updatedset = new set<id>();
map<string ,id> mapid = new map<string,id>();
list<Heroku_User_Database__c> hudlist = new list<Heroku_User_Database__c>();
if(Trigger.isInsert && Trigger.isAfter){
for(user u : Trigger.new){
setid.add(u.id);
}
list<user> usr =[select id, name, Email,Username from user where id IN: setid];
for(user u : usr){
Heroku_User_Database__c hu = new Heroku_User_Database__c();
hu.name= u.name;
hu.Email__c= u.Email;
hu.User__c= u.id;
hu.Username__c = u.Username ;
hudlist.add(hu);
}
insert hudlist;
system.debug('hudid2'+hudlist);
}
if(Trigger.isUpdate && Trigger.isAfter){
for(user u : trigger.new){
updatedset.add(u.id);
}
}
}
1. Create a User lookup field on the Heroku User Database object.
- I have created it
2. Whenever the User is inserted create a copy of the user in Heroku User Database object and also update when the user is updated.
- when user is inserted its creating a copy but Iam unable to update when user is updated. Please look at my code
trigger UpdateHerokuUser on User (after insert, after update) {
set<id> setid = new set<id>();
set<id> updatedset = new set<id>();
map<string ,id> mapid = new map<string,id>();
list<Heroku_User_Database__c> hudlist = new list<Heroku_User_Database__c>();
if(Trigger.isInsert && Trigger.isAfter){
for(user u : Trigger.new){
setid.add(u.id);
}
list<user> usr =[select id, name, Email,Username from user where id IN: setid];
for(user u : usr){
Heroku_User_Database__c hu = new Heroku_User_Database__c();
hu.name= u.name;
hu.Email__c= u.Email;
hu.User__c= u.id;
hu.Username__c = u.Username ;
hudlist.add(hu);
}
insert hudlist;
system.debug('hudid2'+hudlist);
}
if(Trigger.isUpdate && Trigger.isAfter){
for(user u : trigger.new){
updatedset.add(u.id);
}
}
}
What do you mean you're "unable to update" ?
I don't see that you're doing anything with "updatedset" after adding updated user Ids to it. Are you needing help constructin list of Heroku_User_Database__c records so you can issue an update to it?
If so, I think you could do something like the following (untested logic!) in the "if(Trigger.isUpdate && Trigger.isAfter) { ... } " block of code: