Is there a way for N/query's asMappedResults to re...
# suitescript
s
Is there a way for N/query's asMappedResults to return the json with the label values? I don't want id_1 to be returned. I'd prefer transaction.id or transactionId
s
NFT's
nsSearchResult2obj
does so as a configurable argument. though admittedly that's only for n/search, not n/query.
as a best practice, we always use valid JS identifiers as labels on searches created in the UI meant for scripts to use. This way it's easy to link the search columns to the code names without an additional mental mapping.
s
Could you modify the query columns and read the labels and make them the alias? I believe asMappedResults names them after the alias you have chosen
s
@Shawn Talbert sorry for the delay, just read these! what do you mean by the js identifiers?
@Sandii I can modify it in the UI and code if it's possible. How would you change the alias?
s
I mean making label text be valid JS identifiers then use those in the code.
s
I changed the label to be transactionId, but when I run asMappedResults it is coming back with id_1 still
s
it's NFT nsSearchResult2obj that obeys the labels
s
Did you use suiteQL syntax or query.create()? If using the query.create() syntax, you can definte the
alias
property of each column, and asMappedResults() uses that. I don't know how to do it if using suiteQL syntax
s
I just used the query.load
query.load and then the iterator
s
you could try retrieving the
query.columns
from the query, and settting the
alias
property on each column to be whatever you want
s
Ok, I can get the label in the result.data.columns. Is there a way to change the alias in the UI or does it have to be in the code?
s
I dont know a way to change the alias in the UI, never tried
s
How do you normally set the alias in the n/query? I'm new to analytics, so I was just creating it in the UI. It's pretty simple. I'm getting a few fields from the customer table and a few fields from the transaction table
s
Depends, its you are using the long annoying syntax with query.create, its just one of the properties you set in the query.createColumn() call. Most people I think just use suiteQL syntax (like writing a long string query). I don't know/never tried to use/set alias with suiteQL synax
s
Is there a way to easily convert what I have to suiteql?
s
Query.toSuiteQL()
Copy code
Important The resulting SuiteQL query string (contained in the SuiteQL.query property) does not include any aliases you set on query result columns in the original query.Query object. For more information about aliases, see Column.alias.
help pages specifically indicates alias doesnt work in suiteQL
s
aliases in the SQL sense do work just fine with suiteQL, no?
I mean
select foo as bar from T
will result in objects with a property named
bar
rather than
foo
s
pretty sure that is how it works... I would think loading the query from UI would make the labels like that codewise but I am not sure
s
Ok cool! I'll test it out!
Oof, that's rough! lol SELECT BUILTIN_RESULT.TYPE_STRING(Customer.entityid) AS entityid /*{entityid#RAW}*/, BUILTIN_RESULT.TYPE_INTEGER(Customer."ID") AS "ID" /*{id#RAW}*/, BUILTIN_RESULT.TYPE_INTEGER("TRANSACTION"."ID") AS id_1 /*{entity<transaction.id#RAW}*/, BUILTIN_RESULT.TYPE_STRING("TRANSACTION".tranid) AS tranid /*{entity<transaction.tranid#RAW}*/, BUILTIN_RESULT.TYPE_STRING("TRANSACTION".custbody_ad_accounting_hold_reason) AS custbody_ad_accounting_hold_reason /*{entity<transaction.custbody_ad_accounting_hold_reason#RAW}*/, BUILTIN_RESULT.TYPE_INTEGER(Customer.consoldaysoverduesearch) AS consoldaysoverduesearch /*{consoldaysoverduesearch#RAW}*/, BUILTIN_RESULT.TYPE_CURRENCY(Customer.creditlimit, BUILTIN.CURRENCY(Customer.creditlimit)) AS creditlimit /*{creditlimit#RAW}*/, BUILTIN_RESULT.TYPE_CURRENCY(Customer.balancesearch, BUILTIN.CURRENCY(Customer.balancesearch)) AS balancesearch /*{balancesearch#RAW}*/, BUILTIN_RESULT.TYPE_INTEGER(Customer.custentity_mhi_ad_grace_period) AS custentity_mhi_ad_grace_period /*{custentity_mhi_ad_grace_period#RAW}*/ FROM Customer, "TRANSACTION" WHERE Customer."ID" = "TRANSACTION".entity(+) AND (("TRANSACTION"."TYPE" IN ('SalesOrd') AND "TRANSACTION".status IN ('SalesOrd:D', 'SalesOrd:A', 'SalesOrd:F', 'SalesOrd:E', 'SalesOrd:B') AND "TRANSACTION".custbody_mhi_ad_expected_ship_date IS NOT NULL))