Reference Book
This post helps Salesforce Developers in their routine jobs. This post is a place to search for the shortcuts or code syntax which we don't usually remember and have to search on Google everytime.
1. Identifying Process Name from the Flow Version ID -
Sometimes we get an error like this one "CANNOT_EXECUTE_FLOW_TRIGGER : The record couldn’t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 301140000008sJs. Flow error messages: <b>An unhandled fault has occurred in this flow</b><br>An unhandled fault has occurred while processing the flow. Please contact your system administrator for more information. Contact your administrator for help."
This error message doesn't provide us the Process name which we can debug. But the flow version ID can be utilized to get the process name. Here is all what you need to do:
Go to Workbench --> Utilities --> Rest Explorer --> Make a GET call with this path (/services/data/v37.0/tooling/sobjects/flow/{flowID})
The Master Label value is the process name which we need.
2. Get RecordTypeId without using SOQL -
Sometimes we are so close to reach the SOQL governor limit of 100 per execution. And we don't want to waste an SOQL statement just for the Record Type ID. Use the below syntax to get the record type ID of Test record type on Account object :
Schema.SObjectType.Account.getRecordTypeInfosByName().get('Test').getRecordTypeId();
3. Date formatting on VF Page -
The date formatting is very important when it comes to display the data and avoid the full date time. We can use OutputText tag to format the date on a visualforce page.
JavaScript Remoting or JS Remoting in short is a very useful AJAX technique used in Visualforce pages to make a call to server and return the result on the page in no time. The Remoting method written in your Apex class should be annotated with @RemoteAction annotation. You can send as many parameters as you want by separating them using comma(','). The Remote Action method in the apex class should be a static method. The parameter name should be same for both VF page and Apex class.
Syntax for VF Page:
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.CONTROLLERNAME.myRemoteMethod}',param1,param2,
function(result, event){
console.log(result); /*The "result" variable will contain what you return from your remote action method written in Apex class.*/
}
);
Syntax for Apex Class:
@RemoteAction
public static Object myRemoteMethod(String param1, String param2)
{
return param1+'-'+param2;
}
5. Get the ID of selected Lookup value in VF Page -
JavaScript plays a vast role in a VF page for form validations or getting some values from Input fields. This example shows how to get the ID of the selected LookUp value in a LookUp type of Input field.
Visualforce appends "_lkid" in the ID of the inputField from which you want to get the value of ID of the selected LookUp value. We just need to make sure that we provide IDs of each parent <apex> tags of that Input Field, see the below code snippet for more clarity.
VF Page code:
<apex:page id="pageId" standardController="CustomObject__c">
<apex:form id="formId">
<apex:inputField value="{!CustomObject__c.OwnerField__c}" id="ownerId"/>
<apex:form>
</apex:page>
JavaScript Code:
<script>
window.onload = function()
{
document.getElementById('pageId:formId:ownerId_lkid').value;
};
</script>
Note: Sometimes the browser sets this value as '000000000000000' when the field is left blank, so you will have to put a check and make the value blank whenever it is set as multiple zeros.
var ownerId = document.getElementById('pageId:formId:ownerId_lkid').value;
console.log('OwnerId====',ownerId);
if(ownerId == '000000000000000')
{
ownerId = '';
}
6. Use custom Font Family in Visualforce Page:
Visualforce allows us to use custom font family in the style of HTML or Apex elements. We just need to create a font-face using CSS. Font-Family is the font name which you can provide of your own choice. Src contains the URL of the static resource in which the font file is stored. In the below example, the font file format is woff2. You can choose any format like .ttf, .woff etc.
<apex:page>
<style type="text/css">
@font-face{
font-family: 'ss-regular';
src: url("{!URLFOR($Resource.SalesforceSansFont, 'SalesforceSans-Regular.woff2')}") format('woff2');
}
</style>
<div style="font-family: ss-regular;">
</div>
</apex:page>
1. Identifying Process Name from the Flow Version ID -
Sometimes we get an error like this one "CANNOT_EXECUTE_FLOW_TRIGGER : The record couldn’t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 301140000008sJs. Flow error messages: <b>An unhandled fault has occurred in this flow</b><br>An unhandled fault has occurred while processing the flow. Please contact your system administrator for more information. Contact your administrator for help."
This error message doesn't provide us the Process name which we can debug. But the flow version ID can be utilized to get the process name. Here is all what you need to do:
Go to Workbench --> Utilities --> Rest Explorer --> Make a GET call with this path (/services/data/v37.0/tooling/sobjects/flow/{flowID})
The Master Label value is the process name which we need.
2. Get RecordTypeId without using SOQL -
Sometimes we are so close to reach the SOQL governor limit of 100 per execution. And we don't want to waste an SOQL statement just for the Record Type ID. Use the below syntax to get the record type ID of Test record type on Account object :
Schema.SObjectType.Account.getRecordTypeInfosByName().get('Test').getRecordTypeId();
3. Date formatting on VF Page -
The date formatting is very important when it comes to display the data and avoid the full date time. We can use OutputText tag to format the date on a visualforce page.
<apex:outputText value="{0, date, MM/dd/yyyy}">
<apex:param value="{!Opportunity.CloseDate}"/>
</apex:outputText>
4. JavaScript Remoting on VF Page - JavaScript Remoting or JS Remoting in short is a very useful AJAX technique used in Visualforce pages to make a call to server and return the result on the page in no time. The Remoting method written in your Apex class should be annotated with @RemoteAction annotation. You can send as many parameters as you want by separating them using comma(','). The Remote Action method in the apex class should be a static method. The parameter name should be same for both VF page and Apex class.
Syntax for VF Page:
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.CONTROLLERNAME.myRemoteMethod}',param1,param2,
function(result, event){
console.log(result); /*The "result" variable will contain what you return from your remote action method written in Apex class.*/
}
);
Syntax for Apex Class:
@RemoteAction
public static Object myRemoteMethod(String param1, String param2)
{
return param1+'-'+param2;
}
5. Get the ID of selected Lookup value in VF Page -
JavaScript plays a vast role in a VF page for form validations or getting some values from Input fields. This example shows how to get the ID of the selected LookUp value in a LookUp type of Input field.
Visualforce appends "_lkid" in the ID of the inputField from which you want to get the value of ID of the selected LookUp value. We just need to make sure that we provide IDs of each parent <apex> tags of that Input Field, see the below code snippet for more clarity.
VF Page code:
<apex:page id="pageId" standardController="CustomObject__c">
<apex:form id="formId">
<apex:inputField value="{!CustomObject__c.OwnerField__c}" id="ownerId"/>
<apex:form>
</apex:page>
JavaScript Code:
<script>
window.onload = function()
{
document.getElementById('pageId:formId:ownerId_lkid').value;
};
</script>
Note: Sometimes the browser sets this value as '000000000000000' when the field is left blank, so you will have to put a check and make the value blank whenever it is set as multiple zeros.
var ownerId = document.getElementById('pageId:formId:ownerId_lkid').value;
console.log('OwnerId====',ownerId);
if(ownerId == '000000000000000')
{
ownerId = '';
}
6. Use custom Font Family in Visualforce Page:
Visualforce allows us to use custom font family in the style of HTML or Apex elements. We just need to create a font-face using CSS. Font-Family is the font name which you can provide of your own choice. Src contains the URL of the static resource in which the font file is stored. In the below example, the font file format is woff2. You can choose any format like .ttf, .woff etc.
<apex:page>
<style type="text/css">
@font-face{
font-family: 'ss-regular';
src: url("{!URLFOR($Resource.SalesforceSansFont, 'SalesforceSans-Regular.woff2')}") format('woff2');
}
</style>
<div style="font-family: ss-regular;">
</div>
</apex:page>
First published on Blogger in 2018. Some screenshots and APIs have changed since; the code is kept as written.