Step 4: Walk Through the Sample Code
Once you have imported your WSDL file, you can begin building client applications that use the API.
Use the following samples to create a basic client application. Comments embedded in the sample explain each section of code.
This section walks through a sample Java client application that uses the WSC SOAP client. The purpose of this sample application is to show the required steps for logging into the login server and to demonstrate the invocation and subsequent handling of several API calls.
To run this sample, you must pass the authentication endpoint URL as an argument for your program. You can obtain this URL from the WSDL file. This sample application performs the following main tasks:
- Prompts the user for their Salesforce username and password.
- Calls
login()to log in to the single login server and, if the login succeeds, retrieves user information and writes it to the console along with session information. - Calls
describeGlobal()to retrieve a list of all available objects for the organization’s data. ThedescribeGlobalmethod determines the objects that are available to the logged in user. This call should not be made more than once per session, since the data returned from the call is not likely to change frequently. The DescribeGlobalResult is echoed to the console. - Calls
describeSObjects()to retrieve metadata (field list and object properties) for a specified object. ThedescribeSObjectmethod illustrates the type of metadata information that can be obtained for each object available to the user. The sample client application executes adescribeSObjects()call on the object that the user specifies and then echoes the returned metadata information to the console. Object metadata information includes permissions, field types and lengths, and available values for picklist fields and types forreferenceTofields. - Calls
query(), passing a simple query string("SELECT FirstName, LastName FROM Contact"), and iterating through the returned QueryResult. - Calls
logout()to the log the user out.
The following sample code uses try/catch blocks to handle exceptions that might be thrown by the API calls.
This section walks through a sample C# client application. The purpose of this sample application is to show the required steps for logging in and to demonstrate the invocation and subsequent handling of several API calls.
This sample application performs the following main tasks:
-
Prompts the user for their Salesforce username and password.
-
Calls
login()to log in to the single login server and, if the login succeeds:-
Sets the returned sessionId into the session header, which is required for session authentication on subsequent API calls.
-
Resets the Lightning Platform endpoint to the returned
serverUrl, which is the target of subsequent API calls.All client applications that access the API must complete the tasks in this step before attempting any subsequent API calls.
-
Retrieves user information and writes it to the console along with session information.
-
-
Calls
describeGlobal()to retrieve a list of all available objects for the organization’s data. ThedescribeGlobalmethod determines the objects that are available to the logged in user. This call should not be made more than once per session, since the data returned from the call is not likely to change frequently. The DescribeGlobalResult is echoed to the console. -
Calls
describeSObjects()to retrieve metadata (field list and object properties) for a specified object. ThedescribeSObjectmethod illustrates the type of metadata information that can be obtained for each object available to the user. The sample client application executes adescribeSObjects()call on the object that the user specifies and then echoes the returned metadata information to the console. Object metadata information includes permissions, field types and lengths, and available values for picklist fields and types forreferenceTofields. -
Calls
query(), passing a simple query string("SELECT FirstName, LastName FROM Contact"), and iterating through the returned QueryResult. -
Calls
logout()to the log the user out.
The following sample code uses try/catch blocks to handle exceptions that might be thrown by the API calls.
The following code begins the sample C# client application.
The following C# example is the same as the previous C# example, except it uses .NET 3.0 SoapClient services instead of .NET 2.0 SforceService services.