You need to sign in to do that
Don't have an account?
Melissa Bunch
Trigger Help - Change Profile ID if...
Hi all,
I have never written a trigger before, so I am having some trouble with the "wording".
What I want the Trigger to do:
On user create, update the User Profile ID to "x" if custom user field = "y" or custom user field = "z".
Below is what I have. I can't even save it because my language is wrong, but I'm hoping I'm not too far off:
trigger CommunityProfileUpdate on User (before insert) {
for (User a : Trigger.new) {
if(User.Partner_Conatct_User_Type__c = 'Retailer');
if(User.Partner_Conatct_User_Type__c = 'Distributor'){
a.ProfileId = '00e13000000jVJm'
}
}
}
Thanks in advance for any help!
Melissa
I have never written a trigger before, so I am having some trouble with the "wording".
What I want the Trigger to do:
On user create, update the User Profile ID to "x" if custom user field = "y" or custom user field = "z".
Below is what I have. I can't even save it because my language is wrong, but I'm hoping I'm not too far off:
trigger CommunityProfileUpdate on User (before insert) {
for (User a : Trigger.new) {
if(User.Partner_Conatct_User_Type__c = 'Retailer');
if(User.Partner_Conatct_User_Type__c = 'Distributor'){
a.ProfileId = '00e13000000jVJm'
}
}
}
Thanks in advance for any help!
Melissa
Please try below code, Thanks,
Govindaraj.S
Use this code and change the profile name based on the assignment on 2 and 3 rd lines
I think I'm closest with the below code, but I am getting an error that either "A value cannot be stored" or "field is not writeable" for the Partner Contct User Type field.
This is a formula field and I'm not trying to write in or store a value there. I just want to read it and if it contains a particular value, then change the Profile on the user.
Error: Compile Error: A value cannot be stored to Partner_Contact_User_Type__c in type User at line 6 column 17
trigger CommunityProfileUpdate on User (before insert) {
Profile pRet = [select Id, Name from Profile where name ='Residential Claims Community User Retailer/Distributor'] ;
for (User a : Trigger.new) {
if(User.Partner_Contact_User_Type__c = 'Retailer'){
a.ProfileId = pRet.Id;
}
if(User.Partner_Contact_User_Type__c = 'Distributor'){
a.ProfileId = pRet.Id ;
}
}
}
I was able to save the trigger now.
Unfortunately it didn't work, though.
Our users are self-registering on our community site and that of course generates the user.
So I went to our community, registered as a new user with Distributor in the Partner Contact User Type field, and I was created with our default Profile. It did not switch it to the Residential Claims Community Retailer/Distributor Profile that I listed in the Trigger.
Should I change when the trigger fires? Like from "before insert" to "after"? Or is my trigger missing something?