0

We have a shop portal where the Account will be created in Salesforce whenever a user shops in that portal. When the user logs in for the first time to our community, partner user should be created for the concerned Partner account created in first place. Now when the user logs in for the first time, they are getting this error

We can’t log you in because of the following error.

ERROR_CREATING_USER: Id not specified in an update call

I have my Registration handler as below

global class AutocreatedRegHandler1552501767420 implements Auth.RegistrationHandler{
    global boolean canCreateUser(Auth.UserData data) {
        return true;
    }




    global User createUser(Id portalId, Auth.UserData data){

        if(!canCreateUser(data)) {
            //Returning null or throwing an exception fails the SSO flow
            return null;
        }
        list<Account> Acclist = [SELECT ID FROM Account where Email__c =:data.email AND Account_Id__c = :data.username];
        Account partnerAccount = New Account();
        if(Acclist == Null){

            partnerAccount.Name = data.firstname;
            partnerAccount.email__c = data.email;
            partnerAccount.Account_Id__c = data.username;
            partnerAccount.Account_Status__c = 'True';
            partnerAccount.Account_Active_Status__c = 'Active';
            partnerAccount.Level__c = 'New  Account';
            partnerAccount.Is_Free_Account = True;
            partnerAccount.IsPartner = true;
            Database.insert(partnerAccount);
        }



        update partnerAccount;

        List<Profile> profiles = [SELECT Id FROM Profile WHERE Name='Custom Account'];

        Profile profile = profiles.isEmpty() ? null : profiles[0];

        if(profile==null)
            throw new RegHandlerException('Could not find the profile');

        List<User> users = [SELECT Id,Email,Username,FirstName,LastName,Alias,CommunityNickname,ProfileId,
                            ContactId,LocaleSidKey,LanguageLocaleKey,TimeZoneSidKey,EmailEncodingKey FROM User WHERE  Email = :data.Email and IsActive=true]; // Standard: to exclude Guest user (etc?)Profile.UserType = 'CspLitePortal' and Name='Carolina Ruiz Medina'];//  
        User user = users.isEmpty() ? null : users[0];

        if(user==null)
        {
            List<String> emailComponents = data.email.split('@');
            String nickname = data.attributeMap.get('display_name');
            Contact ct = new Contact(
                LastName = data.LastName,
                AccountId =  partnerAccount.id);
            upsert ct;

            user = new User(
                Email = data.email,
                Username = data.email,
                Alias = emailComponents[0].left(8),
                CommunityNickname = nickname,
                ProfileId = profile.Id,
                FirstName = data.firstName,
                LastName = data.lastName,
                LocaleSidKey = 'en_US',
                LanguageLocaleKey = 'en_US',
                TimeZoneSidKey = 'Europe/London',
                EmailEncodingKey  = 'ISO-8859-1',
                contactId = ct.Id

            );

        }
        return user;
    }

    global void updateUser(Id userId, Id portalId, Auth.UserData data){
        User u = new User(Id=userId);
        //TODO: Customize the username. Must be 80 characters or less.
        u.username = data.email;
        u.email = data.email;
        u.lastName = data.lastName;
        u.firstName = data.firstName;
        String alias = data.username;
        //Alias must be 8 characters or less
        if(alias.length() > 8) {
            alias = alias.substring(0, 8);
        }
        u.alias = alias;
        //update(u);

    }


    class RegHandlerException extends Exception {}
}

Can anyone guide me how to overcome this issue. Thanks

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.