Skip to content

Technical

If you want to create a custom flow using the generate value functionality you can use the custom platform service ValueGeneratorService. It is available using a call to the platform.get function.

Web component example

import {
    ValueConfig,
    ValueGeneratorService,
    ValueGeneratorServiceName,
} from "src/services/value-generator.interface";

class MyComponent {
    private get valueGenerator(): ValueGeneratorService {
        return this.platform.get(ValueGeneratorServiceName);
    }

    public async generateValue() {
        const context = {
            limetype: "person",
            id: 1001,
        };
        const valueConfig = {
            formula: "{{ company.id }}",
        };

        const generatedValue = await this.valueGenerator.GenerateValue(
            context,
            valueConfig
        );
        console.log("The generated value", generatedValue);
    }
}

Service Interface

To use the service in the platform, the following file must be copied by hand to frontend/src/services/value-generator.interface.ts in your project.

import {
    LimeType,
    LimeWebComponentContext,
} from "@limetech/lime-web-components";

export const ValueGeneratorServiceName =
    "limepkg-standard-actions.value-generator.service";

export interface ValueGeneratorService {
    /**
     * Generate multiple values for a lime object.
     *
     * The values will be generated based on the provided value templates.
     *
     * The generated values will be type casted based on the limetype.
     *
     * The generated values will be returned as a record with the same keys as the value templates.
     *
     * If a value template has a formula, the formula will be evaluated (as jinja2) and the result will be returned.
     *
     * If a value template has a constant, the constant will be returned.
     *
     * If a value template has neither a formula nor a constant, null will be returned.
     *
     * If a value template has an expected output type, the value will be type casted to that type.
     *
     * If a value template has no expected output type, the value will be type casted to the type of the property in the limetype.
     *
     * @param limetype The limetype of the object, will be used for type casting each value.
     * @param context The context, what object the formula has as source.
     * @param valuesTemplate The value templates to generate.
     * @returns The generated values.
     */
    GenerateValues(
        limetype: LimeType,
        context: LimeWebComponentContext,
        valuesTemplate: Record<string, ValueConfig>
    ): Promise<Record<string, unknown>>;

    /**
     * Generate a single value.
     *
     * The value will be generated based on the provided value configuration.
     *
     * If the value config has an expected output type, the value will be type casted to that type.
     *
     * If the value config has no expected output type, the value will be type casted to the type of the given property in the context limetype.
     *
     * If the value config has no expected output type and no property name, the value will be casted to the best "guess" based on the value.
     *
     * If the value config has a formula, the formula will be evaluated (as jinja2) and the result will be returned.
     *
     * If the value config has a constant, the constant will be returned.
     *
     * If the value config has neither a formula nor a constant, null will be returned.
     *
     * @param context The context, what object the formula has as source.
     * @param valueConfig The value configuration.
     * @param propertyName The name of the property, used for type casting.
     * @returns The generated value.
     */
    GenerateValue(
        context: LimeWebComponentContext,
        valueConfig: ValueConfig,
        propertyName?: string
    ): Promise<unknown>;
}

export type OutputType = "auto" | "int" | "float" | "str" | "datetime" | "list";

export interface ValueConfig {
    constant?: unknown;
    formula?: string;
    expectedOutputType?: OutputType;
}