Netsuite get "Currency Revaluation (Unrealized Gain/Loss)" Table - netsuite

I am new to netsuite,
Is it possible to get the table "Currency Revaluation (Unrealized Gain/Loss)" using suitescript QL.
I would like to use it in Suite QL
here is an sample:
SELECT
NT .*
FROM
NextTransactionLineLink AS NTLL
INNER JOIN Transaction AS NT ON (NT.ID = NTLL.NextDoc)
INNER JOIN Transaction ON (Transaction.ID = NTLL.PreviousDoc)
inner join transactionline tl ON (tl.TRANSACTION = Transaction.id)
inner join subsidiary sb ON sb.id = tl.subsidiary
inner join accountingperiod ap ON (
(ap.id = Transaction.postingperiod)
AND ap.isposting = 'T'
)
inner join accountingperiod pap ON (
(pap.id = NT.postingperiod)
AND pap.isposting = 'T'
)
inner join currencyrate cr1 ON (
cr1.basecurrency = sb.currency
AND cr1.transactioncurrency = Transaction.currency
AND (
cr1.effectivedate = To_date(
ap.startdate,
'MM/DD/YYYY'
)
)
)
inner join consolidatedexchangerate cexr ON (
cexr.postingperiod = Transaction.postingperiod
AND cexr.fromsubsidiary = tl.subsidiary
AND cexr.tosubsidiary = 1
)
WHERE
(NTLL.NextDoc = 212328)
Thanks in Advance

By "getting", I am assuming you want to load this record and the fetch/update the values within.
Simply use &xml=t in the url, after the URL of the record
Something like this -
https://342xxxx-sb1.app.netsuite.com/app/accounting/transactions/vendbill.nl?id=00000&whence=&xml=t
You will get the record type.
<record recordType="vendorbill"
Use this record type in your Script.
var objRecord = record.load({
type: record.Type.VENDOR_BILL,
id: 157,
isDynamic: true,
});
If in case the record is a Custom Record, use this
var newFeatureRecord = record.load({
type: 'customrecord_feature',
id: 1,
isDynamic: true
});
Let me know in case of issues in the comments below.

Related

TableAlias doesn't work with multiple joins

TableAlias isn't working with multiple joins.
The query:
var q = Db.From<Blog>(Db.TableAlias("b"))
.LeftJoin<Blog, BlogToBlogCategory>((b,btb)=> b.Id == btb.BlogId, Db.TableAlias("btbc"))
.Join<BlogToBlogCategory, BlogCategory>((bt,bc)=>bt.BlogCategoryId == bc.Id, Db.TableAlias("cats"))
.GroupBy(x => x.Id);
.Select("b.*, json_agg(cats) as BlogCategoriesJson");
var results = Db.Select<BlogQueryResponse>(q);
Generates this SQL:
SELECT b.*, json_agg(cats) as BlogCategoriesJson
FROM "blog" "b" LEFT JOIN "blog_to_blog_category" "btbc" ON ("b"."id" = "btbc"."blog_id") INNER JOIN "blog_category" "cats" ON ("blog_to_blog_category"."blog_category_id" = "cats"."id")
GROUP BY "b"."id"
This causes error because it is referencing "blog_to_blog_category" instead of btbc
The Db.TableAlias() only provides an alias for the target join table, your inner join does not specify the alias to use for the source table so it references the full table name as expected.
You can use Sql.TableAlias() in your LINQ Expression to reference a table alias, e.g:
var q = Db.From<Blog>(Db.TableAlias("b"))
.LeftJoin<Blog, BlogToBlogCategory>((b,btb)=> b.Id == btb.BlogId, Db.TableAlias("btbc"))
.Join<BlogToBlogCategory, BlogCategory>((bt,bc)=>
Sql.TableAlias(bt.BlogCategoryId, "btbc") == bc.Id, Db.TableAlias("cats"))
.GroupBy(x => x.Id);
.Select("b.*, json_agg(cats) as BlogCategoriesJson");

Select distinct on joined table

I have this query
var q = Db
.From<Blog>()
.LeftJoin<BlogToBlogCategory>()
.Join<BlogToBlogCategory, BlogCategory>()
.Where( .. )
.SelectDistinct();
var results = Db.Select<BlogCategory>(q);
It generates this SQL:
SELECT DISTINCT "blog".*, 0 EOT, "blog_category".*, 0 EOT
FROM "blog" LEFT JOIN "blog_to_blog_category" ON
("blog"."id" = "blog_to_blog_category"."blog_id") INNER JOIN "blog_category" ON
("blog_category"."id" = "blog_to_blog_category"."blog_category_id")
WHERE ...
I want to select distinct blog_category but the select is adding all the blog fields also so I am getting duplicate blog_category entries.
How do I just select distint the joined table fields?
In OrmLite the SqlExpression is what builds the SQL query you want executed:
var q = Db
.From<Blog>()
.LeftJoin<BlogToBlogCategory>()
.Join<BlogToBlogCategory, BlogCategory>()
.Where( .. )
.SelectDistinct();
Whilst the API use to execute the query maps the result back to the specified model, in this case you're saying you want the results of the above query mapped to BlogCategory POCOs:
var results = Db.Select<BlogCategory>(q);
But you need to make sure that the query you create returns results that can map to the POCO you've specified.
If you only want to select distinct BlogCategory columns you'll need to do this in your SqlExpression:
var q = Db
.From<Blog>()
.LeftJoin<BlogToBlogCategory>()
.Join<BlogToBlogCategory, BlogCategory>()
.Where( .. )
.SelectDistinct<BlogCategory>(c => c);
Or if you want to select columns across different joined tables you'd use a standard anonymous type expression:
var q = Db
.From<Blog>()
.LeftJoin<BlogToBlogCategory>()
.Join<BlogToBlogCategory, BlogCategory>()
.Where( .. )
.SelectDistinct<Blog,BlogCategory>((b,c) => new { b.Id, c.Name, ... });
Then the custom model you map the results to should have properties that match the returned columns:
var results = db.Select<BlogCategoryResult>(q);
Alternatively you can access the custom result set with the Dynamic Result Set APIs.

adding limit to my sequelize query is throwing an error

this works properly
let data=await db.company.findAll({
include:{model:db.user,as:'users'},
where:{'$users.id$':7}
});
but as soon as i add limit below, it throws an error
let data=await db.company.findAll({
limit:10,
include:{model:db.user,as:'users'},
where:{'$users.id$':7}
});
here's the error when i execute the one with limit.
{
name: "SequelizeDatabaseError",
parent: {
message: "The multi-part identifier "users.id" could not be bound.",
code: "EREQUEST",
number: 4104,
state: 1,
class: 16,
serverName: "LAPTOP-5ED24571",
procName: "",
lineNumber: 1,
sql: "SELECT [company].*, [users].[id] AS [users.id], [users].[username] AS [users.username], [users].[name] AS [users.name], [users].[address] AS [users.address], [users].[active] AS [users.active], [users].[email] AS [users.email], [users].[phone] AS [users.phone], [users].[job_role] AS [users.job_role], [users].[time_zone] AS [users.time_zone], [users].[country] AS [users.country], [users].[currency] AS [users.currency], [users].[induction] AS [users.induction], [users].[createdAt] AS [users.createdAt], [users].[updatedAt] AS [users.updatedAt], [users].[userRoleId] AS [users.userRoleId], [users->user_company_mm].[id] AS [users.user_company_mm.id], [users->user_company_mm].[userId] AS [users.user_company_mm.userId], [users->user_company_mm].[companyId] AS [users.user_company_mm.companyId], [users->user_company_mm].[createdAt] AS [users.user_company_mm.createdAt], [users->user_company_mm].[updatedAt] AS [users.user_company_mm.updatedAt] FROM (SELECT [company].[id], [company].[name], [company].[address], [company].[city], [company].[country], [company].[state_region], [company].[logo], [company].[numberOfLocations], [company].[numberOfEmployees], [company].[audience_builder], [company].[creator_user_id], [company].[tags], [company].[createdAt], [company].[updatedAt] FROM [company] AS [company] WHERE [users].[id] = 7 ORDER BY [company].[id] OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY) AS [company] LEFT OUTER JOIN ( [user_company_mm] AS [users->user_company_mm] INNER JOIN [user] AS [users] ON [users].[id] = [users->user_company_mm].[userId]) ON [company].[id] = [users->user_company_mm].[companyId];"
}
Any idea why this is happening? thank you
I'm not necessarily familiar with sequelize.js. But looking at your query statement, you have a WHERE condition in your sub query limiting it to [Users].[Id] = 7. But this doesn't exist in this part of your query context.
...
FROM
(SELECT [company].[id],
[company].[name],
[company].[address],
[company].[city],
[company].[country],
[company].[state_region],
[company].[logo],
[company].[numberOfLocations],
[company].[numberOfEmployees],
[company].[audience_builder],
[company].[creator_user_id],
[company].[tags],
[company].[createdAt],
[company].[updatedAt] FROM [company] AS [company]
/* HERE --> */ WHERE [users].[id] = 7 ORDER BY [company].[id] OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY
)
AS [company] LEFT OUTER JOIN
( [user_company_mm] AS [users->user_company_mm] INNER JOIN [user]
AS [users] ON [users].[id] = [users->user_company_mm].[userId]) ON [company].[id] = [users->user_company_mm].[companyId]
But why the query is structured as so, I can't tell. That would be something for how you've configured it/how sequalized works.

How can I write this query using TypeORM createQueryBuilder?

I want to write this raw query using the TypeORM createQueryBuilder. How can I achieve this?
SELECT *
FROM Location l
INNER JOIN LocationType t ON l.TypeId = t.Id
LEFT JOIN Location p ON l.ParentId = p.Id
LEFT JOIN (SELECT locationId, SUM(Units) TotalUnits FROM ItemInventory GROUP BY LocationId) qty ON l.Id = qty.LocationId`

Inner Join with more than a field

Precise to do a select with inner join that has relationship in more than a field among the tables
Exemple:
DataSet dt = new Select().From(SubConta.Schema)
.InnerJoin(PlanoContabilSubConta.EmpSubContaColumn, SubConta.CodEmpColumn)
.InnerJoin(PlanoContabilSubConta.FilSubContaColumn, SubConta.CodFilColumn)
.InnerJoin(PlanoContabilSubConta.SubContaColumn, SubConta.TradutorColumn)
.Where(PlanoContabilSubConta.Columns.EmpContabil).IsEqualTo(cEmp)
.And(PlanoContabilSubConta.Columns.FilContabil).IsEqualTo(cFil)
.And(PlanoContabilSubConta.Columns.Conta).IsEqualTo(cTrad)
.ExecuteDataSet();
But the generated sql is wrong:
exec sp_executesql N'/* GetDataSet() */ SELECT [dbo].[SubContas].[CodEmp], [dbo].[SubContas].[CodFil], [dbo].[SubContas].[Tradutor], [dbo].[SubContas].[Descricao], [dbo].[SubContas].[Inativa], [dbo].[SubContas].[DataImplantacao]
FROM [dbo].[SubContas]
INNER JOIN [dbo].[PlanoContabilSubContas] ON [dbo].[SubContas].[CodEmp] = [dbo].[PlanoContabilSubContas].[EmpSubConta]
INNER JOIN [dbo].[PlanoContabilSubContas] ON [dbo].[SubContas].[CodFil] = [dbo].[PlanoContabilSubContas].[FilSubConta]
INNER JOIN [dbo].[PlanoContabilSubContas] ON [dbo].[SubContas].[Tradutor] = [dbo].[PlanoContabilSubContas].[SubConta]
WHERE EmpContabil = #EmpContabil0
AND FilContabil = #FilContabil1
AND Conta = #Conta2
',N'#EmpContabil0 varchar(1),#FilContabil1 varchar(1),#Conta2 varchar(1)',#EmpContabil0='1',#FilContabil1='1',#Conta2='1'
What should be made to generate this sql?
exec sp_executesql N'/* GetDataSet() */ SELECT [dbo].[SubContas].[CodEmp], [dbo].[SubContas].[CodFil], [dbo].[SubContas].[Tradutor], [dbo].[SubContas].[Descricao], [dbo].[SubContas].[Inativa], [dbo].[SubContas].[DataImplantacao]
FROM [dbo].[SubContas]
INNER JOIN [dbo].[PlanoContabilSubContas] ON [dbo].[SubContas].[CodEmp] = [dbo].[PlanoContabilSubContas].[EmpSubConta] AND
[dbo].[SubContas].[CodFil] = [dbo].[PlanoContabilSubContas].[FilSubConta] AND
[dbo].[SubContas].[Tradutor] = [dbo].[PlanoContabilSubContas].[SubConta]
WHERE EmpContabil = #EmpContabil0
AND FilContabil = #FilContabil1
AND Conta = #Conta2
',N'#EmpContabil0 varchar(1),#FilContabil1 varchar(1),#Conta2 varchar(1)',#EmpContabil0='1',#FilContabil1='1',#Conta2='1'
Looks like you can't join on multiple columns in SubSonic 2, you could just create a SQL view that joins as you want it to and then get SubSonic to generate a model for that view.

Resources