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

Help with bulkifying the trigger
How to bulkify this trigger
trigger UpdateUserFields on User (before insert ,before update) {
Set<String> userEmails = new Set<String>();
if(Trigger.isInsert){
for(User u :trigger.new){
userEmails.add(u.Email);
}
}
if(Trigger.isUpdate){
for(User u : trigger.new)
{
User oldUser =trigger.OldMap.get(u.id);
userEmails.add(oldUser.Email);
}
}
List<Contact> conList =new List<Contact>([Select Email ,Code__c , BTF__c from Contact where Account.Name = 'Test' and id IN :userEmails]);
for(User u : trigger.new){
if(u.Email == conList[0].Email){
u.Testfield1__c = conList[0]. Code__c ;
u.TestField2__c = conList[0].BTF__c;
}
}
}
Thanks
Is there any relationship between user and the contact?.
No, Theres no relation ship. The user fields should get updated by matching the email id of the contact with that of user.
There may be multiple contacts with the same email id but the field values will be same for the contacts with same email id.
Thanks
For Before Insert. Try this!!!!!! If this works then modify it for the before update
trigger UpdateUserFields on User (before update, before insert) {
Set<String> userEmails = new Set<String>();
List<Contact> con=new List<Contact>();
Map<String,Contact> conmap=new Map<String,Contact>();
if(Trigger.isInsert && Trigger.isBefore){
for(User u:Trigger.new){
userEmails.add(u.Email);
}
con=[select id,name,Email from Contact where Email IN : userEmails];
if(!con.isEmpty()) {
for(Contact c:con){
conmap.put(c.Email,c);
}
for(User u :Trigger.new){
if(u.Email == conmap.get(u.Email).Email){
u.Testfield1__c = conmap.get(u.Email).Code__c ;
u.TestField2__c = conmap.get(u.Email).BTF__c;
}
}
}
}
}
Thanks you so much. I was able to save the code but the values are not getting updated on the user :(
How did you test this?
I changed the code to include update also . I included the below piece of code
for(User u : trigger.new){
if(Trigger.isUpdate)
{
if(System.Trigger.oldMap.get(u.Id).Email!= System.Trigger.newMap.get(u.Id).Email)
userEmails.add(u.Email);
}
else
userEmails.add(u.Email);
}
and i edited the user record and saved it . and there is a contact with the same email id as user email id. so the fileds on teh user shud get updated with the values of the contact fields. but its not happening
Thanks
See this!!!!!!!
trigger UpdateUserFields on User (before update, before insert) {
Set<String> userEmails = new Set<String>();
List<Contact> con=new List<Contact>();
Map<String,Contact> conmap=new Map<String,Contact>();
if(Trigger.isInsert && Trigger.isBefore){
for(User u:Trigger.new){
userEmails.add(u.Email);
}
if(Trigger.isUpdate && Trigger.isBefore){
for(User u:Trigger.old){
userEmails.add(u.Email);
}
con=[select id,name,Email,Code__c,BTF__c from Contact where Email IN : userEmails];
if(!con.isEmpty()) {
for(Contact c:con){
conmap.put(c.Email,c);
}
for(User u :Trigger.new){
if(u.Email == conmap.get(u.Email).Email){
u.Testfield1__c = conmap.get(u.Email).Code__c ;
u.TestField2__c = conmap.get(u.Email).BTF__c;
}
}
}
}
}