Lucas
03/17/2023, 7:33 AMSELECT
T.id AS 'Transaction ID',
T.trandate AS 'Transaction Date',
T.tranid AS 'Transaction Number',
T.entity AS 'Entity ID',
COALESCE(C.companyname, C.entityid, CustForJob.companyname, CustForJob.entityid) AS 'Customer Name',
CASE
WHEN C.parent IS NOT NULL THEN 'Sub Customer Record'
WHEN J.parent IS NOT NULL THEN 'Job Record'
WHEN J.id IS NOT NULL THEN 'Job or Project Record'
ELSE 'Direct Customer Record'
END AS 'Record Type',
J.id AS 'Job or Project ID',
COALESCE(J.companyname, J.entityid) AS 'Job or Project Name'
FROM
Transaction T
LEFT JOIN Customer C ON T.entity = C.id
LEFT JOIN Job J ON T.entity = J.id
LEFT JOIN Customer CustForJob ON J.customer = CustForJob.id
WHERE
T.type = 'CustInvc'
ORDER BY
T.trandate DESC;
This query uses the COALESCE function to return the 'companyname' field if available, and the 'entityid' field as a fallback for both Customer and Job records. This should resolve the issue of empty customer names for direct customer records.
and It also provided me an explanation:
Summary:
The provided query retrieves invoice details, including the transaction ID, transaction date, transaction number, entity ID, customer name or entity ID, record type (direct customer, sub customer, job or project, or sub job), job or project ID, and job or project name or entity ID for all invoice records in NetSuite. The query distinguishes between different types of records by joining the Transaction table with the Customer and Job tables, and uses the COALESCE function to handle cases where the customer name or job/project name is empty.JR
03/17/2023, 7:59 PMNElliott
03/20/2023, 8:17 AM