SFDC 001

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Reference Exception Class

Enum Description
• Started

See Also:
Chatter in Apex Classes

ConnectApi Exceptions
The ConnectApi namespace contains exception classes.

All exceptions support built-in methods for returning the error message and exception type. The ConnectApi namespace
contains these exceptions:

Exception Description
ConnectApi.ConnectApiException Any logic error in the way your application is utilizing
ConnectApi code. This is equivalent to receiving a 400 error
with Chatter API in REST.
ConnectApi.NotFoundException Any issues with the specified resource being found. This is
equivalent to receiving a 404 error with Chatter API in REST.
ConnectApi.RateLimitException When you exceed the rate limit. This is equivalent to receiving
a 503 Service Unavailable error with Chatter API in REST.

See Also:
Chatter in Apex Classes

Exception Class
You can create your own exception classes in Apex. Exceptions can be top-level classes, that is, they can have member variables,
methods and constructors, they can implement interfaces, and so on.
Exceptions that you create behave as any other standard exception type, and can be thrown and caught as expected.
User-defined exception class names must end with the string exception, such as “MyException”, “PurchaseException” and
so on. All exception classes automatically extend the system-defined base class exception.
For example, the following code defines an exception type within an anonymous block:

public class MyException extends Exception {}

try {
Integer i;
// Your code here
if (i < 5) throw new MyException();
} catch (MyException e) {

612
Reference Apex Classes

// Your MyException handling code here


}

Like Java classes, user-defined exception types can form an inheritance tree, and catch blocks can catch any portion. For
example:

public class BaseException extends Exception {}


public class OtherException extends BaseException {}

try {
Integer i;
// Your code here
if (i < 5) throw new OtherException('This is bad');
} catch (BaseException e) {
// This catches the OtherException
}

This section contains the following topics:

• Constructing an Exception
• Using Exception Variables

See also Using Exception Methods.

Constructing an Exception
You can construct exceptions:

• With no arguments:

new MyException();

• With a single String argument that specifies the error message:

new MyException('This is bad');

• With a single Exception argument that specifies the cause and that displays in any stack trace:

new MyException(e);

• With both a String error message and a chained exception cause that displays in any stack trace:

new MyException('This is bad', e);

For example the following code generates a stack trace with information about both My1Exception and My2Exception:

public class My1Exception extends Exception {}


public class My2Exception extends Exception {}
try {
throw new My1Exception();
} catch (My1Exception e) {
throw new My2Exception('This is bad', e);
}

613
Reference Flow.Interview Class

The following figure shows the stack trace that results from running the code above:

Figure 8: Stack Trace For Exceptions (From Debug Log)

Using Exception Variables


As in Java, variables, arguments, and return types can be declared of type Exception, which is a system-defined based class in
Apex. For example:

Exception e1;
try {
String s = null;
s.tolowercase(); // This will generate a null pointer exception...
} catch (System.NullPointerException e) {
e1 = e; // ...which can be assigned to a variable, or passed
// into or out of another method
}

Flow.Interview Class
The Flow.Interview class provides advanced Visualforce controller access to flows.

Usage
Used with Visual Workflow, the Flow.Interview class has one method, which enables a Visualforce controller to access
the value of a flow variable embedded in the Visualforce page, or in a separate flow called by a subflow element.

Methods
The following is the instance method of the Flow.Interview class.

614
Reference HTTP (RESTful) Services Classes

Method Arguments Return Description


Type
getVariableValue String Object Returns the value of the specified flow variable. The flow variable
variableName can be in the flow embedded in the Visualforce page, or in a
separate flow that is called by a subflow element.
The returned variable value comes from whichever flow the
interview is currently running. If the specified variable can’t be
found in that flow, the method returns null.
The variableName argument specifies the unique name of the
flow variable.
This method checks for the existence of the variable at run time
only, not at compile time.

Sample
The following sample uses the getVariableValue method to obtain breadcrumb (navigation) information from the flow
embedded in the Visualforce page. If that flow contains subflow elements, and each of the referenced flows also contains a
vaBreadCrumb variable, the Visualforce page can provide users with breadcrumbs regardless of which flow the interview is
running.

public class SampleContoller {

//Instance of the flow


public Flow.Interview.Flow_Template_Gallery myFlow {get; set;}

public String getBreadCrumb() {


String aBreadCrumb;
if (myFlow==null) { return 'Home';}
else aBreadCrumb = (String) myFlow.getVariableValue('vaBreadCrumb');

return(aBreadCrumb==null ? 'Home': aBreadCrumb);

}
}

HTTP (RESTful) Services Classes


You can access HTTP services, also called RESTful services, using the following classes:

• HTTP Classes
• Crypto Class
• EncodingUtil Class

HTTP Classes
These classes expose the general HTTP request/response functionality:

• Http Class. Use this class to initiate an HTTP request and response.
• HttpRequest Class: Use this class to programmatically create HTTP requests like GET, POST, PUT, and DELETE.
• HttpResponse Class: Use this class to handle the HTTP response returned by HTTP.

615
Reference Apex Classes

The HttpRequest and HttpResponse classes support the following elements:

• HttpRequest:

◊ HTTP request types such as GET, POST, PUT, DELETE, TRACE, CONNECT, HEAD, and OPTIONS.
◊ Request headers if needed.
◊ Read and connection timeouts.
◊ Redirects if needed.
◊ Content of the message body.

• HttpResponse:

◊ The HTTP status code.


◊ Response headers if needed.
◊ Content of the response body.

The following example shows an HTTP GET request made to the external server specified by the value of url that gets
passed into the getContent method. This example also shows accessing the body of the returned response:

public class HttpCalloutSample {

// Pass in the endpoint to be used using the string url


public String getContent(String url) {

// Instantiate a new http object


Http h = new Http();

// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');

// Send the request, and return a response


HttpResponse res = h.send(req);
return res.getBody();
}
}

The previous example runs synchronously, meaning no further processing happens until the external Web service returns a
response. Alternatively, you can use the @future annotation to make the callout run asynchronously.
Before you can access external servers from an endpoint or redirect endpoint using Apex or any other feature, you must add
the remote site to a list of authorized remote sites in the Salesforce user interface. To do this, log in to Salesforce and from
Setup, click Security Controls > Remote Site Settings.

Note: The AJAX proxy handles redirects and authentication challenges (401/407 responses) automatically. For more
information about the AJAX proxy, see AJAX Toolkit documentation.

Use the DOM Classes or JSON Classes to parse XML or JSON content in the body of a request created by HttpRequest,
or a response accessed by HttpResponse.

Http Class
Use the Http class to initiate an HTTP request and response. The Http class contains the following public methods:

616

You might also like