Salesforce PRIORVALUE Function

The PRIORVALUE function in Salesforce is used to monitor field modifications within Salesforce records, which offers invaluable information on how data changes over time.

In this Salesforce tutorial, I will walk you through the syntax, use cases, considerations, and other aspects of the PRIORVALUE function as a Salesforce Administrator.

Additionally, I will guide you through the use of the PRIORVALUE function in Salesforce Lightning and Salesforce Classic step-by-step with real business-related scenarios.

PRIORVALUE Function in Salesforce

What is the PRIORVALUE Function in Salesforce?

One of the Salesforce logical functions that is used to monitor fields and their updates in the Salesforce org is the PRIORVALUE function.

The PRIORVALUE function in Salesforce is used to get a field’s previous value prior to a modification. By comparing the field’s current value with the previous value (before it was updated), users can enable to perform dynamic processes and actions based on changes in data.

How does the PRIORVALUE Function work in Salesforce?

The PRIORVALUE function works when the record is updated. The function noted the value of the field before the update occurred.

After that, the prior value can then be utilized to make calculations based on the change or to trigger specific actions in workflow rules, validation rules, formula fields, and other automation processes.

Let’s understand with an example: Suppose you want to create a validation rule in Salesforce that checks whether the previous value of the phone field is different from the current value; if yes, it prevents the record from being saved.

For this, you need to use the PRIORVALUE function given as follows:

PRIORVALUE(Phone__c) != Phone__c

If the previous value of the phone field was “123-456-7890” and the new value of the phone field is “987-654-3210”, the validation rule checks that the previous value is not equal to the current value, so it stops the record from being saved.

What is the Salesforce syntax for writing the PRIORVALUE Function?

The following is the PRIORVALUE function’s syntax:

PRIORVALUE(field)

Parameter:

  • field: This field parameter specifies the field whose previous value you want to retrieve. It can be any standard or custom field on the object.

Return:

  • The function returns the previous value for the field.

What are the use cases of the Salesforce PRIORVALUE Function?

The PRIORVALUE function is ready to work in the following use cases in Salesforce:

Workflow Rules: Workflow rules can use PRIORVALUE to perform action when particular fields are modified. For instance, when specific fields are updated, you can update related records or send automated messages. It can be utilized if the evaluation criterion is set to “Evaluate the rule when a record is created, and every time it’s edited”.

Validation Rule: Data validation criteria can be implemented with the help of the PRIORVALUE function. It evaluates changes made to a field and ensures that data meets specific criteria before it is saved.

Assignment Rules: The PRIORVALUE function can be used to compare the previous value of a field with its current value in the assignment rules and assign records to specific users or queues if certain assignment criteria are met.

Field Updates: In Salesforce, the PRIORVALUE function can also be used in field update actions to define conditions under which a field should be updated based on its previous value.

Formula Criteria in Process Builder: PRIORVALUE can be used within formula criteria to access field changes and trigger follow-up activities when establishing criteria for actions to be performed and input values to be set in Process Builder.

Are there any considerations we should know while using Salesforce’s PRIORVALUE Function?

There are some considerations that you should keep in mind when using the PRIORVALUE function in Salesforce, given below:

1. The PRIORVALUE function doesn’t return default values. This means that when a record is created, and a field is empty, the function will not return null. It returns the value that was present before the record was created.

2. If the record is newly created, that means there was no previous value in the field; at that moment, the PRIORVALUE function will return the value that the field had during the creation of the record.

For example, if you create a new account record with the name “Xmexz” and change the name to “AbcCrop”, the PRIORVALUE function will return “Xmexz” until the record is edited again.

3. When using PRIORVALUE to get the previous value of a picklist field, use it with the ISPICKVAL function. It allows you to compare the previous picklist value with a specific picklist value to determine if a change occurred.

The syntax is as follows:

ISPICKVAL(PRIORVALUE
(picklist_field), 
text_literal)

4. In multi-select picklist fields, you can use the PRIORVALUE function inside the INCLUDES function to check if the previous value included a specific value.

See also  CASESAFEID() Function in Salesforce

The syntax is as follows:

INCLUDES(
   PRIORVALUE(multiselect_picklist_field),
text_literal
)

With this, you have learned the syntax, usage cases, and considerations of the PRIORVALUE function in Salesforce.

Difference between ISCHANGED and PRIORVALUE Function in Salesforce

The below given table compares and summarizes the key differences between the ISCHANGED and PRIORVALUE functions in Salesforce.

ParametersISCHANGED FunctionPRIORVALUE Function
PurposeThe Salesforce ISCHANGED function is used to determine if a field has changed from its prior value.The Salesforce PRIORVALUE function is used to determine if a field has changed from its prior value
SyntaxISCHANGED(field)PRIORVALUE(field)
UsageThe ISCHANGED function is used to evaluate if a field has been modified.The PRIORVALUE function is used to compare the current value of a field with its previous value.
ReturnsThe function returns results in a boolean true if the field has changed; otherwise, it is false.The function returns the previous value of the field
Comparison with Default ValuesNothing will do. It only compares the current value with the previous value.Nothing will do. It only compares the current value with the previous value.
Consideration with Record HistoryThe ISCHANGED function does not directly access record history to compare previous values.The PRIORVALUE function accesses record history to retrieve the previous value, providing historical context.
ExampleSuppose a scenario where you change the amount from $500 to $800.
The formula is as follows:
ISCHANGED(Amount__c)
The result is as follows:
TRUE
Suppose a scenario where you change the amount from $500 to $800.
The formula is as follows:
PRIORVALUE(Amount__c)
The result is as follows:
$500.

With this, you have explored the comparison of the ISCHANGED and PRIORVALUE functions in Salesforce in the table format.

How to use the PRIORVALUE function in Salesforce Lightning

Let’s learn to use the PRIORVALUE function in the validation rule in Salesforce Lightning with the help of an example.

Scenario: Imagine you are managing a sales team for a software company. Your opportunity process consists of several stages: Prospecting, Qualification, Negotiation, Closed Won, etc.

Consider a scenario where the opportunity is initially in the “Prospecting” stage, during which the sales representative finds potential leads and learns about their requirements.

The sales representative feels they have qualified the lead after a while and are ready to move the opportunity to the “Closed Won” stage without following the proper procedure of moving the opportunity.

Now, you, as the sales team manager, want to apply the validation rule that prevents the stage change from “Prospecting” to “Closed Won” and displays an error message.

The following are the steps to use the PRIORVALUE function to achieve the above-defined scenario in Salesforce Lightning:

1. Use your login credentials to access your Salesforce account as an administrator, then navigate to the Lightning Interface if you are not there.

Now, navigate to the setup menu by clicking on the “Gear Icon” in the top right corner of the screen and select the “Setup” option from the dropdown.

Salesforce PRIORVALUE Function

2. Locate and select the “Object Manager” tab from the navigation bar on the main setup page.

Select the object for which you want to create the validation rule. For example, according to the scenario, you want to create a validation rule for the opportunity object, so select “Opportunity” from the object list.

Salesforce PRIORVALUE Function Example

3. On the left-hand menu within the selected object, locate the “Validation Rules” tab and click on it.

After that, click on the “New” button on the top right side to create a new validation rule.

Salesforce Lightning PRIORVALUE Function

4. Enter the following details for your validation rule:

  • Rule Name: Give your validation rule a clear and descriptive name. According to the scenario, you could name the rule “Stop Stage Change Prospect to Closed Won”.
  • Description: You can enter more information about the validation rule.

Next, to make the validation rule active, check the “Active” checkbox. The rule will not be applied if this checkbox is left unchecked.

Salesforce Lightning PRIORVALUE Function Example

5. Now, you need to define the conditions that will trigger the validation rule. Use the formula editor to define the conditions and to create the formula, follow the below-given steps:

  • Click on the “All Function Categories” under the “Functions” section.
  • Choose the category from the list. According to your scenario, choose “Logical”.
  • Select the function from the list of the functions. According to your scenario, select the “PRIORVALUE”.
  • Click on the “Insert Selected Function” button located under the functions box.

According to your scenario, replace the parameters of the function with fields, operations, or functions required. The formula is as given below:

AND(
    ISPICKVAL(StageName, "Closed Won"),
    ISPICKVAL(PRIORVALUE(StageName), "Prospecting")
)

A detailed description of the formula is given below:

  • AND Function: The AND function is used to combine both conditions. It returns TRUE only if both conditions are TRUE; otherwise, it returns FALSE.
  • ISPICKVAL(StageName, “Closed Won”): The ISPICKVAL function checks if the current value of the StageName field is “Closed Won”. If it is, the function returns TRUE; otherwise, it returns FALSE.
  • ISPICKVAL(PRIORVALUE(StageName), “Prospecting”): The function checks if the previous value of the StageName field was “Prospecting” using the PRIORVALUE function. If the value was prospecting, the function returns TRUE; otherwise, it returns FALSE.

The validation rule triggers an error if all the following conditions are TRUE:

  • The current stage is “Closed Won”.
  • The previous stage was “Prospecting”.

To check if the formula is correct or contains any errors, click the “Check Syntax” button after entering the formula.

PRIORVALUE Function in Salesforce

6. Enter a clear and concise error message that will display to the user when the validation rule is triggered in the “Error Message” field.

See also  Distance() Function in Salesforce | Calculate distance between locations in Salesforce

According to your scenario, you can add the error message as given below:

You cannot change the Opportunity Stage to 'Closed Won' directly from the 'Prospecting' stage. Please follow the appropriate progression in the sales process.

Next, optionally, you can specify where the error message should be displayed in the “Error Location” field.

  • You could choose to display the error message in the field itself by checking the radio button “Field”.
  • You could choose to display the error message at the top of the page by checking the radio button “Top of Page”.

7. Click on the “Save” button to save the validation rule. The rule is now active and will trigger when the conditions you defined are met.

PRIORVALUE Function in Salesforce Example

8. After creating the validation rule, let’s test it to ensure that it works as expected.

  • Create a new or edit an existing “Opportunities” record and save it.
  • If you update the stage field from “Prospecting” to “Closed Won” then a validation error will arise.

Let’s deeply understand it with the example:

  • Suppose you are trying to update the stage from “Prospecting” to “Closed Won”.
  • As per the example defined above, the validation rule meets and sends the error message “You cannot change the Opportunity Stage to ‘Closed Won’ directly from the ‘Prospecting’ stage. Please follow the appropriate progression in the sales process”.
PRIORVALUE Function in Salesforce Lightning

With this, you have successfully learned how to use the PRIORVALUE function in the validation rule in Salesforce Lightning. Let’s proceed and learn to use the PRIORVALUE function in Salesforce Classic.

How to get the prior value of field in Salesforce Classic

Let’s take a scenario to understand to get the prior value of the formula field in Salesforce Classic.

Scenario: Suppose you want that whenever the “Email” field is updated with a new value, the previous email value should be copied to the “Alternate Email” field.

For this, you need to use the Salesforce workflow rules that update the field value. The following are the steps to use the PRIORVALUE function in Salesforce Classic using workflow rules.

1. Login to your Salesforce account by entering the username and password of the administrator in the Classic Interface.

Find and click on the “Setup” option given in the top right corner of the page.

Salesforce Classic PRIORVALUE Function

2. In the setup menu, you will find a search bar on the left sidebar. Type “Workflow Rules” into the search bar, and select “Workflow Rules” from the options given under the “Workflow & Approvals” section.

Salesforce Classic PRIORVALUE Function Example

3. On the workflow rules page, to create a new rule, click the “New Rule” button.

PRIORVALUE Function in Salesforce Classic

It shows the pop-up that you want to use the workflow rules or want to use flow builder, which is the future of low-code automation. Click on the “Continue with Workflow Rules” button.

PRIORVALUE Function in Salesforce Classic Example

4. In the “Select Object” dropdown menu, choose the object for which you want to create the workflow rule. According to the scenario, choose the “Contacts” object from the dropdown.

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

How to use the PRIORVALUE function in Salesforce

5. In the “Edit Rule” section, fill in the following details:

  • Name: Enter a descriptive name for the rule that indicates the purpose of the rule. According to the scenario, you can name them “Capture Prior Value for Alternate Email”.
  • Description: Optionally, you can enter a brief description to explain what the rule does.

6. In the “Evaluation Criteria” section, choose when the rule should be evaluated. There are three options related to it, which are given below:

  • When a record is created
  • Every time a record is created or edited
  • Created and only when a record is edited to meet the criteria

7. In the “Rule Criteria” section, define the criteria that must be met for the rule to trigger. This is done using a formula or logic based on field values.

In the “Run this rule if the” dropdown, you get options “criteria are met” and “formula evaluates to true”.

See also  Salesforce REVERSE Function | How to reverse string in Salesforce

According to your scenario, select the formula evaluation option. Now enter the formula in the formula editor by following the below-given steps:

  • Navigate to the “Functions” section and select “All Function Categories”.
  • Select the function category. As per the scenario, choose “Logical”.
  • From the list of functions, choose the desired function. As per the scenario, select “PRIORVALUE”.
  • Below the functions box, click the “Insert Selected Function” button.

Replace the function’s parameters with the necessary fields, operators, or functions based on the situation. The following is the formula according to the scenario:

PRIORVALUE(Email) <> Email

Here is a detailed explanation of the formula:

  • PRIORVALUE(Email): It is used to retrieve the previous value of the “Email” field before the record is updated.
  • <>: It is the “not equal to” operator. It compares the previous value of the “Email” field to the current value.
  • Email: It is the current value of the “Email” field.

Click on the “Check Syntax” button to check whether the formula is correct or has some error. After that, click on the “Save & Next” button to proceed.

How to use the PRIORVALUE function in Salesforce Example

8. After creating the workflow rule, the next important thing is to specify the workflow actions that will be triggered when the rule criteria are met.

For this, click the “Add Workflow Action” button to define the actions to be taken when the rule criteria are met.

Then, choose the type of action you want to perform. Here, you get the following options.

  • New Email Alert
  • New Field Update
  • New Outbound Message
  • Select Existing Action

According to the requirement of the scenario, you need to select the “New Field Update” option as you want to update the alternate email field if the workflow rule meets up.

How to use the PRIORVALUE function in Salesforce Classic

9. Define the field update, including the object associated with the workflow rule, the field to update, and the value to apply. To create a new field update, enter the following details:

  • Name: Enter the name for the field update. According to the scenario, you need to enter the name as “Update Alternate Email with Prior Email Value”.
  • Unique Name: The unique name field gets automatically populated based on the name field. If you want to change it you can change but make sure that the name must begin with a letter and use only alphanumeric characters and underscores. The name cannot end with an underscore or have two consecutive underscores.
  • Description: Optionally, provide a description that understands the purpose of this field update.
  • Field to Update: Select the field you want to update from the dropdown menu. This is the field whose value will be changed by the field update.

Then, click on the “Next” button to move.

10. If you want this field updated to change the field’s value, all workflow rules on the associated object are re-evaluated, and any workflow rules whose criteria are met as a result of the field update will be triggered, checking the “Re-evaluate Workflow Rules after Field Change” checkbox.

11. Now specify the new field value. If you choose the updated field as text type, you will get options: “A blank value (null)” and “Use a formula to set the new value”.

According to the scenario, choose the formula to set up the new value by checking the radio button. Click on the show formula editor and enter the formula using the required functions, fields, and operators, and after that, click on the “Check Syntax” button.

According to the scenario, you want to fill up the previous filled value in the updated field alternative email, so the formula will look as given below:

PRIORVALUE(Email)

Here is a detailed explanation of the formula:

  • PRIORVALUE(Email): It is used to get the previous value of the email address field.

12. Once you have configured the field update details, click on the “Save” button to save your changes.

How to use the PRIORVALUE function in Salesforce Classic Example

13. Then, click on the “Done” button given on the top right side.

How to get the prior value of field in Salesforce

14. After the rule is created and the action is configured, check the “Active” checkbox given on the top to activate it. This ensures that the rule will be evaluated and actions will be triggered according to the specified criteria.

How to get the prior value of field in Salesforce Example

15. After following up on the above-given steps, it is important to test the workflow rule to ensure it behaves as expected. Create or edit contact records that meet the rule criteria and verify that the specified actions are triggered accordingly.

Let’s understand with an example:

  • Suppose you edit the contact record whose “Email” field is already filled with the value “john@example.com”. Now you edit the record and change the email field to “john.doe@example.com”.
  • The workflow rule is triggered because the previously filled email value is not equal to the currently filled email value.
  • Therefore, the field update action updates the “Alternate Email” field with the previous email value “john@example.com”.

After this process, the Contact record will have:

  • Email: john.doe@example.com
  • Alternate Email: john@example.com
Example of PRIORVALUE Function in Salesforce

With this, you have learned the step-by-step procedure of how the PRIORVALUE function can be used in a Salesforce workflow rule to get the prior value of the field to update a field based on changes to another field.

Conclusion

The Salesforce PRIORVALUE function is a powerful function that is used to capture previous field values. You can easily use this function to enforce data integrity and enhance the efficiency of the code.

In this complete tutorial, I give detailed information about the PRIORVALUE function, including its definition, syntax, considerations, examples, etc.

Moreover, with the proper steps, you can implement this function in validation rules, workflow rules, field updates, etc. I have explained this with proper business-related examples.

You may like to read the following articles: