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
ManuManu 

Getting contact through portal user not working

Hi all,

In our customer portal we have users created through "Enable Customer Users" option in contact. While saving user unfortunately I am getting an exception from our trigger that contact Id is null. I am facing this issue while creating user through contact. I guess something went wrong in latest release?

Any suggestions please?
Sameer Tyagi SFDCSameer Tyagi SFDC
Hi Manu, 

When you click on manage external user on any contact and select "Enable Customer user", then you are redirected to user creation page autometically then you select Customer Community in Licence picklist and "Customer community user" in profile picklist. Correct?

Do you get this error when you click on Save button on User Creation page?
and Please confirm if you have written any tirgger on user object?


Regards,
Sameer Tyagi 
http://mirketa.com/
 
ManuManu
Yes..you are right...

I am getting error when I click on save button of the user with customer community user license.

I have trigger on user object which is getting the contact Id before insert. The contact Id is getting null and throwing null pointer exception?

Any suggestoins please?
Sameer Tyagi SFDCSameer Tyagi SFDC
Hi Manu, 

So its clear that issue with your trigger code. Can you please share sample code of your trigger. so that I can provide you solution quickly. 

Sameer
ManuManu

trigger EmailChangeNotification on User (after Update, before insert,before update) {
String userid,profileName,profileId,contactid;
if(trigger.isBefore){
for(User u2 : trigger.new){
contactid = u2.ContactId;

/*I am getting contactId as null here...any reason why?
}
}
}
Sameer Tyagi SFDCSameer Tyagi SFDC
Hi Manuj, 

I have checked your trigger, which is working fine. It does not show any error during customer user creation and showing the contactid in debug log also. 
I pasted same code which you sent me. 

Sameer 
Sameer Tyagi SFDCSameer Tyagi SFDC
Its seems like any other code snippet showing error. 

Sameer
ManuManu
Can you quickly tell me to which data center your organization belongs to ? Mine is Cs17

Also can you confirm that you are getting contactID before insert?

My org is recently updated with winter 15..may be that could be the problem?

Thanks for you help indeed..
 
Sameer Tyagi SFDCSameer Tyagi SFDC
Its ap1

and my developer org also updated with winter 15. Thats not the issue. 

Issue with Code . I am damn sure. 

Sameer 
ManuManu
Here is the full code..The exact exception is at line 25 in below code..let me know if you find any.

if(Trigger.IsBefore){
            Integer i=0;
            String userid,profileName,profileId,contactid;
            User u;
            Contact portalUser;
            
            for(User u2 : trigger.new){
                system.debug('===========u2 =========='+u2);
                userid = Trigger.isUpdate ? u2.Id : null;
                profileId = u2.ProfileId;
                contactid = u2.ContactId;
            }
            system.debug('===========contactid =========='+contactid); //Getting Null here
            try{
               if(Trigger.isInsert && contactid  != null)
                    portalUser = [Select  Account.Member_Type__c, Account.recordType.name from Contact where Id =:contactid LIMIT 1];
            }catch(Exception e){}
            
                
            
            for(User u1 : trigger.new){
                if(Trigger.isInsert){
                    if(!Test.isRunningTest()){            
                        System.debug('=======portalUser======'+portalUser);
                        if(portalUser.Account.recordType.name == 'Member'){
                             if((portalUser.Account.Member_Type__c == 'A') && !(profileName == 'A Member Checker' ||  profileName == 'A Member Editor')){
                                //Some Contion
                            }
                        }                
                    }   
                }
            }
        }
ManjunathManjunath
Hi,

instead of this "if(trigger.isBefore){" try putting this "if(trigger.isBefore && trigger.isInsert) {"
Check if any other trigger is updating the User record(It is just a guess).

Regards,
Manjunath C S
Sameer Tyagi SFDCSameer Tyagi SFDC
Hi Manu, 

Requesting you for the whole code from line one. So that I will be able to know where your bracket is closed and where is start of bracket. 

Sameer
ManuManu

Error Message : Apex trigger EmailChangeNotification caused an unexpected exception, contact your administrator: EmailChangeNotification: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.EmailChangeNotification: line 26, column 1

Full Code

trigger EmailChangeNotification on User (after Update, before insert,before update) {
    if(Trigger.IsBefore){
        Integer i=0;
        String userid,profileName,profileId,contactid;
        User u;
        Contact portalUser;
        
        for(User u2 : trigger.new){
            system.debug('===========u2 =========='+u2);
            userid = Trigger.isUpdate ? u2.Id : null;
            profileId = u2.ProfileId;
            contactid = u2.ContactId;
        }
        system.debug('===========contactid =========='+contactid); //Getting Null here
        try{
           if(Trigger.isInsert && contactid  != null)
                portalUser = [Select  Account.Member_Type__c, Account.recordType.name from Contact where Id =:contactid LIMIT 1];
        }catch(Exception e){}
        
            
        
        for(User u1 : trigger.new){
            if(Trigger.isInsert){
                if(!Test.isRunningTest()){            
                    System.debug('=======portalUser======'+portalUser);
                    if(portalUser.Account.recordType.name == 'Member'){
                         if((portalUser.Account.Member_Type__c == 'A') && !(profileName == 'A Member Checker' ||  profileName == 'A Member Editor')){
                            //Some Contion
                        }
                    }                
                }   
            }
        }
    }
}
Sameer Tyagi SFDCSameer Tyagi SFDC
Hi Manu, 

I do not see any error with your code, working fine. 
here is the debug. 
 
32.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
03:23:17.293 (293002744)|EXECUTION_STARTED
03:23:17.293 (293039581)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
03:23:17.294 (294337876)|CODE_UNIT_STARTED|[EXTERNAL]|01q90000000TGLF|mytestsam.EmailChangeNotification on User trigger event BeforeInsert for [new]
03:23:17.300 (300430259)|SYSTEM_METHOD_ENTRY|[8]|LIST<User>.iterator()
03:23:17.300 (300467933)|ENTERING_MANAGED_PKG|
03:23:17.300 (300591549)|SYSTEM_METHOD_EXIT|[8]|LIST<User>.iterator()
03:23:17.300 (300616816)|SYSTEM_METHOD_ENTRY|[8]|system.ListIterator.hasNext()
03:23:17.300 (300642667)|SYSTEM_METHOD_EXIT|[8]|system.ListIterator.hasNext()
03:23:17.300 (300701481)|SYSTEM_METHOD_ENTRY|[9]|String.valueOf(Object)
03:23:17.300 (300708939)|ENTERING_MANAGED_PKG|
03:23:17.304 (304127290)|SYSTEM_METHOD_EXIT|[9]|String.valueOf(Object)
03:23:17.304 (304167653)|SYSTEM_METHOD_ENTRY|[9]|System.debug(ANY)
03:23:17.304 (304175991)|ENTERING_MANAGED_PKG|
03:23:17.304 (304182398)|USER_DEBUG|[9]|DEBUG|===========u2 ==========User:{UserRoleId=null, LastModifiedDate=null, LocaleSidKey=en_US, LastName=Frank, PortalRole=null, IsActive=true, MobilePhone=(650) 867-7686, ReceivesInfoEmails=false, SmallPhotoUrl=null, OfflineTrialExpirationDate=null, LanguageLocaleKey=en_US, StayInTouchNote=null, Country=null, Email=efrank@genepoint.com, CreatedById=null, ManagerId=null, Street=345 Shoreline Park
Mountain View, CA 94043
USA, ForecastEnabled=false, FederationIdentifier=null, IsPortalEnabled=false, DefaultGroupNotificationFrequency=null, SystemModstamp=null, FirstName=Edna, Latitude=null, LastReferencedDate=null, TimeZoneSidKey=America/Los_Angeles, Title=VP, Technology, City=null, StayInTouchSubject=null, CreatedDate=null, ContactId=0039000001JKpSIAA1, SenderEmail=null, CommunityNickname=efrank, JigsawImportLimitOverride=300, BadgeText=null, LastPasswordChangeDate=null, DigestFrequency=null, StayInTouchSignature=null, Extension=null, UserType=CspLitePortal, IsPortalSelfRegistered=false, ProfileId=00e90000001avhxAAA, CallCenterId=null, LastViewedDate=null, AccountId=null, IsBadged=false, Id=null, Longitude=null, ReceivesAdminInfoEmails=false, Phone=(650) 867-3450, EmployeeNumber=null, Signature=null, LastLoginDate=null, Department=Technology, DelegatedApproverId=null, LastModifiedById=null, Fax=(650) 867-9895, CompanyName=null, SenderName=null, OfflinePdaTrialExpirationDate=null, EmailEncodingKey=ISO-8859-1, PostalCode=null, Alias=efran, State=null, Division=null, Username=efrank@genepoint.com, FullPhotoUrl=null, AboutMe=null}
03:23:17.304 (304194396)|SYSTEM_METHOD_EXIT|[9]|System.debug(ANY)
03:23:17.304 (304251238)|SYSTEM_METHOD_ENTRY|[8]|system.ListIterator.hasNext()
03:23:17.304 (304265805)|SYSTEM_METHOD_EXIT|[8]|system.ListIterator.hasNext()
03:23:17.304 (304280643)|SYSTEM_METHOD_ENTRY|[14]|System.debug(ANY)
03:23:17.304 (304285923)|ENTERING_MANAGED_PKG|
03:23:17.304 (304303394)|USER_DEBUG|[14]|DEBUG|===========contactid ==========0039000001JKpSIAA1
03:23:17.304 (304308844)|SYSTEM_METHOD_EXIT|[14]|System.debug(ANY)
03:23:17.305 (305148541)|SOQL_EXECUTE_BEGIN|[17]|Aggregations:0|select Account.mytestsam__Member_Type__c, Account.recordType.name from Contact where Id = :tmpVar1 limit 1
03:23:17.344 (344350299)|SOQL_EXECUTE_END|[17]|Rows:1
03:23:17.344 (344617870)|SYSTEM_METHOD_ENTRY|[22]|LIST<User>.iterator()
03:23:17.344 (344630671)|ENTERING_MANAGED_PKG|
03:23:17.344 (344671980)|SYSTEM_METHOD_EXIT|[22]|LIST<User>.iterator()
03:23:17.344 (344692186)|SYSTEM_METHOD_ENTRY|[22]|system.ListIterator.hasNext()
03:23:17.344 (344719812)|SYSTEM_METHOD_EXIT|[22]|system.ListIterator.hasNext()
03:23:17.344 (344878242)|SYSTEM_METHOD_ENTRY|[24]|system.Test.isRunningTest()
03:23:17.344 (344936945)|SYSTEM_METHOD_EXIT|[24]|system.Test.isRunningTest()
03:23:17.344 (344983757)|SYSTEM_METHOD_ENTRY|[25]|String.valueOf(Object)
03:23:17.344 (344993511)|ENTERING_MANAGED_PKG|
03:23:17.345 (345068971)|SYSTEM_METHOD_EXIT|[25]|String.valueOf(Object)
03:23:17.345 (345105829)|SYSTEM_METHOD_ENTRY|[25]|System.debug(ANY)
03:23:17.345 (345116266)|ENTERING_MANAGED_PKG|
03:23:17.345 (345124598)|USER_DEBUG|[25]|DEBUG|=======portalUser======Contact:{AccountId=0019000001BGwPPAA1, Id=0039000001JKpSIAA1}
03:23:17.345 (345134875)|SYSTEM_METHOD_EXIT|[25]|System.debug(ANY)
03:23:17.345 (345229256)|SYSTEM_METHOD_ENTRY|[22]|system.ListIterator.hasNext()
03:23:17.345 (345248106)|SYSTEM_METHOD_EXIT|[22]|system.ListIterator.hasNext()
03:23:17.276 (345275283)|CUMULATIVE_LIMIT_USAGE
03:23:17.276|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

03:23:17.276|LIMIT_USAGE_FOR_NS|mytestsam|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

03:23:17.276|CUMULATIVE_LIMIT_USAGE_END

03:23:17.345 (345359707)|CODE_UNIT_FINISHED|mytestsam.EmailChangeNotification on User trigger event BeforeInsert for [new]
03:23:17.708 (708291857)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:NetworkMember:new
03:23:17.708 (708317067)|VALIDATION_RULE|NonNegativeReputationPoints|NonNegativeReputationPoints
03:23:17.708 (708439740)|VALIDATION_FORMULA|ReputationPoints<0|ReputationPoints=1.0
03:23:17.708 (708451598)|VALIDATION_PASS
03:23:17.708 (708472969)|CODE_UNIT_FINISHED|Validation:NetworkMember:new
03:23:17.913 (913695124)|CODE_UNIT_FINISHED|TRIGGERS
03:23:17.913 (913715531)|EXECUTION_FINISHED

 
ManuManu
Strange...I am still getting the error...and here is my debug log'
 
Sandbox

Apex script unhandled trigger exception by user/organization: 005b0000000FbnO/00Dg00000006Vfg
Source organization: 00Db0000000Z6zo (null)
EmailChangeNotification: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.EmailChangeNotification: line 26, column 1

Debug Log:
28.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
07:13:49.560 (560921385)|EXECUTION_STARTED
07:13:49.561 (561004353)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
07:13:49.691 (691596080)|CODE_UNIT_STARTED|[EXTERNAL]|01qg0000000Cpnr|EmailChangeNotification on User trigger event BeforeInsert for [new]
07:13:49.694 (694724213)|SYSTEM_METHOD_ENTRY|[8]|List<User>.iterator()
07:13:49.695 (695075190)|SYSTEM_METHOD_EXIT|[8]|List<User>.iterator()
07:13:49.695 (695124552)|SYSTEM_METHOD_ENTRY|[8]|system.ListIterator.hasNext()
07:13:49.695 (695173856)|SYSTEM_METHOD_EXIT|[8]|system.ListIterator.hasNext()
07:13:49.695 (695297428)|SYSTEM_METHOD_ENTRY|[9]|String.valueOf(Object)
07:13:49.702 (702824669)|SYSTEM_METHOD_EXIT|[9]|String.valueOf(Object)
07:13:49.702 (702906643)|SYSTEM_METHOD_ENTRY|[9]|System.debug(ANY)
07:13:49.702 (702923521)|USER_DEBUG|[9]|DEBUG|===========u2 ==========User:{Token_Expiration_Time__c=null, UserRoleId=null, PortalRole=null, OfflineTrialExpirationDate=null, Country=null, Email=ccb-cc@ceptes.com, CreatedById=null, Edit_Default_As_Of_Date_Count__c=null, Expire_Edit_Action_History_Access__c=null, SystemModstamp=null, DefaultGroupNotificationFrequency=null, Latitude=null, LastReferencedDate=null, TimeZoneSidKey=Europe/Dublin, Title=null, StayInTouchSubject=null, CreatedDate=null, DigestFrequency=null, Extension=null, IsPortalSelfRegistered=false, ProfileId=00eg0000000M4E1AAK, LastViewedDate=null, Action_Histories_Edit_Count__c=null, Longitude=null, EmployeeNumber=null, Phone=null, Signature=null, LastLoginDate=null, Department=null, LastModifiedById=null, CompanyName=null, SenderName=null, OfflinePdaTrialExpirationDate=null, State=null, Alias=car, Username=ccb-cc@ceptes.com, Division=null, AboutMe=null, LastModifiedDate=null, LocaleSidKey=en_IE_EURO, LastName=CAR Member Editor, IsActive=true, MobilePhone=null, ReceivesInfoEmails=false, SmallPhotoUrl=null, LanguageLocaleKey=en_US, StayInTouchNote=null, Security_Question__c=null, ManagerId=null, Street=null, ForecastEnabled=false, FederationIdentifier=null, IsPortalEnabled=false, FirstName=null, Security_Answer__c=null, City=null, ContactId=null, SenderEmail=null, CommunityNickname=ccb-cc, LastPasswordChangeDate=null, StayInTouchSignature=null, UserType=CspLitePortal, CurrencyIsoCode=USD, DefaultCurrencyIsoCode=USD, CallCenterId=null, AccountId=null, Id=null, ReceivesAdminInfoEmails=false, Token__c=null, DelegatedApproverId=null, Fax=null, EmailEncodingKey=ISO-8859-1, PostalCode=null, FullPhotoUrl=null}
07:13:49.702 (702958172)|SYSTEM_METHOD_EXIT|[9]|System.debug(ANY)
07:13:49.703 (703157641)|SYSTEM_METHOD_ENTRY|[8]|system.ListIterator.hasNext()
07:13:49.703 (703195007)|SYSTEM_METHOD_EXIT|[8]|system.ListIterator.hasNext()
07:13:49.703 (703258825)|SYSTEM_METHOD_ENTRY|[14]|System.debug(ANY)
07:13:49.703 (703297714)|USER_DEBUG|[14]|DEBUG|===========contactid ==========null
07:13:49.703 (703310807)|SYSTEM_METHOD_EXIT|[14]|System.debug(ANY)
07:13:49.703 (703380753)|SYSTEM_METHOD_ENTRY|[22]|List<User>.iterator()
07:13:49.703 (703417945)|SYSTEM_METHOD_EXIT|[22]|List<User>.iterator()
07:13:49.703 (703442154)|SYSTEM_METHOD_ENTRY|[22]|system.ListIterator.hasNext()
07:13:49.703 (703472163)|SYSTEM_METHOD_EXIT|[22]|system.ListIterator.hasNext()
07:13:49.703 (703815800)|SYSTEM_METHOD_ENTRY|[24]|system.Test.isRunningTest()
07:13:49.703 (703890406)|SYSTEM_METHOD_EXIT|[24]|system.Test.isRunningTest()
07:13:49.703 (703947629)|SYSTEM_METHOD_ENTRY|[25]|String.valueOf(Object)
07:13:49.703 (703998536)|SYSTEM_METHOD_EXIT|[25]|String.valueOf(Object)
07:13:49.704 (704039474)|SYSTEM_METHOD_ENTRY|[25]|System.debug(ANY)
07:13:49.704 (704054469)|USER_DEBUG|[25]|DEBUG|=======portalUser======null
07:13:49.704 (704064951)|SYSTEM_METHOD_EXIT|[25]|System.debug(ANY)
07:13:49.704 (704144218)|EXCEPTION_THROWN|[26]|System.NullPointerException: Attempt to de-reference a null object
07:13:49.706 (706051978)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Trigger.EmailChangeNotification: line 26, column 1
07:13:49.706 (706080054)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Trigger.EmailChangeNotification: line 26, column 1
07:13:49.448 (706147558)|CUMULATIVE_LIMIT_USAGE
07:13:49.448|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

07:13:49.448|CUMULATIVE_LIMIT_USAGE_END

07:13:49.706 (706247764)|CODE_UNIT_FINISHED|EmailChangeNotification on User trigger event BeforeInsert for [new]
ManuManu
I think it is the salesforce release issue...My sandbox is updated to spring '15...and thats why I am getting this issue. ap1 is still not updated to spring 15...
Sameer Tyagi SFDCSameer Tyagi SFDC
Manu, 

I am doing it like this. 

User-added image

then redirected to User creation screen on click of Enable Customer User. 
User-added image

Now you are getting error on click on save. am I correct?

Sameer
ManuManu
Yes....you are correct...i am doing the sameUser-added image
Sameer Tyagi SFDCSameer Tyagi SFDC
My org is also updated with winter 15 
User-added image
ManuManu
User-added image

Its sring 15My org is updated to spring 15 already and ap1 is still not updated to spring 15 I guess...
Sameer Tyagi SFDCSameer Tyagi SFDC
May be its due to new release. 
Sorry I could not fix your issue. 
I will try this same code when my org will be updated with Spring 15

Sameer Tyagi 
ManuManu
Even I guess its due to new release. I will try to raise a case with salesforce and will update you if I get any information on this.

However thanks for your great support.