runAs Method

Using the runAs Method in Salesforce | Apex Developer Guide

By and large, all Apex code runs in framework mode, where the authorizations and record sharing of the current client are not considered. The framework technique runAs empowers you to compose test strategies that change the client set to a current client or another client so the client's record sharing is authorized. The runAs strategy doesn't authorize client consents or field-level authorizations, just record sharing

You can utilize runAs just in test strategies. The first framework setting is begun again after all runAs test strategies are complete. 

The runAs technique overlooks client permit limits. You can make new clients with runAs regardless of whether your association has no extra client licenses. 

Note: Each call to runAs means something negative for the complete number of DML explanations given simultaneously. 

In the accompanying model, another test client is made, then, at that point, code is run as that client, with that client's record sharing access: 

private class TestRunAs { 
    public static testMethod void testRunAs() { 
        //Setup test information 
        //Create an interesting UserName 
        String uniqueUsName = 'sample' + + ''; 
        //This code runs as the framework client 
        Profile getprofile = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        Client currentUser = new User(Alias = 'testus', Email='[email protected]', 
        EmailEncodingKey='UTF-8', LastName='sample', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId = getprofile.Id, 
        System.runAs(currentUser) { 
            /The accompanying code runs as client 'u' 
            System.debug( UserInfo.getUserName()); 
            System.debug( UserInfo.getProfileId()); 

dont miss out iconDon't forget to check out: Mass/Bulk Insert Custom MetaData Records through CSV | Salesforce Developer Guide

You can settle more than one runAs technique. For instance: 

private class TestRunAs2 { 
    public static testMethod void test2() { 
        Profile getprofile = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        Client currentUser2 = new User(Alias = 'testus2', Email='[email protected]', 
        EmailEncodingKey='UTF-8', LastName='sample 2', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId = getprofile.Id, 
        TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]'); 
        System.runAs(currentUser2) { 
            /The accompanying code runs as client currentUser2. 
            //The accompanying code runs as client currentUser3. 
            System.runAs(currentUser3) { 
                System.debug( UserInfo.getProfileId()); 
            //Any extra code here would run as client currentUser2. 

Different Uses of runAs 

You can moreover use the runAs method to perform mixed DML assignments in your test by encasing the DML activities inside the runAs block. Thusly, you sidestep the blended DML mistake that is generally returned when embedding or refreshing arrangement protests along with other sObjects.

There is one more over-burden of the runAs technique (runAs(System.Version)) that accepts a bundle adaptation as a contention. This technique causes the code of a particular adaptation of an oversaw bundle to be utilized. For data on utilizing the runAs strategy and indicating a bundle rendition setting, see Testing Behavior in Package Versions.

dont miss out iconCheck out another amazing blog by Rajesh here: Learn All About Process Builder in Salesforce and Its Features

Testing Behavior in Package Versions

At the point when you change the conduct in an Apex class or trigger for various bundle variants, test that your code runs true to form in the distinctive bundle adaptations. You can compose test techniques that change the bundle variant setting to an alternate bundle form by utilizing the framework strategy runAs. You can just utilize runAs in a test technique.


Popular Salesforce Blogs