Sometime we need to get Default New User License of Customer Portal in the apex code, After doing some google search, I found that there is no direct method or way to do it in the apex.
To get the Default New User License of customer portal in apex code, we need either customer portal name or site name. Below are the steps for Getting “Default New User License” of customer portal in apex code. Here you go :-
Getting Default New User License of Customer Portal in Code
STEP 1 : You need MetadataService.cls Apex class in your salesforce org.(I would suggest keep only required methods of your need only, as this class has huge lines of code).
STEP 2 : Now you need to set Remote Site Setting. Setting up remote site setting is shown in below figures.
STEP 3 : After setting up MetaDataService apex class and remote site setting we are ready to read “Default New User License” of customer portal in apex code. Given below is the code snippet for this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public String getDefaultNewUserLicense(){ String siteName = 'YOUR_SITE_NAME'; // Your Site Name String portalName = ''; String defaultUserLicenseName = ''; String profileFullName = ''; // Setup Connection MetadataService.MetadataPort service = new MetadataService.MetadataPort(); service.SessionHeader = new MetadataService.SessionHeader_element(); service.SessionHeader.sessionId = UserInfo.getSessionId(); // Reading Site Metadata to get portal name MetadataService.IReadResult resCustomSite = service.readMetaData('CustomSite', new String[] { siteName} ); MetadataService.Metadata[] infoCustomSite = resCustomSite.getRecords(); for(MetadataService.Metadata md : infoCustomSite){ MetadataService.CustomSite siteInfo = (MetadataService.CustomSite) md; portalName = siteInfo.portal; } // Reading Portal Metadata to get profile name of portal MetadataService.IReadResult res = service.readMetaData('Portal', new String[]{portalName}); MetadataService.Metadata[] info = res.getRecords(); for(MetadataService.Metadata md : info){ MetadataService.Portal pt = (MetadataService.Portal) md; profileFullName = pt.selfRegUserDefaultProfile; } // Reading Profile Metadata to get Default New User License MetadataService.IReadResult resProfile = service.readMetaData('Profile', new String[]{profileFullName}); MetadataService.Metadata[] infoProfile = resProfile.getRecords(); for(MetadataService.Metadata md : infoProfile){ MetadataService.Profile pr = (MetadataService.Profile) md; defaultUserLicenseName = pr.userLicense; } System.debug('### Defualt New User License = '+defaultUserLicenseName); return defaultUserLicenseName; } |