A blog containing Salesforce coding examples in detail.It's a place where you can find solutions to the problems that you may face in your daily salesforce coding. Apex classes,visual force page,we services,Integration,plugins,extensions,Lightning web components,Aura components,Email Messages,Sales cloud,service Cloud,Marketing Cloud.Lightning Web components,salesforce.com, Salesforce Tips and Tricks,short cuts,ApexMocks,Stubs, visibility,Salesforce Shorts
Tuesday, December 31, 2019
Monday, December 2, 2019
Trigger must be associated with a job detail error while scheduling the batch job
Trigger must be associated with a job detail error while scheduling the batch job
Problem: This error occurred while scheduling the batch job using cron expression.
System.schedule('Schedule Batch Job that runs only once after 5 mins ', '0 '+sMinute+' '+sHour+' '+sDayOfMonth+' '+sMonth+' ?'+' '+sYear, new AccountUpdatorBatchSchedular());
Solution: Notice in the above sample code, there is space after the name of the job i.e Schedule Batch Job that runs only once after 5 mins.
When I remove extra spaces,I was able to resolve the issue.
System.schedule('Schedule Batch Job that runs only once after 5 mins', '0 '+sMinute+' '+sHour+' '+sDayOfMonth+' '+sMonth+' ?'+' '+sYear, new AccountUpdatorBatchSchedular());
Problem: This error occurred while scheduling the batch job using cron expression.
System.schedule('Schedule Batch Job that runs only once after 5 mins ', '0 '+sMinute+' '+sHour+' '+sDayOfMonth+' '+sMonth+' ?'+' '+sYear, new AccountUpdatorBatchSchedular());
Solution: Notice in the above sample code, there is space after the name of the job i.e Schedule Batch Job that runs only once after 5 mins.
When I remove extra spaces,I was able to resolve the issue.
System.schedule('Schedule Batch Job that runs only once after 5 mins', '0 '+sMinute+' '+sHour+' '+sDayOfMonth+' '+sMonth+' ?'+' '+sYear, new AccountUpdatorBatchSchedular());
How to schedule a batch job to run only once after 5 mins
How to schedule a batch job to run only once after 5 mins
Assume AccountUpdatorBatchSchedular is the schedular class that schedules the batch job.
global class AccountUpdatorBatchSchedular implements schedulable {
global void execute(SchedulableContext sc) {
AccountUpdatorBatch b = new AccountUpdatorBatch();
database.executebatch(b);
}
}
AccountUpdatorBatch is the actual batch job.
Sample Code to schedule job:
// Add 5 minutes to current Time
DateTime dtCurrentTime = System.now().addminutes(5);
String sHour = '', sMinute='', sDayOfMonth='', sMonth='', sYear='';
sMinute = String.ValueOf(dtCurrentTime.minute());
sHour = String.ValueOf(dtCurrentTime.hour());
sDayOfMonth = String.ValueOf(dtCurrentTime.day());
sMonth = String.ValueOf(dtCurrentTime.month());
sYear = String.ValueOf(dtCurrentTime.year());
System.schedule('Schedule Batch Job that runs only once after 5 mins', '0 '+sMinute+' '+sHour+' '+sDayOfMonth+' '+sMonth+' ?'+' '+sYear, new AccountUpdatorBatchSchedular());
Output:
Assume AccountUpdatorBatchSchedular is the schedular class that schedules the batch job.
global class AccountUpdatorBatchSchedular implements schedulable {
global void execute(SchedulableContext sc) {
AccountUpdatorBatch b = new AccountUpdatorBatch();
database.executebatch(b);
}
}
AccountUpdatorBatch is the actual batch job.
Sample Code to schedule job:
// Add 5 minutes to current Time
DateTime dtCurrentTime = System.now().addminutes(5);
String sHour = '', sMinute='', sDayOfMonth='', sMonth='', sYear='';
sMinute = String.ValueOf(dtCurrentTime.minute());
sHour = String.ValueOf(dtCurrentTime.hour());
sDayOfMonth = String.ValueOf(dtCurrentTime.day());
sMonth = String.ValueOf(dtCurrentTime.month());
sYear = String.ValueOf(dtCurrentTime.year());
System.schedule('Schedule Batch Job that runs only once after 5 mins', '0 '+sMinute+' '+sHour+' '+sDayOfMonth+' '+sMonth+' ?'+' '+sYear, new AccountUpdatorBatchSchedular());
Output:
Wednesday, October 23, 2019
Display elements of a list by skipping first few elements of it in visualforce page
We can display list by skipping specified number of elements using first attribute of <apex:repeat> tag.
Apex Code: DisplayElements
public class DisplayElements {
public List<Integer> lstNumber {get;set;}
public DisplayElements() {
lstNumber = new List<Integer>();
for(integer i=1; i<20; i++) {
lstNumber.add(i);
}
}
}
Visualforce Page:
<apex:page controller="DisplayElements" id="thePage">
<b>Elements after skipping first 5 elements:</b><br/>
<apex:repeat value="{!lstNumber}" var="num" id="theRepeat" first="5">
<apex:outputText value="{!num}" id="theValue"/><br/>
</apex:repeat>
</apex:page>
Output:
Apex Code: DisplayElements
public class DisplayElements {
public List<Integer> lstNumber {get;set;}
public DisplayElements() {
lstNumber = new List<Integer>();
for(integer i=1; i<20; i++) {
lstNumber.add(i);
}
}
}
Visualforce Page:
<apex:page controller="DisplayElements" id="thePage">
<b>Elements after skipping first 5 elements:</b><br/>
<apex:repeat value="{!lstNumber}" var="num" id="theRepeat" first="5">
<apex:outputText value="{!num}" id="theValue"/><br/>
</apex:repeat>
</apex:page>
Output:
Display first 5 elements of a list in visualforce page - Salesforce Globe For You
Display first 5 elements of a list in visualforce page
We can display first specified number of elements using rows attribute of <apex:repeat> tag.
Apex Code: DisplayFirst5Element
public class DisplayFirst5Element {
public List<Integer> lstNumber {get;set;}
public DisplayFirst5Element() {
lstNumber = new List<Integer>();
for(integer i=1; i<20; i++) {
lstNumber.add(i);
}
}
}
Visualforce Page: DisplayFirst5Element
<apex:page controller="DisplayFirst5Element" id="thePage">
<b>First 5 Elements are:</b><br/>
<apex:repeat value="{!lstNumber}" var="num" id="theRepeat" rows="5">
<apex:outputText value="{!num}" id="theValue"/><br/>
</apex:repeat>
</apex:page>
Output:
We can display first specified number of elements using rows attribute of <apex:repeat> tag.
Apex Code: DisplayFirst5Element
public class DisplayFirst5Element {
public List<Integer> lstNumber {get;set;}
public DisplayFirst5Element() {
lstNumber = new List<Integer>();
for(integer i=1; i<20; i++) {
lstNumber.add(i);
}
}
}
Visualforce Page: DisplayFirst5Element
<apex:page controller="DisplayFirst5Element" id="thePage">
<b>First 5 Elements are:</b><br/>
<apex:repeat value="{!lstNumber}" var="num" id="theRepeat" rows="5">
<apex:outputText value="{!num}" id="theValue"/><br/>
</apex:repeat>
</apex:page>
Output:
Friday, September 27, 2019
How to get package url created in salesforce
How to get package url created in salesforce
Step 1) Go to Setup --> create --> packages
Click on the package name for which we need package url
Step 2) Below package, we have 2 sub tabs 1) components 2)versions
click on particular version number as shown in the image below.
You will find the installation url as shown in the below.
Step 1) Go to Setup --> create --> packages
Click on the package name for which we need package url
Step 2) Below package, we have 2 sub tabs 1) components 2)versions
click on particular version number as shown in the image below.
You will find the installation url as shown in the below.
Sunday, September 1, 2019
Wednesday, August 28, 2019
Surprise your loved ones with birthday wishes in different way using salesforce
Do you want to see preview ? Demo
Please contact me for backend code through 'YOU ASK WE ANSWER' form on the right side of my blog.
Sunday, August 25, 2019
How to rotate an image in visualforce page
How to rotate an image in visualforce page
Sample VF Code:
<apex:page showHeader="false" sidebar="false">
<html>
<head>
<title>Image Rotation</title>
<style>
body {
background: gold;
}
.loadingImage {
border-top:5px solid green;
border-bottom:5px solid green;
border-left:5px solid green;
border-right:5px solid green;
border-radius:50%;
left:45%;
top:25%;
width:200px;
height:200px;
background:black;
animation:spin 10s linear infinite;
position:absolute;
}
.circularImage {
border-radius:50%;
width:200px;
height:200px;
}
@-webkit-keyframes spin {
0% {
transform:rotate(0deg);
}
100% {
transform:rotate(360deg);
}
}
</style>
</head>
<body>
<div>
<p style="font-size: 18px;text-align: center;color: green;"> Image is Rotating in Visualforce Page</p>
<br/>
<div>
<img class="loadingImage circularImage" src="Logo URL"/>
</div>
</div>
</body>
</html>
</apex:page>
Output:
Demo
Sample VF Code:
<apex:page showHeader="false" sidebar="false">
<html>
<head>
<title>Image Rotation</title>
<style>
body {
background: gold;
}
.loadingImage {
border-top:5px solid green;
border-bottom:5px solid green;
border-left:5px solid green;
border-right:5px solid green;
border-radius:50%;
left:45%;
top:25%;
width:200px;
height:200px;
background:black;
animation:spin 10s linear infinite;
position:absolute;
}
.circularImage {
border-radius:50%;
width:200px;
height:200px;
}
@-webkit-keyframes spin {
0% {
transform:rotate(0deg);
}
100% {
transform:rotate(360deg);
}
}
</style>
</head>
<body>
<div>
<p style="font-size: 18px;text-align: center;color: green;"> Image is Rotating in Visualforce Page</p>
<br/>
<div>
<img class="loadingImage circularImage" src="Logo URL"/>
</div>
</div>
</body>
</html>
</apex:page>
Output:
Demo
Saturday, August 24, 2019
How to display image as circular or rounded in visualforce page salesforce
How to display image as circular or rounded in visualforce page salesforce
Sample Visualforce Page Code:
<apex:page >
<html>
<head>
<style>
.circularImage {
border-radius:50%;
width:350px;
height:350px;
}
</style>
</head>
<body>
<br/>
<img class="circularImage" src="imageURL"/>
</body>
</html>
</apex:page>
Output:
Sample Visualforce Page Code:
<apex:page >
<html>
<head>
<style>
.circularImage {
border-radius:50%;
width:350px;
height:350px;
}
</style>
</head>
<body>
<br/>
<img class="circularImage" src="imageURL"/>
</body>
</html>
</apex:page>
Output:
Wednesday, August 7, 2019
How to link contact with Account in Salesforce
How to link contact with Account in Salesforce
We can use AccountId field of contact to associate contact with particular Account.
check the below link to see the fields of contact object for better idea
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contact.htm
Example: Run the following code in Execute anonymous window.
Account objAccount = new Account();
objAccount.Name ='Test Account';
insert objAccount;
system.debug('Account:'+objAccount);
Contact objContact = new Contact();
objContact.lastName ='Last Name';
objContact.firstName = 'First Name';
objContact.AccountId = objAccount.Id;
insert objContact;
system.debug('Contact:'+objContact);
Output: It creates Account and then it associates this Account with the newly created contact.
Happy Coding !!!
Friday, August 2, 2019
Friday, July 26, 2019
How to retrieve LWC components from salesforce instance to Visual studio code tool - Salesforce Globe For You
How to retrieve LWC components from salesforce instance to Visual studio code tool
Step 1: Open the vs code editor and open the commands palette by pressing command+shift +P in Mac.
The following screen appears.
Step 2: Type the command :SFDX create project with Manifest and press enter.
It will ask for project alias.give name and then press enter.It will ask for the folder to store in your local directory(in your local machine laptop or system).
Give the folder and then the project creation will be done as shown in the screen below.
Step 3: Press command+shift+P and then type the following command :SFDX Authorize an org and press enter.
It will ask for alias and then it will be redirected the salesforce login page .
Select the salesforce instance from which we need to retrieve LWC components.
Step 4:Under manifest file, click on package.xml file.
Now if you right click on package.xml it will show you the option 'SFDX:Retrieve source in Manifest from Org'
Select that option, all the required files will get retrieved to your source folder.
Now you can go back to LWC folder and observe all required components are retrieved.
Enjoy..
Step 1: Open the vs code editor and open the commands palette by pressing command+shift +P in Mac.
The following screen appears.
Step 2: Type the command :SFDX create project with Manifest and press enter.
It will ask for project alias.give name and then press enter.It will ask for the folder to store in your local directory(in your local machine laptop or system).
Give the folder and then the project creation will be done as shown in the screen below.
Step 3: Press command+shift+P and then type the following command :SFDX Authorize an org and press enter.
It will ask for alias and then it will be redirected the salesforce login page .
Select the salesforce instance from which we need to retrieve LWC components.
Step 4:Under manifest file, click on package.xml file.
Now if you right click on package.xml it will show you the option 'SFDX:Retrieve source in Manifest from Org'
Select that option, all the required files will get retrieved to your source folder.
Now you can go back to LWC folder and observe all required components are retrieved.
Enjoy..
Saturday, July 20, 2019
Salesforce playground where we can write code and test functionality on the GO
Salesforce playground where we can write code and test functionality on the GO
URL :Salesforce PlayGround
Salesforce playground is a place where we can write ,test and practise our coding on the go.
URL :Salesforce PlayGround
Salesforce playground is a place where we can write ,test and practise our coding on the go.
URL of Sample Gallary Salesforce.
URL of Sample Gallary Salesforce.
Sample gallary is a place where all the sample apps are located.We can get the sample codes related to various funtcionalities that we can directly copy and use in our app development.
URL: Sample Gallary
Sample gallary is a place where all the sample apps are located.We can get the sample codes related to various funtcionalities that we can directly copy and use in our app development.
URL: Sample Gallary
Thursday, July 18, 2019
How to check if the logged in user has access to a group in visualforce page
How to check if the logged in user has access to a group in visualforce page
We can check whether the logged-in user has access to a group or not with the below code
Boolean hasUserAccess = false;
List<GroupMember> lstGroupMember = new List<GroupMember>();
lstGroupMember = [select GroupId,group.name, group.DeveloperName from GroupMember where groupId='00G0o000003nffh' and UserOrGroupId=:userInfo.getUserId()];
if(lstGroupMember.size() >0) {
hasUserAccess = true;
}
where '00G0o000003nffh' is the ID of the public group created.
Example:
Apex Class: LoggedInUserGroupAccessController
public class LoggedInUserGroupAccessController {
public Boolean hasUserAccess {get;set;}
public LoggedInUserGroupAccessController() {
hasUserAccess = false;
List<GroupMember> lstGroupMember = new List<GroupMember>();
lstGroupMember = [select GroupId,group.name, group.DeveloperName from GroupMember where groupId='00G0o000003nffh' and UserOrGroupId=:userInfo.getUserId()];
if(lstGroupMember.size() >0) {
hasUserAccess = true;
}
}
}
Visualforce Page: LoggedInUserAccessInVFPage
<apex:page controller="LoggedInUserGroupAccessController">
<apex:outputPanel rendered="{!if(hasUserAccess == true,true,false)}">Logged In User has access</apex:outputPanel>
</apex:page>
Output:
We can check whether the logged-in user has access to a group or not with the below code
Boolean hasUserAccess = false;
List<GroupMember> lstGroupMember = new List<GroupMember>();
lstGroupMember = [select GroupId,group.name, group.DeveloperName from GroupMember where groupId='00G0o000003nffh' and UserOrGroupId=:userInfo.getUserId()];
if(lstGroupMember.size() >0) {
hasUserAccess = true;
}
where '00G0o000003nffh' is the ID of the public group created.
Example:
Apex Class: LoggedInUserGroupAccessController
public class LoggedInUserGroupAccessController {
public Boolean hasUserAccess {get;set;}
public LoggedInUserGroupAccessController() {
hasUserAccess = false;
List<GroupMember> lstGroupMember = new List<GroupMember>();
lstGroupMember = [select GroupId,group.name, group.DeveloperName from GroupMember where groupId='00G0o000003nffh' and UserOrGroupId=:userInfo.getUserId()];
if(lstGroupMember.size() >0) {
hasUserAccess = true;
}
}
}
Visualforce Page: LoggedInUserAccessInVFPage
<apex:page controller="LoggedInUserGroupAccessController">
<apex:outputPanel rendered="{!if(hasUserAccess == true,true,false)}">Logged In User has access</apex:outputPanel>
</apex:page>
Output:
Friday, July 12, 2019
How to rename trialhead playground name
How to rename trialhead playground name
Problem: I want to rename the trial-head playground name so that its easy for me on which trial-head playground i need to work.
Solution:
Step1 : Go the trial-head module and login.
In the dropdown where it displays all trial-heads, select 'Manage my hand-on orgs' value as shown below.
Step2: A page will be displayed as shown in the image below.
Select pencil icon next to trailhead playground which you want to rename and do the change.
once you rename, click save to save the changes.
Thats it.Now you can see the name in the dropdown if you refresh the page.
Problem: I want to rename the trial-head playground name so that its easy for me on which trial-head playground i need to work.
Solution:
Step1 : Go the trial-head module and login.
In the dropdown where it displays all trial-heads, select 'Manage my hand-on orgs' value as shown below.
Step2: A page will be displayed as shown in the image below.
Select pencil icon next to trailhead playground which you want to rename and do the change.
once you rename, click save to save the changes.
Thats it.Now you can see the name in the dropdown if you refresh the page.
Saturday, June 29, 2019
Could not create Apex Class .The Apex class " AccountController" is not a legal name.
Could not create Apex Class .The Apex class " AccountController" is not a legal name.
Problem: While creating apex class, we sometimes encounter this error.
Solution: While creating Apex class, if we give some whitespace before class name, then it will give the above error.
Please check if you gave some whitespace before class name by mistake.This can be of the reason for this error.
Problem: While creating apex class, we sometimes encounter this error.
Solution: While creating Apex class, if we give some whitespace before class name, then it will give the above error.
Please check if you gave some whitespace before class name by mistake.This can be of the reason for this error.
Tuesday, May 28, 2019
Tab is created for Custom Object but Not able to see the tab in salesforce instance
Tab is created for Custom Object but Not able to see the tab in salesforce instance
Problem: Tab is created for custom object but not able to see that tab to create records in it
Solution: Go to logged in user profile and go the custom Tab settings as shown in the image below.
If the picklist value selected for that particular tab is 'Tab Hidden',then that tab won't be visible.
Change the setting to 'Default On' to make that tab appear in the salesforce instance.
Problem: Tab is created for custom object but not able to see that tab to create records in it
Solution: Go to logged in user profile and go the custom Tab settings as shown in the image below.
If the picklist value selected for that particular tab is 'Tab Hidden',then that tab won't be visible.
Change the setting to 'Default On' to make that tab appear in the salesforce instance.
Friday, May 17, 2019
How to query field history of a particular field of an object Salesforce.
Assume we need to query history of status__c field of Suggestion__c object.
Also assume 'a0H0o00000iFDkU' is the id of the suggestion record created.
required SOQL will be as follows.
List<Suggestion__c> lstSuggestion = [SELECT Name, Status__c , (SELECT OldValue, NewValue FROM Histories where field='Status__c')
FROM Suggestion__c where id=:'a0H0o00000iFDkU'];
system.debug('Suggestion Status Field History:'+lstSuggestion[0].Histories);
Output:
Also assume 'a0H0o00000iFDkU' is the id of the suggestion record created.
required SOQL will be as follows.
List<Suggestion__c> lstSuggestion = [SELECT Name, Status__c , (SELECT OldValue, NewValue FROM Histories where field='Status__c')
FROM Suggestion__c where id=:'a0H0o00000iFDkU'];
system.debug('Suggestion Status Field History:'+lstSuggestion[0].Histories);
Output:
How to enable field history tracking for custom object Salesforce
Here in this example we are going to enable field history tracking for Suggestion custom object
Open the custom object as shown below.
Click on 'Edit' button of that custom object present at the top.
In the Optional Features section, enable 'Tracking Field History' by marking the check box of it as shown in the image below.
Once you save the object, you can see 'set History Tracking' button is enabled as shown in the image below.
Once you click on 'Set History Button' ,a screen will be displayed with all fields of that object. Select the required fields for tracking and save.
That's it.Enjoy !!!
Open the custom object as shown below.
Click on 'Edit' button of that custom object present at the top.
In the Optional Features section, enable 'Tracking Field History' by marking the check box of it as shown in the image below.
Once you save the object, you can see 'set History Tracking' button is enabled as shown in the image below.
Once you click on 'Set History Button' ,a screen will be displayed with all fields of that object. Select the required fields for tracking and save.
That's it.Enjoy !!!
Thursday, May 16, 2019
How to create custom field in Lightning Experience salesforce.
Go to Setup --> object Manager Tab
Once you click on 'object Manager Tab',it will display the follow screen which shows all the objects in the instance.
Click on the label of particular object on which you want to create custom field.Here I wanted to create custom field on Expense__c object.
So I clicked on 'Expense' label.
The following screen appears which shows all the details of that particular Expense object.
Click on 'Fields & Relationships' tab, it will show all the fields.
Now click on 'New' button to create new custom field.
Once you click on 'object Manager Tab',it will display the follow screen which shows all the objects in the instance.
Click on the label of particular object on which you want to create custom field.Here I wanted to create custom field on Expense__c object.
So I clicked on 'Expense' label.
The following screen appears which shows all the details of that particular Expense object.
Click on 'Fields & Relationships' tab, it will show all the fields.
Now click on 'New' button to create new custom field.
Saturday, April 27, 2019
How to determine Inbound or Outbound webservice in salesforce
Solution:Determining Inbound or outbound web service is very easy.
If the salesforce exposes SOAP or REST web service and any external system consume it to get required data from salesforce, then it is Inbound call to Salesforce.
If the salesforce consumes any external system web service to get required data to salesforce, then its Outbound Call to Salesforce.
In the Inbound Web service, Salesforce will be the publisher and external system will be consumer of web service and in Outbound web service its reverse i.e salesforce will be consumer and external system will be the publisher of web service.
If the salesforce exposes SOAP or REST web service and any external system consume it to get required data from salesforce, then it is Inbound call to Salesforce.
If the salesforce consumes any external system web service to get required data to salesforce, then its Outbound Call to Salesforce.
In the Inbound Web service, Salesforce will be the publisher and external system will be consumer of web service and in Outbound web service its reverse i.e salesforce will be consumer and external system will be the publisher of web service.
Friday, February 15, 2019
How to get color code directly from web Page
Solution: Use the ColorZilla Chrome extension. ColorZilla Link
Friday, January 18, 2019
Sort SFDC records based on multiple fields in SOQL query - Salesforce Globe For You
Problem: We have a requirement where we need to sort lead records based on Company field first and then based on Name.
Solution: Yes, we can sort records based on multiple fields as well.
List<Lead> lstLead = new List<Lead>();
lstLead = [Select id,company,name from Lead where name != null order by Company Asc,name ASC];
system.debug('Sorted Lead Records'+lstLead);
Solution: Yes, we can sort records based on multiple fields as well.
List<Lead> lstLead = new List<Lead>();
lstLead = [Select id,company,name from Lead where name != null order by Company Asc,name ASC];
system.debug('Sorted Lead Records'+lstLead);
Monday, January 14, 2019
Subscribe to:
Posts (Atom)
-
How to retrieve validation rules using package.xml in vs code salesforce - Salesforce Globe For You Problem : Assume a validation Mobile_...
-
Assume AccountPage is an apex class in salesforce. Sample Xml file : <?xml version="1.0" encoding="UTF-8"?> ...