Salesforce UNIXTIMESTAMP Function

The Salesforce UNIXTIMESTAMP function is used to retrieve the number of seconds that have elapsed since January 1, 1970 (Coordinated Universal Time or UTC).

The UNIXTIMESTAMP function in Salesforce is a common topic of inquiry among learners who are aspiring Salesforce admins. Unfortunately, there is no online article that helps you understand.

Don’t worry; I’m here, your expert Salesforce administrator. I will help you to get complete knowledge about this function with its syntax, example, use case, etc.

In this Salesforce tutorial, I am going to cover each and every concept of the function in detail with how to use the UNIXTIMESTAMP in Salesforce Lightning and Salesforce Classic.

Salesforce UNIXTIMESTAMP Function

What do we mean by the UNIXTIMESTAMP function in Salesforce?

The UNIXTIMESTAMP function is one of the Salesforce Date and Time functions that is used to deal with the date and time manipulations in Salesforce.

The UNIXTIMESTAMP function in Salesforce is used to return the number of seconds 1 Jan 1970 for the given date or the number of seconds in the day for a time.

What is the main purpose of the UNIXTIMESTAMP function?

The main purpose of the function in Salesforce is to give a standardized representation of time by returning the number of seconds from the timestamp, which is January 1, 1970, 00:00:00 UTC.

Here, this timestamp is commonly called a UNIX timestamp or epoch time.

What is the advantage of the Salesforce UNIXTIMESTAMP function?

If we talk about the advantage of the function it is that the function is independent of the any time zones. The function works on Coordinated Universal Time (UTC), so it removes the different time zones and daylight-saving time changes.

How can we write the Salesforce UNIXTIMESTAMP function?

The following is the syntax for the UNIXTIMESTAMP function in Salesforce:

UNIXTIMESTAMP(date/time)

Here:

  • date/time: The date/time parameter specifies the date, date/time, or time expression.

Additionally, the function returns the number of seconds since 1 Jan 1970. Make sure that you have chosen the “Number” as the formula return as it results in number format.

Where can we use the UNIXTIMESTAMP function in Salesforce?

We can use this function when we want to integrate Salesforce with any external system; at that moment, it provides a standardized format for representing time.

We can use it to make sure that there is consistent time on different platforms. It is even used while calculating time differences between two Date, DateTime, or Time values. as it easily gives the result in seconds.

Example of UNIXTIMESTAMP function in Salesforce

The below given-table explains the examples related to the UNIXTIMESTAMP function in Salesforce so we can easily understand how it works.

InputOutput
UNIXTIMESTAMP(1/26/2024)1,706,227,200
UNIXTIMESTAMP(3:00:00 AM)10,800
UNIXTIMESTAMP(2/10/2024, 2:00 PM)1,707,602,400
UNIXTIMESTAMP(6/22/2024)1,719,014,400

In the table, the “Input” column indicates the date, time, or date/time field provided as the parameter in the function by the user. On the other hand, the “Output” column illustrates the outcome generated by the UNIXTIMESTAMP function in Salesforce.

How to use UNIXTIMESTAMP Function in Formula Field in Salesforce Lightning

We can use the UNIXTIMESTAMP function in various scenarios, and one of them is that we want to show an alert that a campaign is about to start within a specified timeframe.

We will go through a step-by-step implementation of how to use the UNIXTIMESTAMP function to achieve the above-defined scenario in Salesforce Lightning. Below are the steps:

Step-1: Login to the Salesforce account in the Lightning Experience with the help of your administrator credentials.

If we are on the default home page, click on the “Gear” icon on the top right side, and from the dropdown, select the “Setup” option. But if we are already on “Home Setup”, we can skip this step.

See also  OR Function in Salesforce

Step-2: Click on the “Object Manager” option in the navigation bar. Then, on the right side, in the “Quick Find,” search for the object in which we want to use the function.

According to the scenario, we need to search and choose the “Campaign” object.

Salesforce UNIXTIMESTAMP Function

Step-3: In this step, on the left section of the page, click on the “Fields & Relationships” option. Then, click on the “New” button at the right to create a new field.

Salesforce UNIXTIMESTAMP Function Example

Step-4: Choose the field type for specifying the type of information we want to store. As we are using the function, as per the scenario, click on the radio button next to the “Formula” data type.

Then, click on the “Next” to move to the next step.

Step-5: Enter the “Field Label” and then click on the “Field Name”, we see that it t automatically fills up based on the field label.

According to the scenario, we can type “Campaign Start Alert” as the field label.

Move downward and select the formula return type from the list of available data types. By the scenario, click on the radio button next to the “Text” data type.

After that, click on the “Next” button.

UNIXTIMESTAMP Function in Salesforce Example

Step-6: In this step, to enter the formula, click on the “All Functions Categories” dropdown and choose the required function category. Following the scenario, we need to select the “Date & Time” category.

Now, search for the “UNIXTIMESTAMP” function and click on it. Then, click on the “Insert Selected Function” button under the functions section.

After that, replace the parameters of the functions with the functions, fields, or operators and formula according to the scenario.

For example, according to the scenario, we are creating a formula field that shows the alert of the campaign that it is going to start soon. The formula for the scenario is given below:

IF(UNIXTIMESTAMP(Start_Date_Time__c) - UNIXTIMESTAMP(NOW()) < 3600, "Starting Soon", "Campaign Start Later")

Here is the detailed description of the formula:

  • UNIXTIMESTAMP(Start_Date_Time__c): Here, we use the UNIXTIMESTAMP function with the Start_Date_Time__c field to convert the start date time field into a Unix timestamp.
  • UNIXTIMESTAMP(NOW()): Here, we use the UNIXTIMESTAMP function with the NOW function that is used to convert the current date and time into a Unix timestamp.
  • UNIXTIMESTAMP(Start_Date_Time__c) – UNIXTIMESTAMP(NOW())) < 3600: Here, we calculate the difference between the Unix timestamps of the campaign start date time and the current date time. If this difference is less than 3600 seconds (1 hour), then the condition is true.
  • IF Function: Here, we use the IF function that checks if the condition is true; the result is “Starting Soon”; otherwise, it gives a string as “Campaign Start Later”.

After entering the formula, click on the “Check Syntax” button to verify the formula. If there is no error, we will get the notification “No Syntax error in merge fields or function”. Otherwise, we will get an error.

If we want, we can enter the “Description” and “Help Text” to provide additional information. To proceed, click on the “Next” button.

Salesforce Lightning UNIXTIMESTAMP Function

Step-7: Select the profiles to whom we want to grant field-level security edit access. Some profiles may be selected by default.

If we want to grant access to additional profiles, click on the visible checkbox next to the respective profile names. To quickly select all the profiles, click on the checkbox next to the “Visible” option.

If we want to grant read-only access, check the “Read Only” checkbox next to the respective profile name.  Click on the “Next” button to move to the next step.

Salesforce Lightning UNIXTIMESTAMP Function Example

Step-8: In this step, we need to setup the page layout. Here, we will get the list of the page layouts on which this field is visible. By default, all the page layouts are selected.

See also  Salesforce MINUTE Function

But if we do not want to add the custom field on any one of them, unselect them. After that, click on the “Save” button to complete the process.

UNIXTIMESTAMP Function in Salesforce Lightning

Step-9: Once the formula field is created, we will be able to use this formula field. Let’s see how it works:

  • Open the “Campaign” item, create a new campaign with the field “Start Date Time” and save the campaign.
  • Now move to the “Details Section” where we will see the “Campaign Start Alert” field with the output of the UNIXTIMESTAMP function.

Let’s understand it with the example:

  • Suppose we have created the campaign with the start date time “1/20/2024, 8:30 AM” and today’s date time “1/20/2024, 10:55 AM”. After the execution of the formula, we will get the output for the field campaign start alert “Starting Soon”.
UNIXTIMESTAMP Function in Salesforce Lightning Example

With this, we have learned how to use the UNIXTIMESTAMP function in the formula field in Salesforce Lightning.

How to use UNIXTIMESTAMP Function in Salesforce Classic

Let’s explore the steps of how to use the UNIXTIMESTAMP function in the formula field in Salesforce Classic with the scenario.

Consider a scenario where we want to set reminders for upcoming contract renewals. Here are the steps to achieve it:

Step-1: Sign in to your Salesforce account with your credentials as the administrator and make sure we are in the Classic Interface; if not, “Switch to Salesforce Classic.”

Step-2: Click on the “Setup” option located at the top right side of the page between your name or the help option. If it is not located here, click on the dropdown arrow next to your name, and we will get it.

Step-3: Search for the object in the “Quick Find” box in which we want to use the UNIXTIMESTAMP function. We will get the object in the “Build” section under the “Customize” dropdown.

Now, from the expanded list, click on the “Fields” option. Following the scenario, we need to choose the “Contracts” object.

Salesforce Classic UNIXTIMESTAMP Function

Step-4: Scroll down to the “Contract Custom Fields & Relationships” section. Click on the “New” button to create the new field in Salesforce Classic.

Salesforce Classic UNIXTIMESTAMP Function Example

Step-5: From the list of the data types, specify the type of information that the custom field will contain by clicking on the radio button next to it.

According to the scenario, you need to choose the “Formula” as the data type. After that, click on the “Next” button to proceed to the next step.

UNIXTIMESTAMP Function in Salesforce

Step-6: Enter the “Field Label” for the new custom formula field and then click on the “Field Name”, it gets automatically filled up based on the field label.

As per the scenario, we can enter the field label “Contract Renewal Reminder”.

Scroll down to the formula return type and choose the appropriate data type for the formula return type by clicking on the radio button next to the desired data type.

In accordance with the scenario, we can select the “Text” as the data type. After that, click on “Next” and move to the next step.

UNIXTIMESTAMP Function in Salesforce Classic

Step-7: In this step, it is time to define the formula in the formula sub-tab; click and expand the “All Function Categories” dropdown. Search and select the category of the function in which the function resides.

In accordance with the scenario, click on the “Date & Time” category. Now select the function from the category and click on it. By the scenario, we need to click on the “UNIXTIMESTAMP” function.

See also  LEFT Function in Salesforce

To enter the formula in the subtab, click the “Insert Selected Function” button located beneath the function section. Now, replace the parameters with the appropriate fields and operators to create the formula.

As per the scenario, we need to set the reminder for upcoming contract renewals. Below is the formula for that:

IF(UNIXTIMESTAMP(EndDate) - UNIXTIMESTAMP(TODAY()) < 2592000, "Renewal Due Soon", "Contract is Activated")

Here is the detailed description of the formula:

  • UNIXTIMESTAMP(EndDate): The function used to convert “EndDate” into a Unix timestamp.
  • UNIXTIMESTAMP(TODAY()): It is used to convert the current date without considering the time component into a Unix timestamp.
  • (UNIXTIMESTAMP(EndDate) – UNIXTIMESTAMP(TODAY())) < 2592000: Here, we calculate the difference between the Unix timestamps of the contract’s end date and the current date. If this difference is less than 2,592,000 seconds (30 days), then the condition is true.
  • IF function: If the function is used to check the condition and used to give the result as per the condition. Here, if the condition is true, the result is “Renewal Due Soon”; otherwise, if the condition is false, the result is “Contract is Activated”.

To verify whether the formula is correct or has some error, click on the “Check Synatx” button. If there is an error, the error message will be displayed. Otherwise, it displays “No Synatx error in merge fields or function”.

Optionally, you can also enter the “Description” and “Help Text” to provide more information about the formula. After that, click on the “Next” button to proceed.

UNIXTIMESTAMP Function in Salesforce Classic Example

Step-8: Establish field-level security by selecting the profiles to which you want to grant edit access to this field via field-level security. The field will be hidden from all those profiles that you don’t have added to the field-level security.

To proceed to the next step, click on the “Next” button.

How to use UNIXTIMESTAMP Function in Salesforce

Step-9: Select the page layout that should include this field. The field will be added as the last field in the first 2 column section of these page layouts.

The field will not appear on any pages if we do not select a layout. If we want to change the location of the field on the page, we will need to customize the page layout.

Click on the “Save” button to complete the process of creating a formula field.

How to use UNIXTIMESTAMP Function in Salesforce Example

Step-10: Now we can be able to use the formula field. Let’s see the steps:

  • Open the “Contract” object in the Salesforce Classic, create a new contract with the field “End Date,” and save it.
  • Move to the detail section and locate the “Contract Renewal Reminder”. Here, we will see the output of the UNIXTIMESTAMP function based on the result of the formula.

Let’s understand it with the example:

  • Suppose we have created the contract with the field end date “06/15/2024” and today’s date “01/20/2024”. After execution of the formula, we will get the output of the formula field contract renewal reminder “Contract is Activated”.
Example of UNIXTIMESTAMP Function in Salesforce

With this, we have learned how to use the UNIXTIMESTAMP function in Salesforce Classic.

Conclusion

In a nutshell, throughout this article, we have learned about the UNIXTIMESTAMP function in Salesforce. We have also learned the syntax, use case, and examples related to the function.

Moreover, we have explored step-by-step how to use the UNIXTIMESTAMP function in Salesforce Lightning and Salesforce Classic with examples.

You may like to read: