Calculation Examples

JavaScript code samples for common calculation use cases.

📘

Note

The following examples are for illustrative purposes only. These are not SAP LeanIX best practices or recommendations. Use them as a reference to create calculations tailored to your needs.

To learn how to get started with calculations, see Calculations.

Calculating the Sum of Budget Fields

This custom JavaScript function is designed to calculate the total budget for initiative fact sheets by summing the values of CapEx (Budget) and OpEx (Budget). The function first checks if both budgetCapEx and budgetOpEx values are defined. If both values are present, it computes their sum and returns the total budget (totalBudget). If either value is missing, the function returns null, and the target field is set to empty.

export function main() {
    if (data.budgetCapEx == null || data.budgetOpEx == null) {
        return null;
    } 

    const totalBudget = data.budgetCapEx + data.budgetOpEx;
    return totalBudget;
}

Calculating a Weighted Technical Fit Score

This custom JavaScript function is designed to calculate a weighted technical fit score (custom field) for IT component fact sheets based on their stability, maintainability, and security ratings. The function defines a scoring object that maps qualitative ratings (such as best, aboveAverage, average, belowAverage, and worst) to numeric scores and a weighting object that assigns weights to each of these criteria.

The function first checks if a fact sheet contains valid ratings for custom fields stability, maintainability, and security. If all ratings are present, it calculates a weighted average score by summing the products of each rating's score and its corresponding weight, then dividing by the total of the weights. If any of the ratings are missing, the function returns null, and the target field is set to empty.

export function main() {
    if (data.stability == null || data.maintainability == null || data.security == null) {
        return null;
    }
    
    const scoring = {
        best: 100,
        aboveAverage: 75,
        average: 50,
        belowAverage: 25,
        worst: 0,
    };
    const weighting = {
        stability: 1,
        maintainability: 1,
        security: 2
    };
    const totalWeights = weighting["stability"] + weighting["maintainability"] + weighting["security"];
    const technicalFitScore = (weighting["stability"] * scoring[data.stability] + weighting["maintainability"] * scoring[data.maintainability] + weighting["security"] * scoring[data.security])/totalWeights;
    return technicalFitScore;
}

Calculating a Maturity Gap

This custom JavaScript function is designed to calculate and categorize a maturity gap based on the difference between a target maturity level and a current maturity level of a fact sheet. The main function first defines a scoring object that maps maturity levels (ranging from adhoc to optimized) to numerical scores (from 1 to 5). It then initializes a variable maturityGapScore to 0. If both the targetMaturity and currentMaturity fields have values, the function calculates the maturityGapScore by subtracting the score of the current maturity level from the score of the target maturity level. If either field is empty, the function returns null, and the target field is set to empty.

After calculating the maturityGapScore, the function categorizes it into different levels based on its value: from none to veryHigh. This categorization helps in understanding the extent of the gap between the current and target maturity levels.

export function main() {
    if (data.targetMaturity == null || data.currentMaturity == null) {
        return null;
    } 

    const scoring = {
    optimized: 5,
    managed: 4,
    defined: 3,
    repeatable: 2,
    adhoc: 1,
    };
    const maturityGapScore = scoring[data.targetMaturity] - scoring[data.currentMaturity];    
    
    if (maturityGapScore <= 0) {
        return 'none'
    };
    if (maturityGapScore === 1) {
        return 'low'
    };
    if (maturityGapScore === 2) {
        return 'medium'
    }
    if (maturityGapScore === 3) {
        return 'high'
    }
    if (maturityGapScore === 4) {
        return 'veryHigh'
    }
}

Creating a URL from a String

This custom JavaScript function is designed to generate a URL by concatenating a base URL with an external identifier (externalId) from a fact sheet. The function begins by defining a baseUrl constant set to https://www.external-system.com/. It then checks if a fact sheet contains a valid externalId. If an externalId is present, the function concatenates the baseUrl with the externalId to create the full URL and returns it. If the externalId is explicitly set to null, the function returns null, and the target field is set to empty.

export function main() {
    if (data.externalId == null) {
        return null;
    }

    const baseUrl = "https://www.external-system.com/";
    const url = baseUrl.concat(data.externalId);
    return url;
}