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
Prav PPrav P 

Registration handler- Salesforce to Okta

Hi ,

Im trying to authenticate Salesforce -Okta through Auth Provider and to support that I have created the Registration Handler which creates and updates the users from SF to okta. However, Im receiving the below error in line1, though i have implemented the both createUser and UpdateUser method. Any help please

"Class Oktahandler must implement the method: User Auth.RegistrationHandler.createUser(Id, Auth.UserData)"


global class Oktahandler implements Auth.RegistrationHandler {

    private static final String ORG_SUFFIX = '.test';
    private static final String DEFAULT_ACCOUNTNAME = 'Test';
    private static final String DEFAULT_EXTERNAL_USER_PROFILE = 'Customer Community User';
    private static final String DEFAULT_INTERNAL_USER_PROFILE = 'Standard User';
    private static final String EXTERNAL_APPS_LOGIN_LICENSE_NAME = 'Community User';
    private static final String SALESFORCE_LICENSE_NAME = 'Salesforce';
    private static final String TZSID = [SELECT timezonesidkey from User where profile.name = 'System administrator' LIMIT 1].timezonesidkey;

global Boolean canCreateUser(Auth.UserData data) {
      //Check whether we want to allow creation of a user?
      return true;
  }
 
  global User createUser(Id portalId, Auth.UserData data){
      if(!canCreateUser(data)) {
        return null;
      }
      system.debug(data);
      if(data.attributeMap.containsKey('sfdc_networkid')) {
list<user> ulist =[select id,email from user where email=:data.email];
          if(ulist.size()>0){
             return ulist[0];
          }
        
      Account a=[select id from account where id='0017c00000oh8M2AAI'];
      contact c=new contact();
      c.accountid=a.id;
      c.email=data.email;
      c.FirstName=data.attributeMap.get('FirstName');
      c.LastName=data.attributeMap.get('LastName');
      insert c;
         
      user u=new user();
     Profile p = [SELECT Id FROM profile WHERE name='Customer Community User'];
   u.email = data.email;
    u.lastName = data.attributeMap.get('LastName');
    u.firstName = data.attributeMap.get('FirstName');
      u.Username=data.attributeMap.get('UserName');
        String alias = data.username;
    //Alias must be 8 characters or less
    if (alias.length() > 8) {
        alias = alias.substring(0, 8);
    }
       u.alias = alias;
    u.languagelocalekey = 'en_US';
    u.localesidkey = 'en_US';
    u.emailEncodingKey = 'UTF-8';
    u.timeZoneSidKey = 'Australia/Brisbane';
    u.profileId = p.Id;
      u.ContactId=c.id;
    return u;
      }
      else{
          return null;
      }
   }
 
  global void updateUser(Id userId, Id portalId, Auth.UserData data){
      User u = new User(id=userId);
       u.lastName = data.attributeMap.get('LastName');
      //.. update fields if required.
     // update u;
  }
}
AbhinavAbhinav (Salesforce Developers) 
Hi Prav,

I tried same code on dev console . I am not getting any error as you have implemented both method  from Auth.RegistrationHandler interface.

Try refreshing  and then save it.

Thanks!
Prav PPrav P
Thanks Abhinav for quick response. There are no errors now, but when I tried logging with community user still I see fee issues. Could you pls guide me

 
AbhinavAbhinav (Salesforce Developers) 
Same issue??
Prav PPrav P
Hi ,

I did changes to the registraion handler apex class and added the methods, but still im getting the below error in line1. Could you please correct me  if im doing anything wrong here?

Class OktaSSORegHandler must implement the method: User Auth.RegistrationHandler.createUser(Id, Auth.UserData)

global class OktaSSORegHandler implements Auth.RegistrationHandler{
   
    static final string account = 'Test';
    static final string community_profile = 'Customer Community User';
    static final string standard_profile  = 'Standard User';
   
    void prepareUserData(Auth.UserData data, User u)
    {  
        String name, firstName, lastName, username, alias, email;

        //TODO: Customize the user attributes. Also check that the username doesn't
        //already exist and possibly ensure there are enough org licenses to
        //create a user. Must be 80 characters or less
       
        // Print the attributes list retrieved by the Authentication Provider
        system.debug('Email: ' + data.email);
        system.debug('First Name: ' + data.firstName);
        system.debug('Last Name: ' + data.lastName);
        for(string key : data.attributeMap.keySet())
        {
            system.debug('key: ' + key + ' value: ' + data.attributeMap.get(key));
        }
   
        // Initialize the attributes essential for creating a new user with dummy values
        // in case they will not be provided by the Auth Provider
        firstName = 'change-me';
        lastName  = 'change-me';
        email     = 'change@me.com';
       
        if(data.email != null && data.email != '')
            email = data.email;
     
        if(data.firstName != null && data.firstName != '')
            firstName = data.firstName;
       
        if(data.LastName != null && data.lastName != '')
            lastName = data.lastName;

        if(data.attributeMap.containsKey('full_name'))
            name = data.attributeMap.get('full_name');
     
       if(data.attributeMap.containsKey('name'))
           name = data.attributeMap.get('name');

        if(firstName == 'change-me' && name != '')
            firstName = name.substringBefore(' ');

       if(lastName == 'change-me' && name.substringAfter(' ') != '')
           lastName = name.substringAfter(' ');
           
     
  // Generate a random username
       Integer rand = Math.round(Math.random()*100000000);
       username = firstName + '.' + rand + '@salesforce.com';        
  
        alias = firstName;
        //Alias must be 8 characters or less
       if(alias.length() > 8)
            alias = alias.substring(0, 8);  
           
        u.username = username;
        u.email = email;
        u.lastName = lastName;
        u.firstName = firstName;
       u.alias = alias;
        u.languagelocalekey = UserInfo.getLocale();
        u.localesidkey = UserInfo.getLocale();
        u.emailEncodingKey = 'UTF-8';
        u.timeZoneSidKey = 'Australia';
   }
   
 // Creates a Standard salesforce or a community user
 public User createUser(Id portalId, Auth.UserData data){
   
    User u = new User();
   
    prepareUserData(data, u);
   
    //TODO: Customize the username, profile and account name
 
    if(data.attributeMap.containsKey('sfdc_networkid')) {
       //We have a community id, so create a user with community access
       
       //TODO: Customize the Account
       Account a;
       List<Account> accounts = [SELECT Id FROM account WHERE name= 'Test'];
       if(accounts.isEmpty())
       {
           a = new Account(name = 'Test1');
           insert(a);
       }else
           a = accounts[0];
     
       Contact c = new Contact();
       c.accountId = a.Id;
 
       c.firstName = u.firstName;
       c.lastName  = u.lastName;
       insert(c);
       
       //TODO: Customize the profile
       Profile p = [SELECT Id FROM profile WHERE name= 'Customer Community User'];    
       u.profileId = p.Id;
       u.contactId = c.Id;
       return u;
    } else {
       //TODO: Customize the profile
       Profile p = [SELECT Id FROM profile WHERE name= 'Standard User'];
       u.profileId = p.Id;
       return u;
    }
}
   
// Updates the user's first and last name
global void updateUser(Id userId, Id portalId, Auth.UserData data){
 
  User u = new User(id=userId);
     
  if(data.email != null && data.email != '')
      u.email = data.email;
     
  if(data.lastName != null && data.lastName != '')
    u.lastName = data.lastName;
 
  if(data.firstName != null && data.firstName != '')
    u.firstName = data.firstName;
 
  update(u);
}

}