TemplateApexException Apex Examples

Use the TemplateApexException class to handle errors cleanly and provide clear information to template users.

Throwing an exception fails the configuration wizard. You can also create variables to track errors and report them to the user in the wizard without failing.

Note

Report an error to the user with a TemplateApexException: 

1public override void onConfigurationRetrieval(wavetemplate.WaveTemplateInfo template)
2{
3    // Run a query if Forecast History is enabled and report failure if isn't
4    List<User> usersWithForecast = Database.query('SELECT Id FROM User WHERE ForecastEnabled = true LIMIT 1');
5    if (usersWithForecast.isEmpty()) {
6        // Report issue to user
7        throw new TemplateApexException('Forecast history is not enabled. Please have your admin enable it.');
8    }
9}

Handle database exceptions with a TemplateApexException 

1public override void onConfigurationRetrieval(wavetemplate.WaveTemplateInfo template)
2{
3     String query = 'SELECT DeveloperName, MasterLabel, SplitField, SplitEntity FROM OpportunitySplitType WHERE IsActive = true AND SplitEntity = \'Opportunity\'';
4     try {
5         List<SObject> activeSplitTypes = Database.query(query);
6         if (activeSplitTypes.isEmpty()) {
7             template.getVariables().get('Has_OpportunitySplits').setComputedValue('No');
8         }
9         else {
10             template.getVariables().get('Has_OpportunitySplits').setComputedValue('Yes');
11         }
12      }
13      catch (Exception e) {
14          throw new TemplateApexException('Database query to determine Opportunity Splits failed: ' + e.getMessage());
15      }
16}