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...

Tuesday, October 17, 2017

Friday, October 13, 2017

How to get all the child objects related to an object in Salesforce

Solution : By using getChildRelationships() ,we can fetch all the child objects associated to an object.
In this example,we fetch all the child objects associated to Account object.
Sample Apex Code:
Set<String> lstObject = new Set<String>();
Schema.DescribeSObjectResult sObjResult = Account.SObjectType.getDescribe();
for(Schema.ChildRelationship cr: sObjResult.getChildRelationships()) {
    lstObject.add(string.valueOf(cr.getChildSObject()));
}
system.debug('Child Objects associated with Account:'+lstObject);
Output: When we run the above code in workbench,output will be like this:


How to get all the parent objects related to an object in Salesforce

Solution : By iterating through all the fields of an object and checking if it has any reference,we can fetch all the parent objects associated to an object.
In this example,we fetch all the parent objects associated to opportunity object.
Sample Apex Code:
Set<String> lstObject = new Set<String>();
for(Schema.SobjectField sField: opportunity.SobjectType.getDescribe().fields.getMap().Values()) {
    if(sField.getDescribe().getType() == Schema.DisplayType.REFERENCE) {
        for(Schema.sObjectType sObj : sField.getDescribe().getReferenceTo()) {
        lstObject.add(string.valueOf(sObj));
        }
    }
}
system.debug('Parent Objects associated with Opportunity:'+lstObject);
Output: When we run the above code in workbench,output will be like this:

Thursday, October 12, 2017

How to capture/take screenshot of entire screen even by scrolling(Full Page Capture)

Google Chrome has provided a plugin 'Fireshot' to capture full page.
Steps:
Step 1: Open the following url Fire Shot or search for Fireshot plugin in chrome browser.
The following screen will be opened.

Step 2: Click on "Add to Chrome" as shown in the image above.
It will open popup as shown below.

Step 3: Click on 'Add Extension' to add the plugin to browser.
Once plugin is added,you will find symbol 's' added on browser on the top right as shown in the image below

Step 4: Click on that icon and click on 'Capture selection' as shown in the image below to capture what ever you select even by scrolling.

Step 5:Thats it..now your screen is captured....



Tuesday, October 3, 2017

How to login to the newly created salesforce Trailhead playground - Salesforce Globe For You

Steps:
Step 1: Follow the steps from 1 to 5 in the following page
http://salesforceglobe4u.blogspot.in/2017/10/how-to-create-trialhead-playground.html
Step 2: Click on the dropdown icon before 'Launch' button and then select your trialhead playground instance.


Step 3: Click on Launch button to launch the instance.
Step 4: After instance is opened,click on Gear symbol as shown in the image below nd select Setup.

Step 5: Enter Users in the Quick Find box, then select Users.
Check the box next to your name and Make note of your username as highlited in the image below.
Step 6: Click Reset Password(s) and then OK.
This sends an email to the address associated with your username. If you don't see the email, check your spam folder.
Click the link in the email.
Step 7: Enter a new password and confirm it.
Thats it.Now you have the username and password for your Trailhead Playground!

How to create trialhead playground instance for trialhead in salesforce

Steps:
Step 1: Go to https://trailhead.salesforce.com/ 
The following screen opens as shown in the image below.

Step 2: Login to trailhead account by clicking on login button as shown in the above image.

Click on 'Login to Salesforce' and enter credentials to login in.
Step 3: After u logged in to existing salesforce trialhead,Go to modules tab and go to any one trialhead topic for example I went to 'Apex Basics and Database' module.
https://trailhead.salesforce.com/modules/apex_database


Step 4: Click on 'Get Started with Apex' hyperlink .It will be redirected to the following screen.

Step 5: Scroll down the screen where you find launch button as shown in the image.

Step 6: Click on the dropdown icon before 'Launch' button as shown in the image above and then click on 'Create a Trialhead playground' as shown in the image below to create a trialhead playground instance.

Thats it.Wait for couple of minutes.The playground instance gets created.Enjoy !!!

Wednesday, September 27, 2017

How to override tab with custom visualforce page in salesforce

In this example,We will override Account Tab with custom Visualforce Page.
Steps:
Step 1: Go to Setup --> customize --> Accounts --> Buttons,links and Actions.

Step 2: In the Buttons,links and Actions Section,click on Edit link of the tab as shown in the image below.
Step 3: Click on visualforce page radio button and select the necessary visualforce page as shown in the image below
Step 4: Click on save button to save the data as show in the image below.
Thats it.Go to Accounts Tab,now you can see your visualforce page gets opened.

Data Loader Command-Line Interface In Salesforce

Thank you Shiva Chitta for sharing this information.

INTRODUCTION:
Data Loader is tool provided by Salesforce for bulk import or export of data. Sometimes, we come across many scenarios where we want to automate the operations or we perform the same task repeatedly.
In this case, we can use Salesforce Data Loader Command Line Interface (CLI).
CLI runs Data Loader from the command line but it is challenging and difficult to set up. So there CLI quickstart (CLIq) comes into help. It will generate all required files for Command line data loader.
  Installation Steps:
·                 Install Data Loader version 17.0 or later.
·                To download Data Loader, login to Salesforce and go to
       Set Up -> Data Management -> Data Loader -> Download Data Loader
·                Download the latest version of CLIq
·                Unzip cliq.zip and copy & paste the ‘cliq’ folder into your Data Loader home directory
      Typically, this is: C:\Program Files\salesforce.com\Data Loader\cliq
 Configuration Steps:

Go to C:\Program Files\salesforce.com\Data Loader\cliq and open cliq.properties file for proxy settings.

Note: By default, cliq will set for Production. To use it for the sandbox, uncomment line 23 (remove # and save it).
Let’s Get Started – (Part 1 – Data Export)
To run CLIq, run cliq.bat on Windows (cliq.sh on UNIX).
1. Choose an Operation
2. Enter the process name (string). Example: Export_ Accounts
3. Click on Next and enter your org credentials (username, password + security token)
4. Enter Query (Export) or Entity (Object API Name).
5. Review results and create Data Loader CLI Files.
Note: When trying to run process.bat for Salesforce Data Loader from the command line, I encountered an error, "The system cannot find the path specified".
Follow the below steps to resolve the error:

1) Create a folder dataloader (no spaces) in C:\ drive (C:\dataloader)
2) Copy dataloader.jar ,JAVA folder and cliq folder from C:\Program Files\salesforce.com\Data Loader to C:\dataloader
3) Run cliq.bat from cliq folder

A directory will be created with name same as Process name.
·         C:\dataloader\cliq_process\Export_Accounts

As we selected Export Operation, it will automatically create a Export_Accounts.csv file with all data in C:\dataloader\cliq_process\Export_Accounts\write.






Tuesday, September 26, 2017

System.EmailException: SendEmail failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_OR_READONLY, Not profiled to access this Org-wide Email Address:

Problem: Whenever you are referring the organization wide default address while sending emails through sendEmail methods,sometimes you may encounter this error.

Solution: Go to Organization Wide Email Addresses and give necessary profile level access.
Go to Setup --> Email Adminstration --> Organization-wide addresses --> click on 'edit' button of that Email and give profile level access.

How to send email through a custom button on record detail page salesforce

In this example, We are considering sending Email through lead record detail page.

Note: 1) Subject__C custom field is created in  lead object to store the subject of Email.

2) Standard Email field is used to store email address to which email will be sent.
Standard Description field is used to store content/body of Email.

3) Email and Description are mandatory fields in order to send an Email.

Step 1: Create an Apex Class:
public class SendEmailFromLeadController {
    public String emailSent {get;set;}
    public SendEmailFromLeadController(ApexPages.StandardController controller) {
   
    }

    public PageReference sendEmail() {
        emailSent ='';
        List<lead> lstLead = [ select Name,id,description,subject__c,Email FROM lead  WHERE id = :ApexPages.currentPage().getParameters().get('id')];
        emailSent = ApexPages.currentPage().getParameters().get('emailSent');
        PageReference pageRef ;
        if(emailSent == null || emailSent =='') {
            list<Messaging.SingleEmailMessage> lstEmail = new list<Messaging.SingleEmailMessage>();
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setTargetObjectId(lstLead[0].Id);
            mail.setToAddresses(new List<String>{lstLead[0].Email});
            mail.SetSubject(lstLead[0].subject__c);
            mail.setPlainTextBody(lstLead[0].description);
            lstEmail.add(mail);
            list<Messaging.SendEmailResult> lstResults = Messaging.sendEmail(lstEmail);
            if(lstResults[0].isSuccess()) {
                emailSent = 'Email Successfully Sent';
                pageRef = New PageReference('/apex/SendEmailFromLead?emailSent='+'Email Successfully Sent');
            } else {
                emailSent = 'Email Not Sent';
                pageRef = New PageReference('/apex/SendEmailFromLead?emailSent='+'Email Not Sent');
            }
            pageRef.setRedirect(true);
        } else {
            pageRef = null;
        }
       
        return pageRef ;
    }

}

Step 2: Create visualforce Page - PageName : SendEmailFromLead
<apex:page standardcontroller="lead" extensions="SendEmailFromLeadController" action="{!sendEmail}">
<apex:form >
 <b style="color:red">{!emailSent}</b>
</apex:form>
</apex:page>

Step 3: Create custom Button on lead object for example like this

Thats it...Go to Lead Record..Enter email,description,subject fields and then click 'Send Email' button to send an email .Enjoy...

Friday, September 22, 2017

What is Salesforce Ohana?

What is Salesforce Ohana?

Salesforce Ohana is simply an idea that keeps all the family members together ,helping each other,enjoying together etc.

Go through this link Salesforce Ohana to know more about it.

Do you know about Salesforce MVP?

Do you know about Salesforce MVP?

Go through the link Salesforce MVP to know more about Salesforce MVP.

Wednesday, September 20, 2017

Salesforce New MVP's

Congratulations to all the new Salesforce MVP's.
You really helped all the salesforce Admins/Developers through your blogs,communities etc.
Please go through the link for more information on Salesforce MVP
Salesforce MVP 

How to create or register namespace in salesforce instance

Step 1: Go to Setup --> Create --> packages.

Step 2: Click on 'Edit' button under Developer Settings section.The following screen appears.

Step 3: Once 'Continue' button is clicked,the following screen appears

Step 4: Enter Namespace prefix in 'Register a namespace prefix' section and check if its available.For example 'ksk' namespace prefix is available.Then click on 'Review My Selections' button as shown in the image below.

Step 5: After click on 'Review My Selections' button,you will be redirected to the following screen.

Step 6: Click on 'Save' button,it will be redirected to the screen as shown in the image below.

That's it, the namespace is successfully created.

Saturday, August 5, 2017

How to create custom tab in salesforce

Step1 : Go to Setup --> Build --> Create --> Tabs.
Here you have different sections for tabs
1) Custom Object Tabs
2) Web Tabs
3) Visualforce Tabs
4) Lightning Component Tabs
5) Lightning Page Tabs

Based on which tab we want to create,click on the new button next to that tab Section as shown below.

If You want to create tab for custom Object,click on new button next to custom Object tabs section.

Step 2:Once you click new button to create custom object tab,the following screen appears as shown in image below.
Select the object,tab style and click on 'next' button.
Step 3: Choose the profiles for which this custom tab will be available.
click on "Next" button
Step 4: Choose the custom apps for which the new custom tab will be available and then click on 'save' button.

Thats it...now Clients tab is created.

Thursday, July 27, 2017

How to create/write trigger on custom object in salesforce

Step 1: Go to setup --> Create --> Objects

Step 2: Select the custom object on which you want to write a trigger and click on its label.

The custom object page will be opened as shown in the image below.

scroll down the page,you will find a section with trigger as shown in the image.
Click on the "new" button to create the trigger.