Applikon Tech

Creating a Dynamic Salesforce Button for Opportunity-Related Quote (No code)…

In Salesforce CPQ, it’s possible to use a Custom Action to create a button on the Opportunity object for generating new Quotes with default field values. However, in Revenue Cloud, the core Quote object does not support Quick Actions. While you can use the Out-of-the-Box (OOTB) New Quote button available on the Quotes related list on the Opportunity, it may not always meet specific requirements for customization or ease of access.

Salesforce offers a flexible approach to improving the user experience with custom buttons, which can dynamically support both Classic and Lightning environments. This blog explains how to create a button on the Opportunity object that allows users to easily create Quotes and other related records, such as Contracts and Custom Object records, while pre-populating essential fields to streamline the process.


Solution Overview

This solution involves creating a custom button on the Opportunity object that dynamically adapts to the user’s environment—Classic or Lightning. The button can pass default field values, ensuring seamless record creation and reducing manual data entry. This approach works without the need for custom code, making it easy to implement and maintain.


Solution Details and Formula

The button ensures that required fields are automatically populated, using the defaultFieldValues parameter in Lightning and query string parameters in Classic. Below is the formula used to dynamically create a new Quote record.

Dynamic Formula for Quotes

Formula with Optional Contact Field Parameter:

{!IF(

    OR(

        $User.UIThemeDisplayed = ‘Theme4d’,

        $User.UIThemeDisplayed = ‘Theme4t’

    ),

    URLFOR(“/lightning/o/Quote/new?&defaultFieldValues=OpportunityId=” & Opportunity.Id &

    “,Name=” & Opportunity.Name &

    IF(Opportunity.Primary_ContactId__c != NULL, “,ContactId=” & Opportunity.Primary_ContactId__c, “”)),

    URLFOR(“/0Q0/e?retURL=” & Opportunity.Id &

    “&oppid=” & Opportunity.Id &

    “&Name=” & Opportunity.Name &

    IF(Opportunity.Primary_ContactId__c != NULL, “&Contact_lkid=” & Opportunity.Primary_ContactId__c, “”))

)}

Formula Without Optional Contact Field Parameter:

{!IF(

    OR(

        $User.UIThemeDisplayed = ‘Theme4d’,

        $User.UIThemeDisplayed = ‘Theme4t’

    ),

    URLFOR(“/lightning/o/Quote/new?&defaultFieldValues=OpportunityId=” & Opportunity.Id & “,Name=” & Opportunity.Name),

    URLFOR(“/0Q0/e?retURL=” & Opportunity.Id & “&oppid=” & Opportunity.Id & “&Name=” & Opportunity.Name)

)}


Explanation of the Formula
1. Determining User Interface Theme
 
OR(
    $User.UIThemeDisplayed = ‘Theme4d’,
    $User.UIThemeDisplayed = ‘Theme4t’
)
 
Theme4d: Refers to Lightning Experience.
Theme4t: Refers to Salesforce1 (mobile version of Lightning).
The OR function ensures the formula can identify both themes as Lightning-compatible.


2. Handling Lightning (UIThemeDisplayed = ‘Theme4d’ or ‘Theme4t’)
If the user is in Lightning, the formula constructs the following URL:
URLFOR(“/lightning/o/Quote/new?&defaultFieldValues=OpportunityId=” & Opportunity.Id & “,Name=” & Opportunity.Name)
 
OpportunityId: Links the Quote to the originating Opportunity.
Name: Pre-fills the Quote Name with the Opportunity Name.
Optionally:
ContactId: If the Opportunity has a Primary Contact, it pre-fills this field.


3. Handling Classic (UIThemeDisplayed Not Equal to ‘Theme4d’ or ‘Theme4t’)
If the user is in Classic, the formula builds this URL:
URLFOR(“/0Q0/e?retURL=” & Opportunity.Id & “&oppid=” & Opportunity.Id & “&Name=” & Opportunity.Name)
 
retURL: Ensures that after creating the Quote, the user is redirected back to the Opportunity.
oppid: Links the Quote to the Opportunity.
Name: Pre-fills the Quote Name field using the Opportunity Name.


4. Conditional Contact Field
This segment ensures the ContactId or Contact_lkid parameter is included only if the Opportunity’s Primary_ContactId__c field is populated:
IF(Opportunity.Primary_ContactId__c != NULL, “,ContactId=” & Opportunity.Primary_ContactId__c, “”)
 

Additional Formulas for Creating Related Records
1. Create a Contract
Lightning Formula:
/lightning/o/Contract/new?&defaultFieldValues=OpportunityId={!Opportunity.Id},AccountId={!Opportunity.AccountId}
 
Classic Formula:
/0F0/e?retURL={!Opportunity.Id}&oppid={!Opportunity.Id}&accid={!Opportunity.AccountId}
 
2. Create a Custom Object Record
For a custom object with API name Custom_Object__c:
Lightning Formula:
/lightning/o/Custom_Object__c/new?&defaultFieldValues=OpportunityId={!Opportunity.Id},AccountId={!Opportunity.AccountId},Custom_Field__c=’Custom Value’
 
Classic Formula:
/a0X/e?retURL={!Opportunity.Id}&CF00N000000000001={!Opportunity.Id}&CF00N000000000002=Custom+Value
 
3. Create a Task
Lightning Formula:
/lightning/o/Task/new?&defaultFieldValues=WhatId={!Opportunity.Id},Subject=’Follow Up on Opportunity’
 
Classic Formula:
/00T/e?retURL={!Opportunity.Id}&who_id={!Opportunity.Id}&tsk5=Follow+Up+on+Opportunity
 

How to Implement These Buttons


1.Navigate to Setup
– Go to Object Manager > Opportunity > Buttons, Links, and Actions.
2.Create a New Button or Link
Label: New Quote / New Contract, etc.
– Type: Detail Page Button.
– Behavior: Display in a new window.
– Content Source: URL.
– Paste the appropriate formula.
3.Add the Button to Page Layouts
– Edit the Opportunity page layout(s).
– Drag the button to the layout from the buttons section.
4.Test the Button
– Verify that it works seamlessly in both Classic and Lightning.

Conclusion:
These dynamic buttons significantly enhance user productivity by providing an efficient way to create related records directly from the Opportunity page. With pre-populated fields and compatibility across Classic and Lightning, this solution ensures consistent data and a streamlined process.
For additional use cases, these examples can easily be extended to accommodate more parameters or validations. Let us know how this solution improves your workflow!
 
 

Leave a Reply

Your email address will not be published. Required fields are marked *