+ Start a Discussion

display page message on LICENSE_LIMIT_EXCEEDED



I am creating user record using VF page and Apex class. When an Org does not contains sufficient user licenses of Type Salesforce, then I get following message.


But I want to display these message in customized way. For example, Message as "Your Org does not have sufficient Licenses"


System.DmlException: Insert failed. First exception on row 0; first error: LICENSE_LIMIT_EXCEEDED, License Limit Exceeded: []


Now i want display page message instad of exception. is it possible?

Best Answer chosen by Admin (Salesforce Developers) 

You can catch the DML error, inspect it and put your own error out.


Something like:


   insert myUser;
catch (DMLException e)
   if (e.getMessage().contains('LICENSE_LIMIT_EXCEEDED'))
       ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.Error, 'Your org does not have sufficient licenses'));
      // rethrow the exception or take some other action here
      throw e;