Tag Archives: Apex

Get Your Salesforce Org Current IP Address Using Apex

In Salesforce IP whitelisting is one of the most effective methods of ensuring secure connections to external systems. Salesforce provides a range of IP addresses that can be whitelisted by external systems based on your Salesforce instance.

Note: Add https://api.ipify.org in your Org remote site settings before executing below code.

Sample Code:

HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.ipify.org');
request.setMethod('GET');
Http http = new Http();
HttpResponse response = http.send(req);
String responseBody = response.getBody();
System.debug('Your Org current IP Address: ' + responseBody);

Custom Settings in Salesforce

Salesforce.com introduced Custom Settings in Winter ’10 which allows you to store custom data sets and associate them on an org-wide, profile or user basis. Custom settings data is exposed in the application cache and do not count against SOQL limits when fetched. This data can then be used by formula fields, validation rules, Apex, and the SOAP API.

Custom Settings support only Checkbox, Currency, Date, Date/Time, Email, Number, Percent, Phone, Text, Text Area, and URL field types. You can’t create Formula and Picklist, as well as field types that define relationships to other objects, like Lookup and Master/Detail. You can’t create lookups from Custom Objects to Custom Settings either. No Page layouts, record types, validation rules, triggers and workflow rules can be used on Custom Settings.

There are two types of custom settings:

List Custom Settings: It provides a reusable set of static data that can be accessed across your organization. If you use a particular set of data frequently within your application, putting that data in a list custom setting streamlines access to it. Data in list settings does not vary with profile or user, but is available organization-wide.

Hierarchy Custom Settings: Hierarchical Custom Settings are defined once and can hold a unique set of values for the Organization, each Profile or each individual User. Salesforce automatically grabs the lowest level of setting for the running user when a getInstance() call is made, or when accessed in a configuration such as a Validation Rule, Workflow or Formula Field. Only Hierarchical settings can be accessed declaratively whereas List settings are for Apex/Visualforce only.

Limitation of Custom Setting:

  • Maximum total data of 10 MB, but if you have less than 10 license users, multiply 1 MB with number of users.
  • 300 fields per custom setting..
  • Can’t share a custom setting record.
  • No owner assigned for each custom setting record.
  • Each custom setting counts against the total number of custom objects available for your organization.

Note: If you include custom settings in your distributed package you’ll need to build in some scripts which populate the settings with data after the package has been installed.

Salesforce Apex Scheduler

  • To invoke Apex classes to run at specific times, first implement the Schedulable interface for the class. Then, schedule an instance of the class to run at a specific time using the System.schedule method.
  • The class implements the Schedulable interface and must implement the only method that this interface contains, which is the execute method.
  • The parameter of this method is a SchedulableContext object. After a class has been scheduled, a CronTrigger object is created that represents the scheduled job. It provides a getTriggerId method that returns the ID of a CronTrigger API object.

Sample Code:

global class AccountBatchScheduled implements Schedulable {
    global void execute(SchedulableContext ctx) {
        //Batch class scheduled using Schedulable interface
        Database.executebatch(new AccountBatch());
    }
}

There are 2 ways to schedule an apex job:

  • Scheduling a Job from the UI
  • Using the System.Schedule Method

Difference between __c and __r in Salesforce

__c is for Custom objects For example: Custom_Object__c . It is used for reference custom object in Apex or visualforce page, formula field etc internally. Used as suffix.

__r is for Custom objects reference For example: Custom_Object__r . It is used for reference custom object relationship name in Apex or visualforce page, formula field etc. Used as suffix.

Difference between action function and action support

Action Function: Invoke the controller method from java script using AJAX and we can use action function from different places on visual force page.

Action Support: Invoke the controller method using AJAX when event occurs on page like onMouseOver, onClick, etc. and we can use action support for particular single apex component.