January 24, 2024

Salesforce CPQ – How to hide or make fields uneditable in QLE (Quote Line Editor)

Do you ever find yourself in a situation where you want to hide or lock a field in Salesforce CPQ’s Quote Line Editor (QLE)? In this blog post, we’ll walk you through the process of achieving this goal.

Fawad
Fawad Rafique
Salesforce CPQ Developer
Salesforce CPQ

Business Scenario

There are situations where businesses want to maintain flexibility to edit fields on Quotes and Quote Lines, but need to keep things in check within QLE by either hiding the fields or making them uneditable. For instance, you might want to hide the “Additional Discount” field on a Quote in the QLE when the Account has a “Silver” SLA (Service Level Agreement). Or perhaps you need to make the “Additional Discount” field on the QuoteLine uneditable when the “List Price” field has a value less than or equal to €150. We can accomplish this using the Quote Calculator Plugin (QCP). Let’s dive in and learn how to achieve this using the Page Security Plugin methods of the Quote Calculator Plugin.

QLE

Implementation Steps

To set up a page security plugin, you need to follow a couple of steps. First, you’ll define your code within a custom script record, and then you’ll reference the name of that record in the Quote Calculator Plugin field within the Salesforce CPQ Plugin package settings.

Here’s the script we’ll use to make the magic happen:

					
// At the top of our QCP script, we initialise a list of fields for error reduction and easier script maintenance.

// FIELDS are frozen to make immutable
const FIELDS = Object.freeze({
    ACCOUNT_SLA: 'AccountSLA__c',
    ADDITIONAL_DISCOUNT: 'SBQQ__AdditionalDiscount__c',
    CUSTOMER_DISCOUNT: 'SBQQ__CustomerDiscount__c',
    LIST_PRICE: 'SBQQ__ListPrice__c'
});

const always = () => true; // used this to hide or lock fields without any condition
const hasListPriceLessThanOrEqual150 = (record) => record[FIELDS.LIST_PRICE] <= 150;
const hasSilverSLA = (record) => record[FIELDS.ACCOUNT_SLA] === 'Silver';

/**
 * @description array of the editability settings on quote or quoteline
 * @property {string} objectName - name of the object for which fields must be set uneditable
 * @property {string} condition - condition defining when to make fields uneditable,
 *                                access the field using square bracket property access notation, e.g. record[FIELD_NAME]
 * @property {string[]} fields - list of fields to make uneditable for the specified condition and object
 */
const editabilitySettings = {
    QuoteLine__c: [
        {
            // Make SBQQ__AdditionalDiscount__c on QuoteLine__c uneditable if SBQQ__ListPrice__c <= 150
            condition: hasListPriceLessThanOrEqual150,
            fields: new Set([
                FIELDS.ADDITIONAL_DISCOUNT
            ])
        }
    ],
    Quote__c: []
};

/**
 * @description array of the visibility settings on quote or quoteline
 * @property {string} objectName - name of the object for which fields must be hidden
 * @property {string} condition - condition defining when to hide fields,
 *                                access the field using square bracket property access notation, e.g. record[FIELD_NAME]
 * @property {string[]} fields - list of fields to hide for the specified condition and object
 */
const visibilitySettings = {
    QuoteLine__c: [],
    Quote__c: [
        {
            // Hide SBQQ__CustomerDiscount__c on Quote__c if AccountSLA__c === 'Silver'
            condition: hasSilverSLA,
            fields: new Set([
                FIELDS.CUSTOMER_DISCOUNT
            ]),

        }
    ]
};

// In Salesforce CPQ, utilise QCP for a Page Security Plugin in the Quote Line Editor (QLE) to manage field editing and viewing.
// As a best practice, avoid coding directly in PSP functions isFieldEditableForObject and isFieldVisibleForObject to maintain code readability.
// Instead, use visibilitySettings and editabilitySettings objects to centralise visibility and editability logic.
// This way, we can modify our settings and conditions and simply add or remove fields from the object as our requirements change.

/**
 * @description method used to make fields uneditable on the QLE
 * @param {string} fieldName - field name to lock from editing
 * @param {object} record - Quote__c or QuoteLine__c record containing the field to lock
 * @param {object} conn - optional parameter to access JSforce methods
 * @param {string} objectName - object that holds the field, either Quote__c or QuoteLine__c
 * @returns {boolean} field is uneditable if false
 */
export function isFieldEditableForObject(fieldName, record, conn, objectName) {
    for (const setting of editabilitySettings[objectName]) {
        if (setting.fields.has(fieldName) && setting.condition(record)) return false;
    }
}

/**
 * @description method used to hide fields on the QLE
 * @param {string} fieldName - field name to hide
 * @param {object} record - Quote__c or QuoteLine__c record containing the field to hide
 * @param {object} conn - optional parameter to access JSforce methods
 * @param {string} objectName - object that holds the field, either Quote__c or QuoteLine__c
 * @returns {boolean} - field is hidden if false
 */
export function isFieldVisibleForObject(fieldName, record, conn, objectName) {
    for (const setting of visibilitySettings[objectName]) {
        if (setting.fields.has(fieldName) && setting.condition(record)) return false;
    }
}
					
				

Simple, right? The above code snippet contains two functions: the isFieldEditableForObject function to make fields uneditable, and the isFieldVisibleForObject function to hide fields. When these functions return false, Salesforce CPQ either locks or hides the specified fields accordingly.

Putting the script to work:

  1. Go to the App Launcher, search “Custom Script”, and select the “Custom Script” item.
  2. Create a new record and provide the following details:
    • Script Name: QCP
    • Code: Copy and paste the above script
    • Quote Fields: Enter “SBQQ__CustomerDiscount__c” and “AccountSLA__c” on separate lines.
    • Quote Line Fields: Enter “SBQQ__AdditionalDiscount__c” and “SBQQ__ListPrice__c” on separate lines.
    • Quote Line Group Fields: Leave this empty, as we won’t be evaluating Quote Line Group fields.
    • Click Save

CPQ Custom Script

  1. To make this script work its magic, follow these steps:
    • Go to Setup > Installed Packages > Configure Salesforce CPQ.
    • Under ‘Plugin’, set the Quote Calculator Plugin field to QCP.
    • Save your changes.

CPQ Configuration Settings Page

Note: If you’re already using a quote calculator plugin, you can add your page security plugin code to the calculator plugin’s custom script record.

Testing

When you open the QLE, you’ll notice the “Additional Discount” field on Quote is hidden when your Account SLA equals ‘Silver’. Furthermore, the “Additional Discount” fields of Quote Lines with a List Price less than or equal to €150 are locked and uneditable, while the Quote Line with a List Price of €1500 is still editable.

 

Field Hidden

Field still editable

Field Lock 1

Field Lock 2

Consideration

Keep in mind that Salesforce CPQ prioritises field-level security over page security plugins. So, if a field is set to read-only and an editability function for that field returns true, the field will remain read-only.

In a nutshell, controlling field visibility and editability in the Quote Line Editor can be a game-changer for Salesforce CPQ administrators and developers. With the Quote Calculator Plugin and a touch of custom scripting, you can create a more tailored and user-friendly experience for your team. Happy configuring!

Spread the word

Fawad
Fawad Rafique
Salesforce CPQ Developer
As an experienced Salesforce CPQ developer, specialized in designing and implementing custom solutions that enhance business processes, automate workflows, and improve data management.