0

Hi I developed a trigger class where in if a field in account is updated, it will create a community user that has a license of CustomerCommunityPlusLogin1ML now my goal everytime I update the account it should create a community user with 1 role for all, because what's happening right now is it will create a different role for every community user created.

I found some documentation that I should update to Communities Settings and update the Number of customer roles to 1. enter image description here

I also updated the profile being used and unchecked the Show Company Name as Community Role field

I also updated the communities member administration's Community role to default, but everytime my trigger creates a user it will create a role = Account Name + 'customer user' .What I want is for all community users, they will only have 1 role

This is my Trigger Code

    @future
public static void createPortalUser(Set<Id> setAccounts) {

    Set<String> setUCIAsFID = new Set<String>();
    Map<String, Account> mapAccount = new Map<String, Account>();
    Set<String> setExistingUser = new Set<String>();
    Map<Id, Id> mapContacts = new Map<Id, Id>();

    List<User> listPhysicianDashbrdUsr = new List<User>();

    List<Account> accounts = [Select Id, PC_Roche_UCI_ID__c, Type, PatientConnect__PC_First_Name__c,
                              PatientConnect__PC_Last_Name__c, PatientConnect__PC_Email__c from Account where Id in :setAccounts];
    for(Account newAcct : accounts) {
        if (newAcct.PC_Roche_UCI_ID__c != null && newAcct.Type == 'Doctor') {
            setUCIAsFID.add(newAcct.PC_Roche_UCI_ID__c);
            mapAccount.put(newAcct.PC_Roche_UCI_ID__c, newAcct);
        } // if (newAcct.PC_Roche_UCI_ID__c != null) {          
    } // for(Account newAcct : accounts) {  

    for (Contact c : [Select Id, AccountId from Contact where AccountId in :setAccounts]) {
        mapContacts.put(c.AccountId, c.Id);
    } // for (Contact c : [Select Id, AccountId fro

    system.debug('## setUCIAsFID ## ' + setUCIAsFID);
    if(!setUCIAsFID.isEmpty()) {

        for (User u : [Select id from User where FederationIdentifier in :setUCIAsFID]) {
            setExistingUser.add(u.FederationIdentifier);
        }

        Profile p = [select id from Profile where name = 'Physician Dashboard Login User'];
        Map<String, Id> roleMap = new Map<String, Id>();

        for(UserRole role: [select Id, DeveloperName from UserRole]){
            roleMap.put(role.DeveloperName, role.Id);
        }

        //UserRole portalRole = [Select Id, name From UserRole where name = 'SSO AU Customer User'];

        system.debug('## portalRole ## ' + p.id + ' ' + roleMap.get('SSOAUCustomerUser'));

        //mapContacts = createPortalContact(setUCIAsFID, mapAccount);
        system.debug('## mapContacts ## ' + mapContacts);
        for (String fid : setUCIAsFID) {
            system.debug('## fid ## ' + fid);
            if (setExistingUser.contains(fid) == false) {                    

                User u = new User(//alias = mapAccount.get(fid).PatientConnect__PC_First_Name__c.Substring(0,1) + mapAccount.get(fid).PatientConnect__PC_Last_Name__c.Substring(0,4), 
                    alias = 'test123',
                    email=mapAccount.get(fid).PatientConnect__PC_Email__c,
                    emailencodingkey='UTF-8', 
                    firstName=mapAccount.get(fid).PatientConnect__PC_First_Name__c, 
                    lastname=mapAccount.get(fid).PatientConnect__PC_Last_Name__c, 
                    languagelocalekey='en_US',
                    localesidkey='en_US', 
                    profileid = p.id,
                    timezonesidkey = 'Australia/Brisbane',
                    FederationIdentifier = fid,
                    //UserType = 'CustomerSuccess',
                    contactId = mapContacts.get(mapAccount.get(fid).Id), //con.id,
                    //UserRoleId = roleMap.get('AU_Physician'),
                    PC_Roche_Affiliate__c = PC_Roche_Constants.AFFILIATE_AU,
                    username= System.now().millisecond() + fid +'@gmail.com');
                    listPhysicianDashbrdUsr.add(u);

            }   // if (setExistingUser.contains(fid) == false) {             
        } // for (String fid : setUCIAsFID) {

        system.debug('## listPhysicianDashbrdUsr ## ' + listPhysicianDashbrdUsr);
        if(!listPhysicianDashbrdUsr.isEmpty()) {
            Database.insert(listPhysicianDashbrdUsr);
        } // if(!listPhysicianDashbrdUsr.isEmpty()) {

    }
}

Thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.