function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Melissa BunchMelissa 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
GovindarajGovindaraj
Hi Melissa,

Please try below code,
trigger CommunityProfileUpdate on User (before insert) {
    Profile profileObj= [SELECT Id FROM Profile WHERE Name = '<Name of Profile>' LIMIT 1];
    for (User userObj : Trigger.new) {
        if(userObj.Partner_Conatct_User_Type__c = 'Retailer' || userObj.Partner_Conatct_User_Type__c = 'Distributor') {
            userObj.ProfileId = profileObj.Id;
        }
    }
}

//Replace the <Name of Profile> with the correct profile name.
Thanks,
Govindaraj.S
Raj VakatiRaj Vakati


Use this code and change the profile name based on the assignment  on 2 and 3 rd lines 


 
trigger CommunityProfileUpdate on User (before insert) {
Profile pRet = [select Id, Name from Profile where name ='Retailer Profile'] ; 
Profile pDest = [select Id, Name from Profile where name ='Retailer Distributor'] ; 


    for (User a : Trigger.new) {
        if(User.Partner_Conatct_User_Type__c = 'Retailer'){
			 a.ProfileId = pRet.Id;
		}
        if(User.Partner_Conatct_User_Type__c = 'Distributor'){
            a.ProfileId = pDest.Id ;
        }
    }
}

 
Melissa BunchMelissa Bunch
Thank you both - I have tried each of them and keep getting errors.
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.

User-added image
 
Melissa BunchMelissa Bunch
Sorry, that picture is a bit small. Here you go:

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 ;
        }
    }
}
Raj VakatiRaj Vakati
try this
 
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(a.Partner_Contact_User_Type__c == 'Retailer'){
             a.ProfileId = pRet.Id;
        }
        if(a.Partner_Contact_User_Type__c == 'Distributor'){
            a.ProfileId = pRet.Id ;
        }
    }
}

 
Melissa BunchMelissa Bunch
Great, thanks! I really appreciate the help.
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?
Raj VakatiRaj Vakati
Partner Contact User Type  is formula ?? or Partner Contact User Type  is picklist??
Melissa BunchMelissa Bunch
It is a Formula that is based off a Picklist. But the field I'm referecning is a Formula.
Raj VakatiRaj Vakati
Try like this  ..from Line Number 11 change the correct name for Partner_Contact_User_Type__c on contact
 
trigger CommunityProfileUpdate on User (before insert) {
Profile pRet = [select Id, Name from Profile where name ='Residential Claims Community User Retailer/Distributor'] ; 

    Set<Id> conIds = new Set<Id>();
    for (User a : Trigger.new) {
		if(a.ContactId!=null){
			conIds.add(a.ContactId) ;
		}	 
	}

Map<Id,Contact> conMap =new Map<Id,Contact>([Select Id ,Partner_Contact_User_Type__c  from Contact where Id in :conIds]);

    for (User a : Trigger.new) {
		Contact c = conMap.get(a.ContactId) ;
		
        if(c!=null && c.Partner_Contact_User_Type__c == 'Retailer'){
             a.ProfileId = pRet.Id;
        }
        if(c!=null && c.Partner_Contact_User_Type__c == 'Distributor'){
            a.ProfileId = pRet.Id ;
        }
    }
}