function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Yamini BathulaYamini Bathula 

New line in String variable Adderror method in Apex Trigger

Hi Guys,

We have a trigger on Lead Conversion which checks for some fields (if htey are blank) and display a error message in Convert Popup(in Lightining). That is working fine. But the only issue is the new line is not working in the UI.

Here is my trigger : I have added a new line every time I append the error string. but it doesn't show up in the conversion popup in Lightning. 
trigger MandatoryFieldsLeadConversion on Lead (before Update) 
{

           // Select Id,ParentAccount__c,FirstName,Email,Phone,Company,Country__c,Industry_Ipay__c,Trading_Name__c,Street,State,City,PostalCode,Sales_Channel__c,Avg_Trans_Amount__c,Trans_Per_Month__c,Max_Single_Pay_Amt__c,Director_1_First_Name__c,Director_1_Last_Name__c,Director_Email__c,Direct_Debit_Requested__c,eCommerce__c,Video_Payment__c,Xero_Payment_Now__c,Xero_Direct_Debit__c
        
        String sBreak = '<br/>';    
        Boolean hasError = false;
        for (Lead l : Trigger.new)
         {
            if (Trigger.oldMap.get(l.id).isConverted == false && Trigger.newMap.get(l.id).isConverted == true) // to check only in Lead Coversion
             {  
                 String erstring = 'Please Enter Below Mandatory Fields before converting this Lead \n';
               //Lead l = [Select Id,ParentAccount__c,FirstName,Email,Phone,Company,Country__c,Industry_Ipay__c,Trading_Name__c,Street,State,City,PostalCode,Sales_Channel__c,Avg_Trans_Amount__c,Trans_Per_Month__c,Max_Single_Pay_Amt__c,Director_1_First_Name__c,Director_1_Last_Name__c,Director_Email__c,Direct_Debit_Requested__c,eCommerce__c,Video_Payment__c,Xero_Payment_Now__c,Xero_Direct_Debit__c From Lead Where Id in  :trigger.new[i]];
                 //Skip this check for V3 and IODM Leads
               if((l.ParentAccount__c != '0019000001YHuxWAAT') && (l.ParentAccount__c != '0016F00001i2yYHQAY'))
                 { 
                     //if Parent Account is Blank
                   if((l.ParentAccount__c == null) || (l.ParentAccount__r.Name ==  ' '))
                   { 
                    erstring += '\n Parent Account' ; 
                    hasError = true; 
                   }
                     //if First Name is Blank
                   if((l.FirstName == null) || (l.FirstName ==  ' '))
                   {
                     erstring += '\nFirst Name'  ;
                     hasError = true; 
                   }
                     //if Email is Blank
                   if((l.Email == null) || (l.Email ==  ' '))
                   {
                     erstring += '\n Email';
                     hasError = true; 
                   }
                      //if Phone is Blank
                   if((l.Phone == null) || (l.Phone ==  ' '))
                   {
                     erstring += '\nPhone                          ';
                     hasError = true; 
                   }
                      //if Company is Blank
                   if((l.Company == null) || (l.Company ==  ' '))
                   {
                     erstring += 'Company \n';
                     hasError = true; 
                   }
                      //if Country is Blank
                   if((l.Country__c == null) || (l.Country__c ==  ' '))
                   {
                     erstring += 'Country \n';
                     hasError = true; 
                   } 
                      //if Industry is Blank
                   if((l.Industry_Ipay__c == null) || (l.Industry_Ipay__r.Name ==  ' '))
                   {
                     erstring += 'Industry \n';
                     hasError = true; 
                   }  
                      //if Parent Account is Blank
                   if((l.Trading_Name__c == null) || (l.Trading_Name__c ==  ' '))
                   {
                     erstring += 'Trading Name \n';
                     hasError = true; 
                   }
                      //if Address is Blank
                   if((l.Street == null) || (l.Street ==  ' '))
                   {
                     erstring += 'Street \n';
                     hasError = true; 
                   }
                    if((l.State == null) || (l.State ==  ' '))
                   {
                     erstring += 'State \n';
                     hasError = true; 
                   }
                   if((l.City == null) || (l.City ==  ' '))
                   {
                     erstring += 'City \n';
                     hasError = true; 
                   }   
                   if((l.PostalCode == null) || (l.PostalCode ==  ' '))
                   {
                     erstring += 'Post Code \n';
                     hasError = true; 
                   } 
                      //if Sales channel is Blank
                   if((l.Sales_Channel__c == null) || (l.Sales_Channel__c ==  ' '))
                   {
                     erstring += 'Sales Channel \n';
                     hasError = true; 
                   } 
                      //if Average Transaction Amount is Blank
                   if((l.Avg_Trans_Amount__c == null) || (l.Sales_Channel__c ==  ' '))
                   {
                     erstring += 'Average Transaction Amount \n';
                     hasError = true; 
                   } 
                      //ifTransactions per month is Blank
                   if((l.Trans_Per_Month__c == null) )
                   {
                     erstring += 'Transactions per month \n';
                     hasError = true; 
                   }
                      //if Max Singl ePay Amount is Blank
                   if((l.Max_Single_Pay_Amt__c == null))
                   {
                     erstring += 'Maximum single payment amount \n';
                     hasError = true; 
                   }
                      //ifDirector Details Blank
                   if((l.Director_1_First_Name__c == null) || (l.Director_1_First_Name__c ==  ' '))
                   {
                     erstring += 'Director First Name  \n';
                     hasError = true; 
                   } 
                   if((l.Director_1_Last_Name__c == null) || (l.Director_1_Last_Name__c ==  ' '))
                   {
                     erstring += 'Director Last Name  \n';
                     hasError = true; 
                   } 
                   if((l.Director_Email__c == null) || (l.Director_Email__c ==  ' '))
                   {
                     erstring += 'Director Last Name  \n';
                     hasError = true; 
                   } 
                      //if no products selected 
                   if(!((l.Direct_Debit_Requested__c) || (l.eCommerce__c) || (l.Video_Payment__c) || (l.Xero_Payment_Now__c) || (l.Xero_Direct_Debit__c) ))
                   {
                     erstring += 'One of the products Direct Debit Requested/Ecommerce/Video Payment/Xero Payment Now/Xero Direct Debit is not selected';
                     hasError = true; 
                   } 
                      //Finally add the error message if any of the fields above atre blank
                   if(hasError)
                   {
                     l.addError( erstring,false);
                   }
                  }
             }  
         }
}

Attached the popup screenshot. Does any one have any idea what is wrong?
User-added image
Best Answer chosen by Yamini Bathula
Yamini BathulaYamini Bathula
Thanks for the reply Pawan.

I just came through an old thread that this cannot be achived as Salesforce doesnt support HTML inputs in Lead Conversion Page due to security reasons.
https://developer.salesforce.com/forums/?id=906F00000005GY3IAM 

salesforce known Issue link https://success.salesforce.com/issues_view?id=a1p30000000jaTX . Thanks for your help Pawan. I am posting this so that others can benefit.

Thnaks,

 

All Answers

PawanKumarPawanKumar
replace all '\n' with '\r\n'

String erstring = 'Please Enter Below Mandatory Fields before converting this Lead \r\n';
PawanKumarPawanKumar
please confirm whether it works for you or not?
Yamini BathulaYamini Bathula
Hi Pawan,

I tried that. Bu it didnt work
 
PawanKumarPawanKumar
Thanks for Reply. I think what i share you the  above it works good for Long Text Area field.

Root Cause :Salesforce by default escapes any html in the addError String. To work around this, use the addError method with additional 'escape' parameter at the end. Here you are already passing false in addError method. But instead of \n now you have to use <br/> Then you will see the new line in your page.

e.g.

erstring += 'Director Last Name  <br/>';

Reference  URL:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_addError_2
Yamini BathulaYamini Bathula
Thanks for the reply Pawan.

I just came through an old thread that this cannot be achived as Salesforce doesnt support HTML inputs in Lead Conversion Page due to security reasons.
https://developer.salesforce.com/forums/?id=906F00000005GY3IAM 

salesforce known Issue link https://success.salesforce.com/issues_view?id=a1p30000000jaTX . Thanks for your help Pawan. I am posting this so that others can benefit.

Thnaks,

 
This was selected as the best answer