top of page

Sort Order in Salesforce CPQ: A Deep Dive into Custom Scripting

Updated: Dec 1, 2023

Github Link : https://github.com/AourLegacy/AourFactory/tree/sortOrder

Salesforce CPQ (Configure, Price, Quote) has transformed the way businesses set up their product catalogs and pricing structures. But what if you need to sort products in a line editor? This is where custom scripts come into play.

Overview of the Custom Script

The function `onAfterPriceRules` is an integral part of the Salesforce CPQ, enabling the sorting of products in a line editor. At a high level, the function checks for parents of a particular line item up to three levels and accordingly sets the sort order.

Breaking Down the Script

Upon invocation, the script logs its entry point, making debugging easier. After initial checks, the script delves into sorting:

1. For Every Line Item: The function begins by iterating over each quote line, examining the parent-child relationships.

2. Parent Check: Every line item can have a parent, a grandparent, and even a 'grand-grand' parent (essentially three levels of hierarchy). The script starts by checking if a parent exists for the current line item.

3. Sort Order Creation: Based on the existing parent hierarchy, the sort order is generated by concatenating values from several fields like `PRNumber__c`, `POFeaturenumber__c`, and `PONumber__c`.

- If the line item has a 'grand-grand' parent, all the relevant fields from each hierarchy level contribute to the sort order.

- In the absence of a 'grand-grand' parent but the presence of a grandparent, the grandparent's fields, along with some default values, form the sort order.

- If only the parent exists, its fields, combined with specific default values, set the sort order. - Lastly, if the line item stands alone without any parent, a default sort order is set using its fields.

4. Logging for Transparency: Each sort order generation is followed by a log statement, ensuring traceability and ease of debugging.


export function onAfterPriceRules(quoteModel, quoteLines){
    console.log("###############onAfterPriceRules#############"); 
 /// Sort Order 
 if (quoteLines != null) {
    quoteLines.forEach((line) => {

        var parent = line.parentItem;
        if (parent != null) {
          var grandParent = parent.parentItem;
          if (grandParent != null) {
            var gpgrandParent = grandParent.parentItem;
            if (gpgrandParent != null) {

                // avec grand grand parent
              line.record["Sort_Order__c"] = gpgrandParent.record.PRNumber__c  + grandParent.record.POFeaturenumber__c +  grandParent.record.PONumber__c + parent.record.POFeaturenumber__c + parent.record.PONumber__c + line.record.POFeaturenumber__c + line.record.PONumber__c ;
              console.log("gp gp Parent Sort order " + line.record.Sort_Order__c)

            }else {
                // pas de grand grand parent
                line.record["Sort_Order__c"] = grandParent.record.PRNumber__c+ parent.record.POFeaturenumber__c + parent.record.PONumber__c + line.record.POFeaturenumber__c + line.record.PONumber__c +"0000000000" ;
                console.log("gp Parent Sort order " + line.record.Sort_Order__c)

            }
          }else { 
            // pas de grand parent
            line.record["Sort_Order__c"] = parent.record.PRNumber__c  + line.record.POFeaturenumber__c + line.record.PONumber__c +"00000000000000000000";
            console.log(" Parent Sort order " + line.record.Sort_Order__c)

          }
        } else {
          // pas de parent

          line.record["Sort_Order__c"] = line.record.PRNumber__c +"000000000000000000000000000000" ;
          console.log("no Parent Sort order " + line.record.Sort_Order__c)

        }
           

    });
  }

  return Promise.resolve();
}

In the Salesforce CPQ ecosystem, custom scripts like `onAfterPriceRules` empower businesses to tailor their processes more effectively. By leveraging parent-child relationships up to three levels, the script offers a robust mechanism to set a sort order in the line editor, ensuring products are displayed in a desired sequence.

Remember, while the script's logic might seem complex at first, it follows a structured approach based on the hierarchy levels. As businesses evolve, scripts like these can be further customized, ensuring Salesforce CPQ remains a dynamic tool for all pricing needs.

1 view0 comments

Recent Posts

See All

Commentaires

Noté 0 étoile sur 5.
Pas encore de note

Ajouter une note
bottom of page