+ Start a Discussion
sales4cesales4ce 

Trigger to control User registration--Help required!

Hi,

 

I am trying to customize the site registration page, which comes out of the box.

when we put in our username,force.com appends customerportalid.na6.force.com to it. we could also login by just entering the username and no need to enter appended stuff.

 

But, what i am thinking is would it be possible to make username same as email, like the one we use for CRM login.

How can i accomplish this?

I tried writing a trigger on user object for after insert event and it quite did not work. Is there anything that i am missing.

Any ideas/help on this is highly appreciated.

 

trigger MatchUserNamewithEmail on User (before Insert) 
{
   
    if(Trigger.Isbefore && Trigger.IsInsert)
    {
        for(user u : Trigger.New)
        {
            if(u.userType.endsWith('PowerCustomerSuccess'))
            {
                if(u.Username!=u.email)
                {
                    u.Username=u.email;
                   
                }
            }
         }
     }
    
   

}

Thanks,

sales4ce

Best Answer chosen by Admin (Salesforce Developers) 
RyanGuestRyanGuest

How about just doing this when you create the user in Apex?

 

Here's what I did:

 

Change this:

 

 

        User u = new User();
        u.Username = username;
        u.Email = email;
        u.CommunityNickname = communityNickname;
        
        String accountId = PORTAL_ACCOUNT_ID;

        // lastName is a required field on user, but if it isn't specified, we'll default it to the username
        String userId = Site.createPortalUser(u, accountId, password);

 

 

to this:

 

 

        User u = new User();
        u.Username = email;
        u.Email = email;
        u.CommunityNickname = communityNickname;
        
        String accountId = PORTAL_ACCOUNT_ID;

        // lastName is a required field on user, but if it isn't specified, we'll default it to the username
        String userId = Site.createPortalUser(u, accountId, password);

 

Then delete the username parameter from the visualforce page.

All Answers

RyanGuestRyanGuest

How about just doing this when you create the user in Apex?

 

Here's what I did:

 

Change this:

 

 

        User u = new User();
        u.Username = username;
        u.Email = email;
        u.CommunityNickname = communityNickname;
        
        String accountId = PORTAL_ACCOUNT_ID;

        // lastName is a required field on user, but if it isn't specified, we'll default it to the username
        String userId = Site.createPortalUser(u, accountId, password);

 

 

to this:

 

 

        User u = new User();
        u.Username = email;
        u.Email = email;
        u.CommunityNickname = communityNickname;
        
        String accountId = PORTAL_ACCOUNT_ID;

        // lastName is a required field on user, but if it isn't specified, we'll default it to the username
        String userId = Site.createPortalUser(u, accountId, password);

 

Then delete the username parameter from the visualforce page.

This was selected as the best answer
sales4cesales4ce

Thanks Ryan, it worked fine.

I did not have to write a trigger. I was thinking way too complex.

 

Thanks,

sales4ce