5

I have added a validation in Account Trigger to prevent the deletion of Account record if the status is other than Archived

for(Account objAccount: lstOldAccount){
    if(objAccount.Status__c != 'Archived'){
      objAccount.addError('<h1 style="color:red;">You can not delete an account with live status.</h1>', false);
    }
}

So When I try to delete an account with active status is show the error with html formatted

enter image description here

But when you go back to lighting Experience and try to delete the same record with active status

It shows the HTML code and escape=false doesn't work it seems

enter image description here

is it a Salesforce bug?

8

See the idea HTML tags in the error message are escaped even if escape = false is specified at sObject.addError() in Lightning UI and Salesforce1

Sounds like they aren't going to fix/change it.

This limitation has been documented at https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_field_addError.

From the linked addError(errorMsg, escape) docs:

escape
Type: Boolean
Indicates whether any HTML markup in the custom error message should be escaped (true) or not (false). This parameter is ignored in Lightning Experience and the Salesforce app and the HTML is always escaped. The escape parameter only applies in Salesforce Classic.

  • 2
    Ahh, I missed the documentation part somehow. Thanks – Ratan Paul Jul 19 '18 at 11:50
0

While Salesforce fixes the bug, you can use the below workaround.

I created a dummy trigger to see what request URL we get in classic and Salesforce.

trigger ProofTrigger on Proof__c (after insert, after update) {
    System.debug(System.URL.getCurrentRequestUrl().toExternalForm());

}

When in classic the Debug I get

https://pranaydev.my.salesforce.com/a0Y0E00000217jk/e

whereas in Lightning the debug I get is

https://pranaydev.my.salesforce.com/aura?r=67&ui-force-components-controllers-recordGlobalValueProvider.RecordGvp.saveRecord=1

So you can write an if-else to use the following feature to your advantage.

for(Account objAccount: lstOldAccount){

    if(objAccount.Status__c != 'Archived'){ // This is classic
      if(!System.URL.getCurrentRequestUrl().toExternalForm().contains('aura')){
        objAccount.addError('<h1 style="color:red;">You can not delete an account with live status.</h1>', false);
      }else{ // This is lightning
        objAccount.addError('You can not delete an account with live status.');
      }

    }
}
  • Thanks, man for the solution, But I was looking why it is not working. – Ratan Paul Jul 19 '18 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.