Salesforce url’s/instances — November 25, 2016

Salesforce url’s/instances

Hi All,  an idea on Salesforce url’s
we can access one instance from one id at a time.
AP | NA | EU | CS are the possible instances for salesforce.
any environment will have a URL like https://na12.salesforce.com (example), which may vary depending on the location of the organization.
AP | NA | EU – Production Environment Instances
CS – Sandbox Environment Instances
Production environment can be accessed using https://login.salesforce.com
Sandbox environment can be accessed using https://test.salesforce.com

Continue reading

Page Reference method return null — November 21, 2016

Page Reference method return null

What is the use of PageReference ?

For Example

public Pagereference method1()
{
return null;
}

why we use pagereference as a return type?
what is the situation to use the pagereference?
Use of PageReference: A PageReference is a reference to an instantiation of a page reference. Among other attributes, PageReferences consist of a URL and a set of query parameter names and values like custom parameters or standard parameters for PageReference created.
PageReference pageRef = new PageReference('/apex/myVfPage');
There are several standard parameters like setRedirect which can be used for hard “reset” of controller state. If you make it false then state of variables in controller is maitained across many VF pages.
pageRef.setRedirect(true);
If you want to add any custom parameters then you can add them like below
pageRef.getParameters().put('myId', accId); //Assume accId is a variable in class or method

So, overall code will look like

public PageReference redirectToMyVF(Id accId) {	
	PageReference myVFPage = new PageReference('/apex/myVFPage')
	myVFPage.setRedirect(true);
	myVFPage.getParameters().put('myId', accId);
	return myVFPage;
}
1. Why we use pagereference as a return type?
We normally set return type to a PAgeReference when we need user to other VF page. We return “null” if we want user on same page and we normally return “null” in catch blocks.

2. What is the situation to use the pagereference?
When you want to jump to other VF page when something is clicked and method takes you to new VF page or refreshing current page by doing “return null;”

ApexPages.StandardController —

ApexPages.StandardController

Use a StandardController when defining an extension for a standard controller.

Usage

StandardController objects reference the pre-built Visualforce controllers provided by Salesforce. The only time it is necessary to refer to a StandardController object is when defining an extension for a standard controller. StandardController is the data type of the single argument in the extension class constructor.

Instantiation

You can instantiate a StandardController in the following way:

ApexPages.StandardController sc = new ApexPages.StandardController(sObject);

Example

Continue reading

Parsing Json Using Apex — November 20, 2016

Parsing Json Using Apex

JSON is a lightweight data-interchange format. It is the most preferred way of transferring data over web.

In Salesforce, this comes to most use when integrating with an external system.

In most of the integrations, you typically use HTTP Callouts to the end points defined by various services, and the most common response format returned by these is JSON.

Now, parsing this JSON would be very time consuming if not done in the right way. One way of doing this would be manually parsing the complete JSON using the JSONParser method.

But the easiest way of Parsing JSON would be to de-serialize it into an Object.

Continue reading

Using the with sharing or without sharing Keywords — November 19, 2016

Using the with sharing or without sharing Keywords

Use the with sharing or without sharingkeywords on a class to specify whether or not to enforce sharing rules.

The with sharing keyword allows you to specify that the sharing rules for the current user be taken into account for a class. You have to explicitly set this keyword for the class because Apex code runs in system context. In system context, Apex code has access to all objects and fields— object permissions, field-level security, sharing rules aren’t applied for the current user. This is to ensure that code won’t fail to run because of hidden fields or objects for a user. The only exceptions to this rule are Apex code that is executed with the executeAnonymous call and Chatter in Apex. executeAnonymous always executes using the full permissions of the current user. For more information on executeAnonymous, see Anonymous Blocks.

Use the with sharing keywords when declaring a class to enforce the sharing rules that apply to the current user. For example:

1 public with sharing class sharingClass {

2  

3 // Code here

4  

5 }

Use the without sharing keywords when declaring a class to ensure that the sharing rules for the current user are not enforced. For example, you may want to explicitly turn off sharing rule enforcement when a class acquires sharing rules when it is called from another class that is declared using with sharing.

1 public without sharing class noSharing {

2  

3 // Code here

4  

5 }

Some things to note about sharing keywords:

  • The sharing setting of the class where the method is defined is applied, not of the class where the method is called. For example, if a method is defined in a class declared with with sharing is called by a class declared with without sharing, the method will execute with sharing rules enforced.
  • If a class isn’t declared as either with or without sharing, the current sharing rules remain in effect. This means that the class doesn’t enforce sharing rules except if it acquires sharing rules from another class. For example, if the class is called by another class that has sharing enforced, then sharing is enforced for the called class.
  • Both inner classes and outer classes can be declared as with sharing. The sharing setting applies to all code contained in the class, including initialization code, constructors, and methods.
  • Inner classes do not inherit the sharing setting from their container class.
  • Classes inherit this setting from a parent class when one class extends or implements another.
Asynchronous Apex — November 14, 2016
Batch Apex — November 12, 2016

Batch Apex

In this article, we will be loking at Batch Apex.

Batch Apex  is useful to build complex, long-running processes that run on thousands of records on the Force.com platform. Batch Apex operates over small batches of records, covering your entire record set and breaking the processing down to manageable chunks.

Lets take a scenario . Say i have some thousands of records that contain information about a product like product name, category(furniture, electronics etc.), product container etc..

I got a requirement to change the product container name from Medium Box to Medium Size Box  pertaining to a particular product called Furniture. Lets see how to achieve this using Batch Apex. Continue reading

SOQL Parent to child queries — November 7, 2016

SOQL Parent to child queries

Lets look at retrieving child data  from  Parent object

soqlrelations

An Example:

public class SOQLParentChildQueryExample {

public List accs {set;get;}

public void standardObjectStandardLookup(){
flag1=true;
flag2=false;
accs=[select name,Industry ,(select lastName,firstName from Contacts) from Account]; // //Contacts is the standard relation name defined by sfdc.
}

public void standardObjectCustomLookup(){
flag1=false;
flag2=true;
accs=[select name,Industry ,
(select lastName,firstName from CustomAccountRelation__r) from Account 
where Industry='Energy'];

//CustomAccountRelation is the custom relation name defined by user. 
So __r is required.
}
}

SOQL Child to Parent Queries —

SOQL Child to Parent Queries

In this article, lets look at how to retrieve query results in  two related objects.

Child to Parent scenario is:

PField -parent field

CField-Child field

RField – Relationship field

Salesforce created standard  relation name can be directly used whereas custom relation name should be appended by __r.

soqlchildparentrelations

Lets look at an example:

public class SOQLChildParentQueryExample {
public List cons {set;get;}

public void standardObjectStandardLookup(){

cons=[select lastName,firstName , Account.name,Account.Industry from Contact]; 
// Account is the standard relation lookup field name defined by sfdc.
}

public void standardObjectCustomLookup(){

cons=[select lastName,firstName , CustomAccount__r.name, 
CustomAccount__r.Industry from Contact ]; 
//CustomAccount is the custom relation lookup field name defined by user. 
//So __r is required.
}
}

SOQL Basics4 —