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
Sanchivan SivadasanSanchivan Sivadasan 

Customer Portal: Disabling User and Setting Account.IsCustomerPortal to false in testmethod

Hi there,

 

In the testmethod, I am trying to disable a Customer Portal User which I created within the testmethod by doing the following:

 

// Disable the peteSamprass portal use
peteSamprassUser.IsPortalEnabled = False;
peteSamprassUser.IsActive = False;
update peteSamprassUser;
	        
peteSamprassUser = [SELECT Id, AccountId, ContactId, IsActive, IsPortalEnabled FROM User WHERE Id =:peteSamprassUser.Id];
System.assertEquals(peteSamprassUser.IsPortalEnabled, False) System.assertEquals(peteSamprassUser.IsActive, False);

 For some reason the IsPortalEnabled reverts back to True even though I am clearly updating the field. As a result the Assert Statement for IsPortalEnabled fails. I also noticed that IsActive is propery set to False, so we don't have an issue with IsActive field.

 

Then I also tried disabling the Account record for Customer Portal as follows:

 

account101.IsCustomerPortal = False;
update account101;
	        
account101 = [SELECT Id, Name, IsCustomerPortal FROM Account WHERE Id =:account101.Id];
System.assertEquals(account101.IsCustomerPortal, False);

 This statement also fails.  Please let me know what I am doing wrong. Thanks.

 

Sanch S.

Sanchivan SivadasanSanchivan Sivadasan

Here is the full testMethod:

 

static testMethod void myUnitTest() {
    	
    	User runningUser = UserTestData.createTestUser();
    	insert runningUser;
    	System.AssertNotEquals(runningUser.Id, Null);
    	
    	System.runAs(runningUser) {
    	
	        // Create a Parent Account with the name 100
	        Account account100 = AccountTestData.createAccount('100', Null);
	        insert account100;
	        System.AssertNotEquals(account100.Id, Null);
	        
	        // Create a Child Account for the Account 100 with the name 100.1
	        Account account101 = AccountTestData.createAccount('100.1', account100.Id);
	        insert account101;
	        System.AssertNotEquals(account101.Id, Null);
	        
	        // Create a Child Account for the Account 100 with the name 100.2
	        Account account102 = AccountTestData.createAccount('100.2', account100.Id);
	        insert account102;
	        System.AssertNotEquals(account102.Id, Null);
	        
	        // Create a Contact for Account 100
	        Contact andreAgassi = ContactTestData.createContact(account100.Id, 'Andre', 'Agassi');
	        andreAgassi.Title = 'Cluster Owner';
	        insert andreAgassi;
	        System.AssertNotEquals(andreAgassi.Id, Null);
	        
	        // Create a Contact for Account 100.1
	        Contact peteSamprass = ContactTestData.createContact(account101.Id, 'Mr. Pete', 'Samprass');
	        peteSamprass.Title = 'Station Manager';
	        insert peteSamprass;
	        System.AssertNotEquals(peteSamprass.Id, Null);
	        
	        // Create a Contact for Account 100.2
	        Contact johnSmith = ContactTestData.createContact(account102.Id, 'John', 'Smith');
	        johnSmith.Title = 'Station Manager';
	        insert johnSmith;
	        System.AssertNotEquals(johnSmith.Id, Null);
	        
	        Test.startTest();
		        // Create User for peteSamprass
		        User peteSamprassUser = UserTestData.createTestCustomerPortalUser(peteSamprass.Id);
		        System.AssertNotEquals(peteSamprassUser.Id, Null);
		                
		        // Create User for andreAgassi
		        User andreAgassiUser = UserTestData.createTestCustomerPortalUser(andreAgassi.Id);
		        System.AssertNotEquals(andreAgassiUser.Id, Null);
		    Test.stopTest();    
	        
	        account101 = [SELECT Id, Name, IsCustomerPortal FROM Account WHERE Id =:account101.Id];
	        System.AssertEquals(account101.IsCustomerPortal, True);
	        
	        // Verify that andreAgassi has a AccountShare record for account101
	        List<AccountShare> andreAgassiToAccount101Share = [SELECT Id, UserOrGroupId, AccountId, AccountAccessLevel FROM AccountShare WHERE UserOrGroupId =:andreAgassiUser.Id AND AccountId =:account101.Id];
	        System.assertNotEquals(andreAgassiToAccount101Share[0].Id, Null);
	        
	        // Verify that peteSamprass does not have access to account100
	        List<AccountShare> peteSamprassToAccount100Share = [SELECT Id, UserOrGroupId, AccountId, AccountAccessLevel FROM AccountShare WHERE UserOrGroupId =:peteSamprassUser.Id AND AccountId =:account100.Id];
	        System.assertEquals(peteSamprassToAccount100Share.size(), 0);
	        
	        // Disable the peteSamprass portal user
	        peteSamprassUser.IsPortalEnabled = False;
	        peteSamprassUser.IsActive = False;
	        update peteSamprassUser;
	        
	        peteSamprassUser = [SELECT Id, AccountId, ContactId, IsActive, IsPortalEnabled FROM User WHERE Id =:peteSamprassUser.Id];
	        System.assertEquals(peteSamprassUser.IsActive, False);
	        System.assertEquals(peteSamprassUser.IsPortalEnabled, False);
	        
	        // Verify that andreAgassi still has access to account101
	        andreAgassiToAccount101Share = [SELECT Id, UserOrGroupId, AccountId, AccountAccessLevel FROM AccountShare WHERE UserOrGroupId =:andreAgassiUser.Id AND AccountId =:account101.Id];
	        System.assertNotEquals(andreAgassiToAccount101Share[0].Id, Null);
	        
	        // Set account101.IsCustomerPortal to false
	        account101.IsCustomerPortal = False;
	        update account101;
	        
	        account101 = [SELECT Id, Name, IsCustomerPortal FROM Account WHERE Id =:account101.Id];
	        System.assertEquals(account101.IsCustomerPortal, False);
	        
	        // Verify that andreAgassi no longer have access to account101
	        
    	}

 

Sanchivan SivadasanSanchivan Sivadasan

Does anyone have a solution for this? Thanks.

 

Sanch S.