0

My org is setting up social sign on with Google and I am running into an error I have never seen before: System.NullPointerException: Attempt to de-reference a null object

Does anyone know a solution to this issue?

Here is my class and test class for reference.

global class AutocreatedRegHandler1537799224416 implements Auth.RegistrationHandler{
//https://developer.salesforce.com/forums/ForumsMain?id=9060G0000005dc5QAA
global boolean canCreateUser(Auth.UserData data) {
  return false;
}

global User createUser(Id portalId, Auth.UserData data){    
    if(!canCreateUser(data)) {
    return null;
  }

  if(data.attributeMap.containsKey('sfdc_networkid')) {

    Account a = [SELECT Id FROM account WHERE name='Acme'];
    Contact c = new Contact();
    c.accountId = a.Id;
    c.email = data.email;
    c.firstName = data.firstName;
    c.lastName = data.lastName;
    insert(c);

    User u = new User();
    Profile p = [SELECT Id FROM profile WHERE name='Community User'];
    u.username = data.username + '@suyati.test.com ';
    u.email = data.email;
    u.lastName = data.lastName;
    u.firstName = data.firstName;
    String alias = data.username;

    if(alias.length() > 8) {
      alias = alias.substring(0, 8);
    }
    u.alias = alias;

    u.languagelocalekey = 'en_US';
    u.localesidkey = UserInfo.getLocale();
    u.emailEncodingKey = 'UTF-8';
    u.timeZoneSidKey = 'America/Los_Angeles';
    u.profileId = p.Id;
    u.contactId = c.Id;
    return u;
  } else {

    User u = new User();
    Profile p = [SELECT Id FROM profile WHERE name='Standard User'];

    u.username = data.username + '@myorg.com';
    u.email = data.email;
    u.lastName = data.lastName;
    u.firstName = data.firstName;
    String alias = data.username;

    if(alias.length() > 8) {
      alias = alias.substring(0, 8);
    }
    u.alias = alias;
    u.languagelocalekey = 'en_US';
    u.localesidkey = UserInfo.getLocale();
    u.emailEncodingKey = 'UTF-8';
    u.timeZoneSidKey = 'America/Los_Angeles';
    u.profileId = p.Id;
    return u;
  }
}

global void updateUser(Id userId, Id portalId, Auth.UserData data){
  User u = new User(id=userId);


  u.email = data.email;
  u.lastName = data.lastName;
  u.firstName = data.firstName;

  update(u);
}
}

And the test class

    @isTest
private class AutocreatedRegHandler1537799224416Test 
{
static testMethod void testCreateAndUpdateUser() 
{
AutocreatedRegHandler1537799224416  handler = new AutocreatedRegHandler1537799224416 ();

 Auth.UserData sampleData = new Auth.UserData('testId', 'testFirst', 'testLast', 
'testFirst testLast', 'testuser@example.org', 
null, 'testuserlong', 'en_US', 'google', 
null, new Map<String, String>{'language' => 'en_US'});
User u = handler.createuser(null, sampleData);
System.assertEquals('testuser@suyati.test.com', u.userName);
System.assertEquals('testuser@example.org', u.email);
System.assertEquals('testLast', u.lastName);
System.assertEquals('testFirst', u.firstName);
System.assertEquals('ttest', u.alias);
String uid = u.id;

sampleData = new Auth.UserData('testNewId', 'testNewFirst', 'testNewLast',
'testNewFirst testNewLast', 'testnewuser@example.org', null, 'testnewuserlong', 'en_US', 'facebook',
null, new Map<String, String>{});
update(u);

User updatedUser = [SELECT userName, email, firstName, lastName, alias FROM user WHERE id=:uid];
System.assertEquals('testnewuserlong@salesforce.com', updatedUser.userName);
System.assertEquals('testnewuser@example.org', updatedUser.email);
System.assertEquals('testNewLast', updatedUser.lastName);
System.assertEquals('testNewFirst', updatedUser.firstName);
System.assertEquals('testnewu', updatedUser.alias);
}
}
  • 1
    On which line is the NullPointerException thrown? This is a very common error (see numerous questions on SFSE), and is unlikely to be specific to SSO. – David Reed Oct 8 '18 at 19:25
  • Line 13 - System.assertEquals('testuser@suyati.test.com', u.userName); – Alexis Scott Oct 8 '18 at 19:26
1

So if you have a NullPointerException here:

User u = handler.createuser(null, sampleData);
System.assertEquals('testuser@suyati.test.com', u.userName);

on the assertion, there's exactly one possibility: u is null. So our next step is to look at where u comes from, your createUser() method, to determine why.

The answer turns out to be right at the top of your file:

global boolean canCreateUser(Auth.UserData data) {
  return false;
}

global User createUser(Id portalId, Auth.UserData data){    
    if(!canCreateUser(data)) {
    return null;
  }

Your creation class is hard-coded to return false for canCreateUser(), and hard-coded to return null when it cannot create a user.

I'm not sure what business logic you intend to implement for canCreateUser(), but it looks like you need to finish fleshing out this code before you can embark on tests - or at least remove the hard-coded false.

Edit - based on your comments, this is auto-generated code, rather than being copied from the Salesforce Developer Forum. This code is not complete; it is a skeleton that you need to flesh out and complete based on your business requirements. As written, the code is not usable or testable because it is hard-coded to fail.

  • I'm not a developer so I'm not really sure what to do. Our Google Admin set up a SAML with Salesforce and it generated that code and now our code coverage is all messed up so I am just trying to cover the code. – Alexis Scott Oct 9 '18 at 12:33
  • 1
    If you don't have a team member that knows or is willing to learn Apex, I would suggest that you contract with a developer. – David Reed Oct 9 '18 at 12:49
  • The code that you're using here is a skeleton. It needs to be fleshed out to work correctly. – David Reed Oct 9 '18 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.