• Sumanta Mukherjee 32
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 3
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Hi all,

I am trying to login into Salesforce Customer Community using Facebook as Auth Provider. 

Issue faced: I am able to sign in to Salesforce community using facebook authentication, however I am not able to fetch the facebook user details such as firstname,lastname,email dynamically using the Registration Handler Class by  using data.firstname,data.lastname,data.email . Currently i am using static values for the fields like firstname,lastname,email 

Expected : Getting dynamic values from facebook in the code below where static values are provided.

Code for Registration Handler that i am using as below.

global class FacebookHandler implements Auth.RegistrationHandler{ 
    global User createUser(Id portalId, Auth.UserData data){
    system.debug('hahahahaha' +portalId);
      User result;
      try {
        //default account to collect Community Members
        Account a = [ SELECT Id
                      FROM Account
                      WHERE name='MyAllergy'
                      LIMIT 1
                  ];
      
       //default profile
      Profile p = [ SELECT Id
                     FROM Profile
                    WHERE name='Custom Customer Community Plus Login User'
                     LIMIT 1
                   ];
       //contact
       Contact c   = new Contact(
        accountId = a.Id,
         email     = 'prateek.sharma@salesforce.com',
         firstName = data.firstName,
         lastName = data.lastname

     //    lastName  = 'testr'
       );     
       insert c;
      //get the alias as username
      String alias = this.getAlias(data.username);
       //user
       User u = new User(
         username          = alias + '@community.com',                                    
         email             = 'prateek.sharma@salesforce.com',
        lastName          = 'testtr',
         firstName         = data.firstName,
        alias             = alias,
         languagelocalekey = 'en_US',
         localesidkey      = 'en_GB',
         emailEncodingKey  = 'UTF-8',
         timeZoneSidKey    = 'GMT',
        profileId         = p.Id,
         contactId         = c.Id
       );
       //binding
       result = u;
     }
     catch(Exception pEx) {
      //re-throwing exception to allow tracing the error from URL
      throw new HandlerException(
         pEx.getStackTraceString()
       );
     }
    return result;           
   }
   //custom exception
   global class HandlerException extends Exception{}
   global void updateUser(Id userId, Id portalId, Auth.UserData data){
        User u = new User(
       id        = userId,
      email     = 'prateek.sharma@salesforce.com',
       lastName  = 'tata',
     firstName = data.firstName,
     alias     = this.getAlias(data.username)
    );
    
     update u;
   }
   global String getAlias(String pUsername) {
     //generate random string (8)
     Blob blobKey           = crypto.generateAesKey(128);
     String key             = EncodingUtil.convertToHex(blobKey);
     String random_username = key.substring(0,8);
     //if not defined, create a random one
     String alias = pUsername != null ?
                     pUsername :
                     random_username;
     //check alias.length() > 8 and chop it
     alias = alias.length() > 8 ?
               alias.substring(0,8) :
               alias;
     return alias;
   }
}

Any help would be appreciated,

Thanks
Hi all,

I am trying to login into Salesforce Customer Community using Facebook as Auth Provider. 

Issue faced: I am able to sign in to Salesforce community using facebook authentication, however I am not able to fetch the facebook user details such as firstname,lastname,email dynamically using the Registration Handler Class by  using data.firstname,data.lastname,data.email . Currently i am using static values for the fields like firstname,lastname,email 

Expected : Getting dynamic values from facebook in the code below where static values are provided.

Code for Registration Handler that i am using as below.

global class FacebookHandler implements Auth.RegistrationHandler{ 
    global User createUser(Id portalId, Auth.UserData data){
    system.debug('hahahahaha' +portalId);
      User result;
      try {
        //default account to collect Community Members
        Account a = [ SELECT Id
                      FROM Account
                      WHERE name='MyAllergy'
                      LIMIT 1
                  ];
      
       //default profile
      Profile p = [ SELECT Id
                     FROM Profile
                    WHERE name='Custom Customer Community Plus Login User'
                     LIMIT 1
                   ];
       //contact
       Contact c   = new Contact(
        accountId = a.Id,
         email     = 'prateek.sharma@salesforce.com',
         firstName = data.firstName,
         lastName = data.lastname

     //    lastName  = 'testr'
       );     
       insert c;
      //get the alias as username
      String alias = this.getAlias(data.username);
       //user
       User u = new User(
         username          = alias + '@community.com',                                    
         email             = 'prateek.sharma@salesforce.com',
        lastName          = 'testtr',
         firstName         = data.firstName,
        alias             = alias,
         languagelocalekey = 'en_US',
         localesidkey      = 'en_GB',
         emailEncodingKey  = 'UTF-8',
         timeZoneSidKey    = 'GMT',
        profileId         = p.Id,
         contactId         = c.Id
       );
       //binding
       result = u;
     }
     catch(Exception pEx) {
      //re-throwing exception to allow tracing the error from URL
      throw new HandlerException(
         pEx.getStackTraceString()
       );
     }
    return result;           
   }
   //custom exception
   global class HandlerException extends Exception{}
   global void updateUser(Id userId, Id portalId, Auth.UserData data){
        User u = new User(
       id        = userId,
      email     = 'prateek.sharma@salesforce.com',
       lastName  = 'tata',
     firstName = data.firstName,
     alias     = this.getAlias(data.username)
    );
    
     update u;
   }
   global String getAlias(String pUsername) {
     //generate random string (8)
     Blob blobKey           = crypto.generateAesKey(128);
     String key             = EncodingUtil.convertToHex(blobKey);
     String random_username = key.substring(0,8);
     //if not defined, create a random one
     String alias = pUsername != null ?
                     pUsername :
                     random_username;
     //check alias.length() > 8 and chop it
     alias = alias.length() > 8 ?
               alias.substring(0,8) :
               alias;
     return alias;
   }
}

Any help would be appreciated,

Thanks