Service Catalog Calculate Total....not price total

davidvetter
Tera Contributor

Hi all. I'm building a catalog  that lists resource types for provisioning IT services. Each resource type has a numerical value (provisioning units, PUnits)) and each provisioning action is based on a selected project size XS, S, M, L, XL where each project size has a limit of 6, 20, 40, 60, 80 PUnits...respectively. If the XS = 6 PUnits, how would I have the catalog form throw an error if a user selects a combination of items that equals more than 6? The same would apply for 20, 40, 60, and 80.

7 REPLIES 7

Brian Lancaster
Tera Sage

You can do one of two things.

  1. Create onChange client scripts the runs on the variables that need to be totaled and then throw an error. You will then need to clear the values and/or have an onSubmit client script to prevent them from ordering it anyway.
  2. Just have an onSubmit client script that throws the error when they try to submit the script.

Thanks Brian. Guess I'm off to ChatGPT to have it write a script for me. I'm not an experienced coder.

What are all the variable names that need to be totaled?

Vishal Jaswal
Giga Sage

Hello @davidvetter 

If you need something like below, then here are the steps:

VishalJaswal_0-1744129589511.png

VishalJaswal_1-1744129598211.png

A Catalog item (sc_cat_item) with below variables:

VishalJaswal_2-1744129621923.png

Four Catalog Client Scripts as shown below:

VishalJaswal_3-1744129671380.png

Catalog Total onChange Client Script:

VishalJaswal_4-1744129717088.png

 

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue === '') {
    return;
  }

    // Define project size limits
    var projectSizeLimits = {
      "XS": 6,
      "S": 20,
      "M": 40,
      "L": 60,
      "XL": 80
    };

    // Get selected project size and limit
    var projectSize = g_form.getValue('project_size');
    var limit = projectSizeLimits[projectSize];
    if (!limit) return;

    // Get latest values (after delay)
    var devVal = parseInt(g_form.getValue('resource_dev')) || 0;
    var testerVal = parseInt(g_form.getValue('resource_tester')) || 0;
    var analystVal = parseInt(g_form.getValue('resource_analyst')) || 0;
    var totalVal = devVal + testerVal + analystVal;

    // Clear existing messages
    g_form.hideAllFieldMsgs('project_size');

    // Show appropriate message
    if (totalVal > limit) {
      g_form.showFieldMsg(
        'project_size',
        "Total PUnits (" + totalVal + ") exceed the limit for " + projectSize + " (" + limit + ").",
        "error"
      );

    } else {
      g_form.showFieldMsg(
        'project_size',
        "Total PUnits: " + totalVal + " — within limit for " + projectSize + " (" + limit + ").",
        "info"
      );

    }
}

Other three onChange Client scripts as follows:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading) {
      return;
   }

   //Type appropriate comment here, and begin script below
    g_form.setValue('project_size', '');
}

VishalJaswal_5-1744129754541.png

VishalJaswal_6-1744129780118.png

 

VishalJaswal_7-1744129795726.png

 

 



NOTE: The three onChange Client scripts upon changing the Developers, Testers and Analysts value need a click anywhere in the form to set Project Size to None so that it's choice can be selected again for validation. 


Hope that helps!