+1 vote
101 views
by (163k points)

There is a requirement where I set a password from Apex(code), whenever the community user is inserted. When I try logging in with a user inserted from the community portal with the password set in APEX, I am not able to log in.

    public without sharing class CreateUserHandler {
    @future
    public static void createUserFromContact(Set<Id> conIds)
    {
        List<User> userList = new List<User>();
        Set<String> userNames = new Set<String>();
        List<Profile> profileid =[Select id from Profile where name='Copeia Patient Login user'];
        system.debug('contsize'+conIds.size());
        for(Contact con : [Select Id,  Email,firstname,lastname,Birthdate,Phone,MailingCountry,MailingStreet,MailingCity,MailingState,MailingPostalCode from Contact where Id IN: conIds limit 50000]){ 
            //conIds is a list of contact which is being sent from other class. 
            User uObj = new User();
            uObj.Email = con.Email;
            String s='@rahul.com';
            uObj.username=con.email;
            uObj.firstname=con.firstname;
            uObj.lastname = con.lastname;
            uObj.Date_of_Birth__c = con.Birthdate;
            uObj.Alias = con.firstname.substring(0,3)+con.lastname.substring(0,1);
            uObj.ProfileId =profileid[0].Id;
            uObj.IsActive = true; 
            uObj.TimeZoneSidKey = 'GMT';
            uObj.Phone=con.Phone;
            uObj.LanguageLocaleKey = 'en_US';
            uObj.EmailEncodingKey = 'UTF-8';
            uObj.LocaleSidKey = 'en_US';
            uObj.ContactId = con.Id;
            uObj.Street=con.MailingStreet;
            uObj.Address1__c=con.MailingStreet;
            uObj.City=con.MailingCity;
            uObj.State=con.MailingState;
            uObj.PostalCode=con.MailingPostalCode;
            uObj.Country=con.MailingCountry;
            userNames.add(con.email);
            userList.add(uObj);
            //System.debug('userList'+userList);
            system.debug('hellllll'+uObj.Username);
        }
        insert userList;
        System.debug('userList'+userList);
        for (User u : [SELECT Id, Date_of_Birth__c,Firstname FROM User WHERE UserName IN :userNames]){
            
            System.debug('birthday'+u.Date_of_Birth__c);
            Date birthdate=u.Date_of_Birth__c;
            String d = String.valueOf(u.Date_of_Birth__c);
            system.setPassword(u.Id, d);
        }
    }
}

System.setPassword used to work properly before, and all of a sudden it stopped working and is not logging in. (COMMUNITIES)

1 Answer

+1 vote
by (163k points)
 
Best answer
null
Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
...