Tag Archives: Apex

Salesforce StandardController Methods

The following methods are for StandardController:

addFields(fieldNames)
When a Visualforce page is loaded, the fields accessible to the page are based on the fields referenced in the Visualforce markup. This method adds a reference to each field specified in fieldNames so that the controller can explicitly access those fields as well.

cancel()
Returns the PageReference of the cancel page.

delete()
Deletes record and returns the PageReference of the delete page.

edit()
Returns the PageReference of the standard edit page.

getId()
Returns the ID of the record that is currently in context, based on the value of the id query string parameter in the Visualforce page URL.

getRecord()
Returns the record that is currently in context, based on the value of the id query string parameter in the Visualforce page URL.

reset()
Forces the controller to reacquire access to newly referenced fields. Any changes made to the record prior to this method call are discarded.

save()
Saves changes and returns the updated PageReference.

view()
Returns the PageReference object of the standard detail page.

Salesforce StandardSetController Methods

The following methods are for StandardSetController:

cancel()
Returns the PageReference of the original page, if known, or the home page.

first()
Returns the first page of records.

getCompleteResult()
Indicates whether there are more records in the set than the maximum record limit. If this is false, there are more records than you can process using the list controller. The maximum record limit is 10,000 records.

getFilterId()
Returns the ID of the filter that is currently in context.

getHasNext()
Indicates whether there are more records after the current page set.

getHasPrevious()
Indicates whether there are more records before the current page set.

getListViewOptions()
Returns a list of the listviews available to the current user.

getPageNumber()
Returns the page number of the current page set. Note that the first page returns 1.

getPageSize()
Returns the number of records included in each page set.

getRecord()
Returns the sObject that represents the changes to the selected records. This retrieves the prototype object contained within the class, and is used for performing mass updates.

getRecords()
Returns the list of sObjects in the current page set. This list is immutable, i.e. you can’t call clear() on it.

getResultSize()
Returns the number of records in the set.

getSelected()
Returns the list of sObjects that have been selected.

last()
Returns the last page of records.

next()
Returns the next page of records.

previous()
Returns the previous page of records.

save()
Inserts new records or updates existing records that have been changed. After this operation is finished, it returns a PageReference to the original page, if known, or the home page.

setFilterID(filterId)
Sets the filter ID of the controller.

setpageNumber(pageNumber)
Sets the page number.

setPageSize(pageSize)
Sets the number of records in each page set.

setSelected(selectedRecords)
Set the selected records.

Function to Calculate the 18 Character ID From 15 Character ID

public String Convert15CharTo18CharId(String id) {
    String suffix = '';
    Integer flags;

    for (integer i = 0; i < 3; i++) {
        flags = 0;
        for (Integer j = 0; j < 5; j++) {
            String c = id.substring(i * 5 + j, i * 5 + j + 1);
            //Only add to flags if c is an uppercase letter:
            if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
                flags = flags + (1 << j);
            }
        }
        if (flags <= 25) {
            suffix = suffix + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags, flags + 1);
        } else {
            suffix += '012345'.substring(flags - 26, flags - 25);
        }
    }
    String convertId = id + suffix;
    //18 Digit Id with checksum
    System.debug('convertId-' + convertId);
    return 18 digitId;
}

Example:

String Id = '0019000001EJNfj';
String convertedId = Convert15CharTo18CharId(Id);

Difference Between Enterprise WSDL and Partner WSDL

WSDL:
WSDL (Web Services Description Language) is an XML document that describes a web service. WSDL is derived from Microsoft’s Simple Object Access Protocol (SOAP) and IBM’s Network Accessible Service Specification Language (NASSL). WSDL replaces both NASSL and SOAP as the means of expressing business services in the UDDI registry. It is used in combination with SOAP and XML Schema to provide web services over the Internet. A client program connecting to a web service can read the WSDL to determine what functions are available on the server. Any special datatypes used are embedded in the WSDL file in the form of XML Schema. The client can then use SOAP to actually call one of the functions listed in the WSDL.

Salesforce provides a WSDL (Web Service Description Language) files. They are called “Enterprise WSDL” and “Partner WSDL”. The WSDL is used by developers to aid in the creation of Salesforce integration pieces. A typical process involves using the Development Environment (eg, Eclipse for Java, or Visual Studio for .Net) to consume the WSDL, and generate classes which are then referenced in the integration.

Enterprise WSDL:

  • The Enterprise WSDL is strongly typed, which means that it contains objects and fields with specific data types, such as int and string.
  • The Enterprise WSDL document is for customers who want to build an integration with their Salesforce organization only.
  • Customers who use the enterprise WSDL document must download and re-consume it whenever their organization makes a change to its custom objects or fields or whenever they want to use a different version of the API.

Partner WSDL:

  • The Partner WSDL is loosely typed, which means that you work with name-value pairs of field names and values instead of specific data types.
  • This WSDL document is for customers, partners, and ISVs who want to build an integration that can work across multiple Salesforce organizations, regardless of their custom objects or fields.
  • The Partner WSDL is static, and hence does not change if modifications are made to an organization’s Salesforce configuration.

Read REST API GET Parameters in Apex Class

URL: /services/apexrest/AccountAPI?id=AccountId
Method: Get

@RestResource (urlMapping='/AccountAPI/*')
global with sharing class AccountRESTService {
    @HttpGet
    global static Account getAccount() {
        RestRequest req = RestContext.request;
        String accountId = req.params.get('id');
        Account acc = [SELECT Id, Name FROM Account WHERE Id =: accountId];
        return acc;
    }
}

URL: /services/apexrest/AccountAPI/AccountId
Method: Get

@RestResource (urlMapping='/AccountAPI/*')
global with sharing class AccountRESTService {

    @HttpGet
    global static Account getAccount() {
        RestRequest req = RestContext.request;
        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Account acc = [SELECT Id, Name FROM Account WHERE Id =: accountId];
        return acc;
    }
}