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
SamuelDeRyckeSamuelDeRycke 

REST Service testing , portal user problem

Scenario:

 

I have a REST service which returns the account of a portal user, and the subaccounts to that account. It works fine, but I need test code coverag, a problem likely not unfamiliar to many around these parts.

 

I know how to test REST services, well easy ones at least, but have a problem with generating required data, and relations between it in apex. My rest service uses the accoutnId field on a user, and then querries the account and its subaccounts, I would prefer to not rewrite my perfect working service because I don't know how to get test coverage, so I ask your help.

 

The accountId field on a user is not writeable, yet always filled in when creating a customer portal user based on a contact, i think this uses the Site.CreatePortal user, possible i'm wrong in this ? I tried doing it like that in code, and ran into the following error:

 

System.UnexpectedException: Method not yet implemented: createPortalUser(User, String)

 

Any advise or insights are highly appreciated !

 

@IsTest()
  	global static void testGET(){
  		
  		//add data
  		
  			//account 
	  			Account pa = new Account(name='test parent acc');
	  			insert pa;
  			
  			//subaccount
  				Account sa = new Account(name='test sub acc',ParentId =pa.id);
	  			insert sa;
  	
  			//user
  			
  				Profile p = [SELECT Id FROM Profile limit 1];
  				User u = new User(lastname='De Rycke',
  						  Alias='Samuel',
  						  email = 'zert@zer.com',
  						  username= 'zert@zer.com', 
  						  profileId= p.id,
  						  emailencodingkey='UTF-8',
  						  languagelocalekey='en_US',
  						  localesidkey='en_US',
  						  timezonesidkey='America/Los_Angeles');
  				
  				ID userid = Site.CreatePortalUser( u, pa.id );
  				
  		Test.startTest();
  		//rest service
  			RestRequest req = new RestRequest(); 
	  		req.addParameter('userid',userid);
			RestResponse res = new RestResponse();
	 
			req.requestURI = '/Account/MyAccounts/';  
			req.httpMethod = 'GET';
			RestContext.request = req;
			RestContext.response = res;
			List<Account> result = MyAccountsRestService.getMyAccounts();
	  		
  		//assert
  	
  			System.assertEquals(2, result.size());
  			System.assertEquals('test parent acc', result.get(0).name);
  			System.assertEquals('test sub acc', result.get(1).name);
  		Test.stopTest();
  	}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SamuelDeRyckeSamuelDeRycke

I found the solution myself, instead of deleting the post i consider it may be better to just leave it as reference for others who might run into the problem.

 

//contact
  			Contact c = new Contact(accountId=pa.id,lastname='test');
  			insert c;
  			
  			//user
  			
  				Profile p = [select p.id, p.name from profile p where p.UserLicense.name like '%customer portal%' ].get(0);  //obviously, this code will be errorous when customer portal is not enabled in the organisation!
  				User u = new User(lastname='De Rycke',
  						  Alias='sdry',
  						  email = 'zert@zer.com',
  						  username= 'zert@zer.com', 
  						  profileId= p.id, 
  						  contactId=  c.id,
  						  emailencodingkey='UTF-8',
  						  languagelocalekey='en_US',
  						  localesidkey='en_US',
  						  timezonesidkey='America/Los_Angeles');
  				insert u;	  
  				//having a customer portal profile, this will become a portal user !

 Turns out you don't need to use the Site.CreatePortaluser method, if you create a user with a customer portal profile, it will become a portal user, and and contactId and accountId fields will be populated.

All Answers

SamuelDeRyckeSamuelDeRycke

I found the solution myself, instead of deleting the post i consider it may be better to just leave it as reference for others who might run into the problem.

 

//contact
  			Contact c = new Contact(accountId=pa.id,lastname='test');
  			insert c;
  			
  			//user
  			
  				Profile p = [select p.id, p.name from profile p where p.UserLicense.name like '%customer portal%' ].get(0);  //obviously, this code will be errorous when customer portal is not enabled in the organisation!
  				User u = new User(lastname='De Rycke',
  						  Alias='sdry',
  						  email = 'zert@zer.com',
  						  username= 'zert@zer.com', 
  						  profileId= p.id, 
  						  contactId=  c.id,
  						  emailencodingkey='UTF-8',
  						  languagelocalekey='en_US',
  						  localesidkey='en_US',
  						  timezonesidkey='America/Los_Angeles');
  				insert u;	  
  				//having a customer portal profile, this will become a portal user !

 Turns out you don't need to use the Site.CreatePortaluser method, if you create a user with a customer portal profile, it will become a portal user, and and contactId and accountId fields will be populated.

This was selected as the best answer
Hank HolidayHank Holiday
Thanks for posting... this is super helpful!