Friday, 21 June 2024

Create a Community User in the Test Class

Create a Community User in the Test Class 


The process for creating portal test users is more complex than creating standard test users. It may involve additional fields, and security settings.


Step 1: Create Portal Owner User


private static User createPortalAccountOwner() {
//Create a Role Role is mandatory to create a portal user
UserRole portalRole = new UserRole(DeveloperName = 'MyCustomRole', Name = 'My Role', PortalType='None' );
insert portalRole;
System.debug('portalRole is ' + portalRole);
Profile sysProfile = [Select Id from Profile where name = 'System Administrator'];
User portalOwnerusr = new User(
Firstname='Demo',
Lastname='PortalOwner',
UserRoleId = portalRole.Id,
Email='demo.portalowner@test.com',
Username = 'demo.portalOwner' + System.currentTimeMillis() + '@test.com',
ProfileId = sysProfile.Id,
Alias = 'demalss',
EmailEncodingKey='UTF-8',
LanguageLocaleKey='en_US',
LocaleSidKey='en_US',
TimeZoneSidKey = 'America/Los_Angeles'
);
Database.insert(portalOwnerusr);
return portalOwnerusr;
}


Step 2: Create a Community User


private static User createCommunityUser(User portalOwner) {
User communityUser ;
System.runAs ( portalOwner ) {
//Create account
Account acc = new Account(
Name = 'acc',
OwnerId = portalOwner.Id
);
Database.insert(acc);
//Create contact
Contact con = new Contact(
FirstName = 'conFirst',
Lastname = 'conLast',
AccountId = acc.Id,
Email = 'con' + System.currentTimeMillis() + '@test.com'
);
Database.insert(con);
communityUser = new User(
FirstName = 'DemoFirst',
LastName = 'DemoLast',
Email = 'Democommunityuser@test.com',
ProfileId = [SELECT Id FROM Profile WHERE Name = 'Community User Plus User'].Id,
Username = 'Democommunity.user.' + System.currentTimeMillis() + '@test.com',
Title = 'Title',
Alias = 'DemoAls',
TimeZoneSidKey = 'America/Los_Angeles',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US',
LocaleSidKey = 'en_US',
ContactId = con.id
);
Database.insert(communityUser);
}
re



Step 3: call below code from the test method: 

User portalOwner = createPortalAccountOwner();
User portaluser= createCommunityUser(portalOwner);

Thursday, 18 April 2024

Salesforce Import and export Files/Attachments

 Salesforce Import and export Files/Attachments


Step 1:  Export files using the below path:  Setup ==> Data ==> Data Export

Select options: 

        Export File Encoding: UTF-8

        Include images, documents, and attachments: Checked

        Include Salesforce Files and Salesforce CRM Content document versions: Checked

        Replace carriage returns with spaces: Checked

 Select Specific objects to get their attachments: Here, I opted Account






Step 2: Download the zip file from Salesforce

Salesforce sends an email once the files are ready to download

 



Step 3: Unzip the downloaded file and navigate to the ContentVersion folder 
             and ContentVersion.csv. add the extension (.pdf, .xlsx, .jpeg, etc.) to any file.

 

Step 4: Create a CSV file with below Header values: 




Step 5: Go to Data loader settings and check both (Read/Write) UTF-8 settings as below:




Step 6: Select the Insert option, search for the content version object, and load the CSV file to upload the attachment.: 



Step 6: Once Upload is completed, Data Loader creates success and error files respectively:


        















Monday, 18 March 2024

Salesforce Code Analyzer Command


Salesforce Code Analyzer Command example/Sample


Installing Salesforce code analyzer:

Link: https://forcedotcom.github.io/sfdx-scanner/en/
v3.x/getting-started/install/

Go to VScode -- > New terminal - -> Enter below command
(It installs analyser)
Project location > sfdx plugins:install @salesforce/sfdx-scanner

To check whether the code analyzer is installed or not:

(It checks whether the code analyzer is installed or not)
Project location > sfdx plugins -->

To generate a report Go: to the command prompt

and enter the below command :

(Can generate different reports like .xml, and .csv as well)
Project location > sfdx scanner:run --target "**/default/**"
--outfile results.html


GENERIC COMMAND for Code Scanner

$sfdx scanner:run --target './force-app/main/default/' --projectdir
'./force-app/main/default' --format csv --outfile=codereviewGeneric.csv

CPD Command
$sfdx scanner:run --engine cpd --target './force-app/main/default/'
--projectdir './force-app/main/default' --format csv --outfile=codereviewCPD.csv

DFA Command - Graph Engine (this one takes time to generate report)
$sfdx scanner:run:dfa --target './force-app/main/default/' --projectdir
'./force-app/main/default' --format csv --outfile=codereviewDFA.csv 




reference from : 

https://developer.salesforce.com/docs/platform/salesforce-code-analyzer/guide/dfa.html

https://developer.salesforce.com/docs/platform/salesforce-code-analyzer/guide/cpd-engine.html


Salesforce Code Analyzer Command example/Sample

Wednesday, 31 January 2024

Get sObject Name and Permission Set Assignment SOQL in the Salesforce

 Salesforce: Get sObject Name and Permission Set Assignment


SOQL:
SELECT Parent.Name, Parent.PermissionsTransferAnyLead,SobjectType ,PermissionsRead, PermissionsCreate,PermissionsDelete, PermissionsModifyAllRecords, PermissionsViewAllRecords
FROM ObjectPermissions     
WHERE  ParentId in (
            SELECT id
            FROM PermissionSet
            WHERE name IN ( 'Master_Edit_Access'))