Play Games

Search This Blog

Monday, November 13, 2017

How do we know whether it is sandbox or production using code?

Solution: We can know whether the instance is production or sandbox by querying on Organization object.
Example : 
Run the piece of code:
List<organization> lstOrganization = [Select id,instanceName,isSandbox from Organization];
if(lstOrganization.size()>0) {
   if(lstOrganization[0].isSandbox) {
       system.debug('Its Sandbox Instance');
   } else {
       system.debug('Its Production Instance');
   }

}
Output :

Difference between == and ===

'==' checks whether both values are equal or not where as '===' checks both value and type are equal or not.
Example 1:
<html>
<Script>
window.onload = function check() {
var a = 5;
var b = '5';
if(a=== b) {
alert("a and b are equal.Triple Equal checks both value and type are equal or not");
}
if(a==b) {
alert("a and b are equal.Double Equal check only whether value is same but not its type");
}
}
</script>

</html>
if we run the above code,it displays the alert "a and b are equal.Double Equal check only whether value is same but not its type" because a and b have same value 5 but type of a is number and type of b is string.So,it wont display other alert related to '==='.

Example 2:

<html>
<Script>
window.onload = function check() {
var a = 5;
var b = 5;
if(a=== b) {
alert("a and b are equal.Triple Equal checks both value and type are equal or not");
}
if(a==b) {
alert("a and b are equal.Double Equal check only whether value is same but not its type");
}
}
</script>

</html>

It displays both the alerts as both value and type of a and b are equal.

Tuesday, November 7, 2017

Offset in Salesforce

Offset: Offset is used to skip the specified number of records and fetch the other records from database while fetching the data using SOQL query.
For example list<Lead> lstLead = [select id,name from Lead offset 5 limit 10];
The above query skips first 5 records and fetches records from 6 to 15 from Database as we have limit 10.
We usually use the concept of offset in pagination.

Example:
Run the following piece of code and observe the difference in the SOQL queries.

List<lead> lstLead = [select id,name from Lead];
system.debug('Leads count with out Offset:'+lstLead.size());
lstLead = [select id,name from Lead offset 10];
system.debug('Leads count with out Offset:'+lstLead.size());

Output:

Considerations while using OFFSET: 

1) We can use maximum offset as 2000 only.If we give offset value > 2000,results in NUMBER_OUTSIDE_VALID_RANGE error.

2) We cannot use offset in subquery in general,but in one scenario only,we can use it in subquery i.e if the parent query has Limit 1 clause,then we can use offset in subquery.

Example:
SELECT Name, Id
    (
        SELECT Name FROM Opportunity LIMIT 10 OFFSET 2
    )
FROM Account
ORDER BY Name
LIMIT 1 
The above query is valid since parent Account query has Limit 1 clause.

SELECT Name, Id
    (
        SELECT Name FROM Opportunity LIMIT 10 OFFSET 2
    )
FROM Account
ORDER BY Name is invalid because offset is referred in subquery but in parent account query limit 1 is not used.

3) The OFFSET clause is allowed in SOQL used in SOAP API, REST API, and Apex. It’s not allowed in SOQL used within Bulk API or Streaming API.

Saturday, November 4, 2017

How to get name of an object based on id of the record in salesforce

Solution: we can use getSObjectType().getDescribe().getName() to get object name.
Id accountId = '0019000001ryzZP';
String objectName = accountId.getSObjectType().getDescribe().getName();
system.debug('Object Name:'+objectName);

Output: Object Name: Account

How to display today's date in Email Templates(Text template) salesforce

Solution : We use {!TODAY()}  to display today's Date in Email Template.
Example:

1) To display today's Date : {!TODAY()}

2) To display only year of today's date : {!YEAR(TODAY())}

3) To display month in MMM format : {!case(month(today()),1,'Jan',2,'Feb',3,'Mar',4,'Apr',5,'May',6,'Jun',7,'Jul',8,'Aug',9,'Sep',10,'Oct',11,'Nov','Dec')}

4) To display date in MM/DD/YYYY format : {!MONTH(TODAY())} / 01/{!YEAR(TODAY())}

When you use above code in text email template the sample output will be like this.


Thursday, November 2, 2017

Access/refer custom Label in Visualforce Page

Solution : Use {!$Label.LabelName}.
If label Name is 'Blog_URL' ,then in vf page you can refer it as {!$Label.Blog_URL}
Example:
<apex:page>
  <b>Custom Label Referred likt this in vf page </b>: {!$Label.Blog_URL}
</apex:page>

Access/refer custom Label in Apex Class

Solution: use Label.LabelName.
If label Name is 'Blog_URL' ,then in apex you can refer it as Label.Blog_URL
String str = Label.Blog_URL;

How to create custom Label in Salesforce

Goto Setup --> Create --> Custom Labels --> Click on 'New Custom Label'.
Fill all the required details as shown in the image below and save it.


That's it.The custom Label is now ready to use...