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
Renuka ChavanRenuka Chavan 

Apex Test class for AutocreatedConfigSelfReg class

Hi All,
I am  trying to write an Apex Test class for AutocreatedConfigSelfReg 
In the test class I want to pass the real values to below method defined in AutocreatedConfigSelfReg 
global Id createUser(Id accountId, Id profileId, Map<SObjectField, String> registrationAttributes, String password) 
Type: Map<Schema.sObjectField,String>
A map of attributes that the registering user entered on the self-registration page. 
Cam somebody guide me how can I pass the below fields and associated values to the createUser method 
FirstName
LastName
Email
AbhishekAbhishek (Salesforce Developers) 
https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test?rq=1

Try the ways as suggested above to write a test class.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
Renuka ChavanRenuka Chavan
Thanks for your time.But my question is how do I pass the map data type actual parameter values to the method that I want to test using apex test class.
for example I want to test below method defined in AutocreatedConfigSelfReg
  global Id createUser(Id accountId, Id profileId, Map<SObjectField, String> registrationAttributes, String password) 

    id testUid = AutocreatedConfigSelfReg.createUser('0010v00000ZC3ubAAD','00e0c000002Khy2AAC',{['FirstName':'Jack'],['LastName':'Frost'],['Email':'Jack.frost@atos.com']},'Test123$$');

Above giving loads of syntax error.Any pointer to the Apex syntax would be of great help.
 
Renuka ChavanRenuka Chavan
Hi,
I have managed to fix all my syntax errors in the Apex Test class using below syntax.

Map<SObjectField, String> Regfields = new Map<SObjectField, String>();
    
Schema.DescribeFieldResult fn = user.firstname.getDescribe();
Schema.sObjectField fname = fn.getSObjectField();
    
Schema.DescribeFieldResult ln = user.lastname.getDescribe();
Schema.sObjectField lname = ln.getSObjectField();

Schema.DescribeFieldResult em = user.email.getDescribe();
Schema.sObjectField uemail = em.getSObjectField();
   
    Regfields.put(fname,'Jack');
    Regfields.put(lname,'frost');
    Regfields.put(uemail,'jack.frost@atos.com');
    

 id testUid = new AutocreatedConfigSelfRegMetaPack().createUser('0010v00000ZC3ubAAD','00e0c000002Khy2AAC',Regfields,'Test123$$');
Paul DanielPaul Daniel
Thanks for sharing, Renuka. This got me above 75% code coverage but when deploying to production I'm getting the following deployment error:
"System.UnexpectedException: Salesforce System Error: 419877735-162688 (-1761313995) (-1761313995) Stack Trace: Class.System.Site.validatePassword"

Did you get something similar? If so how did you resolve? Seems like the deployed test class gets caught up on the password validation.
Shelley WardwellShelley Wardwell
I'm really struggling with this. I'm very new to Apex and not able to find much at all about the test class for the autocreatedconfigselfreg class.  Could anyone post here the full contents of a working test class.  I have spent days following links to different resources but would so much appreciate a full copy posted here for my limited newbie skills.  I'm trying to check to see if a contact with the same email exists and if so just create the user with the existing contact id - if the user record does not already exist. If the user already exists I would like to display a more user friendly message that indicates there is already a login and to reset the password if needed.  Modifying the code was fairly easy but I'm really lost on test cases.  Thanks for any help you can provide.