0

I have a test class like this:

    Account acc = new Account(name='testAcc');
    Insert acc;

    Contact con = new Contact(LastName='TestContact', AccountId=acc.Id);
    Insert con;

    Id p = [SELECT Id FROM Profile WHERE Name = 'myCustomProfile' Limit 1].Id;
    

    User runas = new User(Alias = 'testUser', Email='[email protected]', 
    EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='en_US', LocaleSidKey='en_US', 
    Profileid = p, Country='United States', IsActive=true, BU_Name__c = 'US', CompanyName = 'testCompany',
    ContactId = con.Id, TimeZoneSidKey='America/Los_Angeles', Username='[email protected]');

    Insert runas;
    
    System.runAs(runas){
        Test.startTest();
        ......
        Test.stopTest();
    }  

However, I do not have 'myCustomProfile' in my scratch org if I run this:

sfdx force:org:create --setdefaultusername --definitionfile config/project-scratch-def.json --wait 10 --durationdays 7

With this project-scratch-def.json file:

{
  "orgName": "myOrgName",
  "edition": "Enterprise",
  "hasSampleData": "true"
}

This caused the unit test of my unlocked package in the scratch org to fail. I guess there is a step that I might have missed. Or is there anything I can do to prevent this?

1 Answer 1

3

You can include the profile in your Unlocked Package; it works the same as including any other type of metadata, just retrieve it and commit it to your repo.

sfdx force:source:retrieve -u sandbox-or-prod -m Profile:myCustomProfile

P.S. If you're still using Profiles like this, it might be time to look in to migrating to Permission Sets instead. They are far easier to use in unit tests and even have a SFDX CLI command to assign Permission Sets easily to your Scratch Org users.

P.P.S. An Unlocked Package doesn't actually create a Scratch Org. It's a temporary org that's used only for the upload/validation process, and is not tied to any existing created sandbox. This process is nearly identical to the Managed Package upload process, except we're allowed to specify dependencies manually, while Classic Managed Packages do this automatically.

3
  • Hi sfdcfox, thanks for the clarification and answer. The reason for the above codes in the test class is to have a user with account Id. Therefore I create an account and a contact before creating a user. Out of prudence I would not add the Profile to my unlocked package as there might be some incoherence between our production org and sandbox. Commented Jun 24, 2020 at 6:15
  • I wonder what I can do with permission set instead of profile in this case. Is there some document explaining this? Thanks once again. Commented Jun 24, 2020 at 6:16
  • @ZhuangPaulus I'm not sure what the docs say about that, if anything. If you're creating a community/portal/etc user, just query for the default profile of that license type. It should be created automatically if you've enabled that Feature Dependency in your Scratch Org Configuration File for the Unlocked Package.
    – sfdcfox
    Commented Jun 24, 2020 at 7:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .