Extract value from object and use it for text field

I have a table with strings for invoices (several languages). Each string has an id (e.g. txt_pay_until) and a language identifier (language_id). I read the data filtered by language_id from the table in a workflow and save it in an object variable (invoice_pdf_labels). This variable therefore contains the IDs and the translations for a specific language. Now I would have to include every single string in my API call. For example:

“txt_pay_until”: Payable until",

I’m not a “real” programmer, so I don’t know how to extract the individual string translations from the object and transfer them to my JavaScript as variables. I could of course include each individual string based on its index, but if this changes, the wrong strings are sent. Also, the different languages have different indexes for the same ID.

I also tried Lookup to extract the translated string, but it only extracts the English strings since they come first:

image

It would also be very laborious to create a separate formula and a separate text field or input field for each string, which would then be used in JavaScript.

I don’t think that this solution works for me: How to Filter collection by object value?

Who can help?

Use lookupArray to find all values with the corresponding id, then use a lookup on the result to get the specific language

Hi Luka,
You mean use a nested formula, like Lookup xxx(LookupArray yyy))? But how can I use the result in my JS? And I had to do this for about 30 ids. I already have an object variable. IMHO It should be possible referencing the items in JS directly.
What do you think?

I’ve now used this formula which finds the localized item. But how can I extract the actual string, i.e. string_text, “Rechnung”? And assign it to a variable to be used in JS?

<(image

The fixed string “Rechnung” should be replaced by the variable:

image

This is some content of the invoice_pdf_labels object variable:

{
“language_id”: 1,
“string_text”: “Frau”,
“tenant_id”: 0,
“pdf_txt_item”: “txt_female_ms”,
“string_id”: 38,
“corresponds_en_string_id”: 3
},
{
“language_id”: 1,
“string_text”: “Bestellung”,
“tenant_id”: 0,
“pdf_txt_item”: “txt_po”,
“string_id”: 39,
“corresponds_en_string_id”: 4
},

As I said, it would be much easier extracting the string_text from the object variable since it’s already filtered. Like Extract string_text for pdf_text_item = “txt_po”

You either need to add .string_text or [0].string_text at the end of your formula :slight_smile:

Hi Broberto,

Unfortunately, this isn’t the ideal solution. I need a formula/variable within JS. Your suggestion only works if I want to display a specific text in an input or text field.

I tried the pick function but it seems this can’t be used within JS (runtime error):
“txt_quote_invoice_date”: wwFormulas.pick(variables[‘ce338942-0acf-49d3-a195-0a2edd669693’],“txt_quote_invoice_date”),

Just for testing, I created an input field which reads the translated string from the database table:
image

I could now use it in JS:
image

I’ve now managed including the lookupArray formula into a Lookup formula (Luka’s suggestion):

lookup(“txt_quote_invoice_number”, lookupArray([“txt_quote_invoice_number”],collections[‘31202653-39ed-4d65-b1ea-b78e32556f42’]?.[‘data’],‘pdf_txt_item’), ‘pdf_txt_item’).string_text
image

But how can I indicate the language? It’s always using the first one (English).

But this method has two major disadvantages:

  1. I’d have to create a field (which could be hidden of course) for each string item which is very time consuming and- IMHO - stupid work.
  2. It will probably consume many resources (time and memory).

Conclusion: A way to extract string_text from the object variable containing all strings within JS would be more elegant and probably improve performance significantly.

But if this isn’t possible, I will use the hidden fields solution (31 fields!).

I found a temporary solution:

I use a Collection filter for the String translations (for PDF output) table: a new customer_communication_language variable. This variable is set according to the selected customer. Now only strings in that language are retrieved and I can use the localized strings in the formula from above.
But as I said: This is probably not the best solution; I still would like to use the item of an invoice_pdf_labels object variable which only contains the localized strings:

{
“language_id”: 1,
“string_text”: “Frau”,
“tenant_id”: 0,
“pdf_txt_item”: “txt_female_ms”,
“string_id”: 38,
“corresponds_en_string_id”: 3
}

Key is pdf_txt_item, value is string_text.

I can’t imagine that this isn’t possible :innocent:

I might have found a solution using JS.

Here is my array of objects like this (invoice_pdf_labels):

[
{
“language_id”: 1,
string_text”: “Frau”,
“tenant_id”: 0,
“pdf_txt_item”: “txt_female_ms”,
“string_id”: 38,
“corresponds_en_string_id”: 3
},
{
“language_id”: 1,
string_text”: “Bestellung”,
“tenant_id”: 0,
“pdf_txt_item”: “txt_po”,
“string_id”: 39,
“corresponds_en_string_id”: 4
}
]

Example of a JS line sent via the API:
“txt_po”: “Bestellung”,

With the help of AI I created a script for ONE function call per string. Here’s the complete code for my sample array:

// Array of objects (extract)
let invoice_pdf_labels = [{
“language_id”: 1,
“string_text”: “Frau”,
“tenant_id”: 0,
“pdf_txt_item”: “txt_female_ms”,
“string_id”: 38,
“corresponds_en_string_id”: 3
},
{
“language_id”: 1,
“string_text”: “Bestellung”,
“tenant_id”: 0,
“pdf_txt_item”: “txt_po”,
“string_id”: 39,
“corresponds_en_string_id”: 4
}
]

// The following lines are for testing only
// 1. Find the object with the key “txt_po”:
// let pdf_string = invoice_pdf_labels.find(pdf_string => pdf_string.pdf_txt_item === “txt_po”);
// console.log(pdf_string);
// 2. Assign the output value for “string_text” to a variable:
// let xx=“"”+pdf_string.pdf_txt_item + “"” +“: " + “"”+pdf_string.string_text+”"“+”, ";
// console.log(xx);

function extract_pdf_string(lookup) {
let pdf_string = invoice_pdf_labels.find(pdf_string => pdf_string.pdf_txt_item === lookup);
let xx = “"” + pdf_string.string_text + “"”;
return xx;
}

// let lookup = “txt_po”;
// let txt_po = extract_pdf_string(lookup);
let txt_po = extract_pdf_string(“txt_po”);
console.log(txt_po);

// Output is “Bestellung”

This function must be run for each item; the output is assigned to a variable (const) with the same name as pdf_txt_item. It will then be used in the corresponding line, e.g.
“txt_po”: txt_po,

But it would be nicer if this could be achieved in a loop, i.e. the function iterates the whole array and performs these steps automatically (i.e. creates xx for each pdf_txt_item).

Or even better: It produces strings like this one:
“txt_po”: “Bestellung”,
and concatenates the results. The combined output is saved in a single constant which in turn will be used in my JS. Like this:
“txt_po”: “Bestellung”, “txt_order_confirmation”: “Auftragsbestätigung”, …

Meanwhile I don’t think this will work because of the quotation marks. Or could escape be used here?

Anyway, I’m already happy with my solution (it works in WeWeb!). Or do you have a better solution?

Hello WeWeb team and Weweb community!

Still no simple functionality like you know how to do so well for this?

I have a formula based on lookupArray which produces a certain number of objects. I need to retrieve particular information (for example a Key called “work”) But I cannot do it without integrating the value of the record [0] or [1]… before putting the .work key or [“work”]

I think we need:
lookupArray(list_of_id_work, Table_of_work, “id”).work
Return: [“work0”, “work1”, “work2”, “work3”] or “work1,work2,work3,…”