Salesforce Cookies #10

Creating a dynamic SOQL which includes all the fields

I have seen this question several times on developer forums and I thought it could be another addition to Salesforce cookies. If you are wondering how can you query all the fields of an object then you can use the following code snippet.

The above code snippet using describe calls to get all the fields and then created a dynamic SOQL query to get all the field values.

This is quite similar to SQL query which uses wildcard characters – SELECT * FROM Table.

Salesforce Cookies #6

Adding Loading Icon on the page for an AJAX Request ( ActionFunction, CommandButton)

It’s always good to add a loading icon on the page when you are processing something on the server side (calling an apex method ) so a user can understand that something is running in the background. Following code snippet can be used along with your apex:actionFunction or apex:commandButton.

In Classic

<apex:actionStatus id="actionStatus">
  <apex:facet name="start" >
    <img src="/img/loading.gif" />

In Lightning Experience

<apex:actionStatus id="actionStatus">
  <apex:facet name="start" >
    <div class="slds-spinner_container">
      <div class="slds-spinner--brand slds-spinner slds-spinner--small" aria-hidden="false" role="alert">
        <div class="slds-spinner__dot-a"></div>
        <div class="slds-spinner__dot-b"></div>

Using ActionStatus in CommandButton

<apex:commandButton value="Save" action="{!Save}" status="actionStatus"/>

Happy Coding 🙂

Salesforce Cookies#5

Uploading Attachment from Non-Salesforce Users ( Site or Guest Users) through sites

I came across a question on Trailblazers community where someone asked for a way to upload an attachment to a record and it should be accessible by non-salesforce users. I won’t go into much detail about that and will focus on providing the solution.

So, you just have to follow the below-mentioned steps to make it work for you.

  1. Setup a site if you haven’t already. You can get more details about this from this link.
  2. Create a VF page which lets an external user upload an attachment to Salesforce record.
  3. Add the page ( created in 2nd step) to the Site created in 1st step. Find more details here.

Visualforce page for Upload Attachment

You just need a VF page and here is the code gist which you can utilize and update as per your need.

Upload Attachment through sites

Happy coding 🙂


Salesforce Cookies #4

Never store record’s Id as a String (Variable type) if you are comparing Ids in your Apex code

If you are writing apex code then make sure that you are treating Ids’ carefully.  You should emphasize on declaring your variables of Id-type instead of String since storing record Id in a variable which type is Id will do the 15-character Id to 18-character Id conversion. Let’s take an example.

Code uses String type variable for holding record Ids
Map<String, Opportunity> mapOpportunities = new Map<String, Opportunity>();
mapOpportunities.put('006w000000tVvtt', Opp);
006w000000tVvtt - 15_Character_Opp_Id  

String oppRecordId = '006w000000tVvttAAG';
mapOpportunities.containsKey( oppRecordId ); // Will return False
006w000000tVvttAAG - 18_Character_Opp_Id

// Same as above 
String character_15_id = '006w000000tVvtt';
String character_18_id = '006w000000tVvttAAG';
character_15_id == character_18_id  // return false
Code uses Id type variable for holding record Ids
Map<Id, Opportunity> mapOpportunities = new Map<Id, Opportunity>();
mapOpportunities.put('006w000000tVvtt', Opp);
006w000000tVvtt - 15_Character_Opp_Id  
Id oppRecordId = '006w000000tVvttAAG';
// Performing containsKey operation will give you true 
// since declaring the map key as Id has done the required ID's conversion
mapOpportunities.containsKey( oppRecordId ); // Will return True
006w000000tVvttAAG - 18_Character_Opp_Id

// Same as above 
Id character_15_id = '006w000000tVvtt';
Id character_18_id = '006w000000tVvttAAG';
character_15_id == character_18_id // return true

Declaring such variables as Id will also help you to deal with invalid Ids. Happy coding!! 🙂

Salesforce Cookies #3

If there is a need of scheduling an apex job or schedulable apex class after each hour then cron expression is the best way. Cron expression allows you to schedule a job on a specific time. Following cron expression runs a job after each hour of the day.

scheduledJob j = new scheduledJob();
String sch = '0 0 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 ? * *';
System.schedule('My Job', sch, j);
Cron expression is built with following parameters - 
Seconds Minutes Hours Day_of_month Month Day_of_week Optional_year

You can Special Characters in a Cron expression such as running a job only on weekdays after each 4 hours then following Cron expression will be used.

scheduledJob j = new scheduledJob();
String sch = '0 0 0,4,8,12,16,20 ?  MON-FRI';
System.schedule('My Job', sch, j);

For more details you can check the Salesforce Article.