Skip to main content
Adding explicit example.
Source Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431

From the docs:

The runAs method doesn’t enforce user permissions or field-level permissions, only record sharing.

Apex code runs in system context so even though the user does not have delete permissions since the delete is done via apex it is allowed.

You could cause it buy using a flag to let the method know toinstead delete the recordrecords twice, and then attempt to delete it again? Not pretty but worksthe second deletion will cause an error:

static testMethod void testDmlException_Delete()
{
    List<Case> records = SObjectFactory.create(Case.sObjectType, RECORD_COUNT);
    delete records;
    
    DmlException expectedException;
    Test.startTest();
        try { delete records; }
        catch (DmlException dmx) { expectedException = dmx; }
    Test.stopTest();
    
    system.assertEquals(RECORD_COUNT, [SELECT count() FROM Case]);
    system.assertNotEquals(null, expectedException);
}

From the docs:

The runAs method doesn’t enforce user permissions or field-level permissions, only record sharing.

Apex code runs in system context so even though the user does not have delete permissions since the delete is done via apex it is allowed.

You could cause it buy using a flag to let the method know to delete the record and then attempt to delete it again? Not pretty but works

From the docs:

The runAs method doesn’t enforce user permissions or field-level permissions, only record sharing.

Apex code runs in system context so even though the user does not have delete permissions since the delete is done via apex it is allowed.

You could instead delete the records twice, and the second deletion will cause an error:

static testMethod void testDmlException_Delete()
{
    List<Case> records = SObjectFactory.create(Case.sObjectType, RECORD_COUNT);
    delete records;
    
    DmlException expectedException;
    Test.startTest();
        try { delete records; }
        catch (DmlException dmx) { expectedException = dmx; }
    Test.stopTest();
    
    system.assertEquals(RECORD_COUNT, [SELECT count() FROM Case]);
    system.assertNotEquals(null, expectedException);
}
added 142 characters in body
Source Link
Eric
  • 54.4k
  • 11
  • 104
  • 196

From the docs:

The runAs method doesn’t enforce user permissions or field-level permissions, only record sharing.

Apex code runs in system context so even though the user does not have delete permissions since the delete is done via apex it is allowed.

You could cause it buy using a flag to let the method know to delete the record and then attempt to delete it again? Not pretty but works

From the docs:

The runAs method doesn’t enforce user permissions or field-level permissions, only record sharing.

Apex code runs in system context so even though the user does not have delete permissions since the delete is done via apex it is allowed

From the docs:

The runAs method doesn’t enforce user permissions or field-level permissions, only record sharing.

Apex code runs in system context so even though the user does not have delete permissions since the delete is done via apex it is allowed.

You could cause it buy using a flag to let the method know to delete the record and then attempt to delete it again? Not pretty but works

Streamlining link
Source Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431

From the docsdocs:

The runAs method doesn’t enforce user permissions or field-level permissions, only record sharing.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm

Apex code runs in system context so even though the user does not have delete permissions since the delete is done via apex it is allowed

From the docs:

The runAs method doesn’t enforce user permissions or field-level permissions, only record sharing.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm

Apex code runs in system context so even though the user does not have delete permissions since the delete is done via apex it is allowed

From the docs:

The runAs method doesn’t enforce user permissions or field-level permissions, only record sharing.

Apex code runs in system context so even though the user does not have delete permissions since the delete is done via apex it is allowed

Source Link
Eric
  • 54.4k
  • 11
  • 104
  • 196
Loading