Issues with writing my first TypeScript function

Hello everyone ;

First off, I tried to look inside the forum, followed most of the “Application Developer Track” in Palantir Learning and did some research but I am a bit lost. Sorry if the answer was provided before, I could not find it.

I want to create a simple calculator : the user has to select two or more entries from the dataset (basic structure below), then a quantity (by what the column “Value” will be multiplied) and then summing everything.

ID Value
1 1,2
2 40
3 30
4 20

I want therefore to create a simple xlookup-ish function that, if the user type 1, it shows 1,2 in a metric card. The second step is to multiply the “1,2” with an input from the user but this is for the moment not the topic.

I already ingested my dataset in the code Repository.

There is what I could do with the help of the chatbot ;

    @Function()
    public lookup(INPUT: string): string | null {
        const result = this.DATA.find(row => row['ID'] === INPUT);
        return result ? result['Value'] : null;
    }
}

Before this I tried to mimic the mandatory decorators :

import { Function } from "@foundry/functions-api";

export class Lookup {
    private DATA: { [key: string]: any }[];

    constructor() {
        this.loadDataset("MYRID").then(data => {
            this.DATA = data;
        });
    }

Any help is really appreciated,

Thanks a lot !!

Hi Moomin,

Welcome to the Dev Community! Excited to have you here.

First of all, since I think you will want to showcase the result in a metric card I am thus assuming that you will be using workshop as a frontend.

When using workshop, the items we are working with are ontology object types that are backed by data. In your case the example dataset you provided. See here on how to create a new object type.

The calculation you would be performing on an instance of this object type would be encompassed in an action where the definition of this action will be a TypeScript function. How to create a new action type is documented here. More specifically, since the logic of your action is a bit more involved than an edit or delete operation, you would be creating a function-backed action.

In the frontend the user could then select two or more of the objects (in our example we hydrated the ExampleObject with your dataset and the user selects for example with ID 1 and ID 3) in the action form, provide a quantity and submit. The corresponding function in TS would then look like this:

import { Function, Double } from "@foundry/functions-api";
import { ExampleObject } from "@foundry/ontology-api";

export class Calculator {
    @Function()
    public calculateSum(objects: ObjectSet<ExampleObject>, multiplier: Double): Double {
        let sum = 0;
        objects.all().forEach(obj => {
            const value = parseFloat(obj.value.replace(',', '.')); // Convert to number
            sum += value * multiplier;
        });
        return sum;
    }
}

Let me know if you are still stuck! Also feel free to roam around the public documentation.