B1 Print and Delivery Conditional Action Current User - sapb1

I've got a piece of code here that should be working but is not, and for the life of me I have no idea why or a better way to go about it.
What I am trying to achieve is having B1 print and delivery determine who the current user is when emailing an A/R invoice, if its X user then it will send the email directly, if it's any other user then it will save as draft + preview. Below is my current code.
DECLARE #GetUser AS nvarchar(12)
SET #GetUser = (Select top 1 T1.U_NAME from USR5 T0 INNER JOIN OUSR T1 ON T0.UserCode = T1.USER_CODE where SessionID =##spid order by Date desc,Time desc)
IF(#GetUser = 'NickJ')
BEGIN
SELECT 'noDraft' FOR BROWSE
END
ELSE
BEGIN
SELECT 'draft' FOR BROWSE
END
When I run that select statement in SAP B1 query generator it shows me NickJ, so I know it works. However boyum and print and delivery seem to not like this query. When I click the email button its not detecting that NickJ is the current user and will instead do the ELSE action.
I also noticed that $[USERNAME] won't work in this conditional or report action.
Does anyone know of a better way of getting the current logged in user for this?

Just base everything of a SQL Query with a CASE selection like below.
Do not use Procedure...do not use C# or VB script. Use only the DocKey# Parameter coz thats the most common parameter used in most layouts. Just my observation to get it work. Bit late but posting for future reference. Hope ur issue is resolved by now.
This syntax is B1 HANA
Select case OINV."DocType" when 'I' then 'Items' else 'Service' end DocType
from OINV where OINV."DocEntry" = DocKey#

Related

Option to leave part of a searchable report/form blank in Microsoft access

I have a query in MS Access that is linked to a searchable report which is linked to a form. Part of the report ask users to include two reasons that will be used as criteria for a query. Currently, the report (screenshot) below does not allow for users to simply enter in 1 reason. Users must enter in 2 reasons, and if the reasons are the same, users must enter in the same reason for both fields. For example, If I only have one reason (conflict of Interest),I would need to enter that reason into both reason boxes shown on the screenshot below. I would like to be able to search for one reason while also being able to leave the other reason box blank. So simply entering in Conflict of interest for one box and have it run the query. The code I have for the query is below.
SELECT C1.ConsultID, C1.Consult_No, C1.Consult_Type, C1.Intake_Date, C1.Adult_Peds, C1.Title, C1.ConsultSummary
FROM ([Intersection Query Pre-Reasons] AS C1 LEFT JOIN tblConsultReasons AS C2 ON C1.ConsultID = C2.ConsultID) LEFT JOIN tblConsultReasons AS C3 ON C2.ConsultID = C3.ConsultID
WHERE (((C2.Reason)=Forms!FrmReasonsCriteria!Reason1) And ((C2.ReasonType)="Discerned")) And (C3.Reason)=Forms!FrmReasonsCriteria!Reason2 And C3.ReasonType="Discerned";
How would you recommend I change the code in order to do this. I believe adding a length condition to the second half of the where clause may work. But am not sure what this would look like.
Screenshot of part of the searchable report that queries on reasons
It will be something like this; I haven't tested it because that requires building forms etc and I don't have the time atm:
SELECT C1.ConsultID, C1.Consult_No, C1.Consult_Type, C1.Intake_Date, C1.Adult_Peds, C1.Title, C1.ConsultSummary
FROM ([Intersection Query Pre-Reasons] AS C1
LEFT JOIN tblConsultReasons AS C2 ON C1.ConsultID = C2.ConsultID)
LEFT JOIN tblConsultReasons AS C3 ON C1.ConsultID = C3.ConsultID
WHERE
NOT (nz(Forms!FrmReasonsCriteria!Reason1,'')='' And
nz(Forms!FrmReasonsCriteria!Reason2,'')=''
)
And (
(nz(C2.Reason=Forms!FrmReasonsCriteria!Reason1,'')=''
And C2.ConsultID is null)
Or
(nz(C2.Reason=Forms!FrmReasonsCriteria!Reason1,'')<>'' And
C2.Reason=Forms!FrmReasonsCriteria!Reason1 And
C2.ReasonType="Discerned")
)
And (
(nz(C3.Reason=Forms!FrmReasonsCriteria!Reason2,'')=''
And C3.ConsultID is null)
Or
(nz(C3.Reason=Forms!FrmReasonsCriteria!Reason2,'')<>'' And
C3.Reason=Forms!FrmReasonsCriteria!Reason2 And
C3.ReasonType="Discerned")
)
It can be simplified a bit if we know the data available, and other controls/checks the search boxes may have.

Using Microsoft Query in Excel to Add Date Parameters to SQL Query

I often use the function (now legacy) in Excel to get data from SQL Server where I paste an actual SQL statement into the Excel sheet and it works fine. I have been researching how to do this with queries I have that have date parameters that need to be changed each time a report is ran and at first it seemed like using Microsoft Query in Excel would be the best option. This would use the '?' instead of the dates themselves and allow for adding parameters. Whenever I try to do this with the below query I get the error "Parameters are not allowed in queries that can't be displayed graphically." I honestly have no idea what that means but would value any input. My Query is below. Thanks
SELECT E.TEAM_MEMBER_NAME AS 'PURCHASER',
M.DEPARTMENT,
M.BUSINESS_SEGMENT_CODE,
KB.BUSINESS_SEGMENT_DESC,
KG.GENDER_DESC,
MR.PLANT_CODE [PLANT],
MR.STOCK_CATEGORY,
M.MATERIAL,
M.[DESCRIPTION],
M.COLOR_1,
M.COLOR_2,
MR.SIZE_LITERAL,
MR.QUANTITY,
M.STANDARD_COST,
M.DEALER_PRICE,
M.CURRENT_SEASON,
MR.STOCK_NUMBER AS 'AFS PO #',
H.PO_CREATED_BY,
H.PO_TYPE,
MR.MRP_INDICATOR,
MR.STOCK_TYPE,
H.PO_ISSUE_DATE
FROM PDX_SAP_USER..VW_MRP_ALLOCATION MR
JOIN PDX_SAP_USER..VW_MM_MATERIAL M ON MR.MATERIAL = M.MATERIAL
JOIN PDX_SAP_USER..VW_KD_BUSINESS_SEGMENT KB ON M.BUSINESS_SEGMENT_CODE = KB.BUSINESS_SEGMENT_CODE
JOIN PDX_SAP_USER..VW_KD_GENDER KG ON M.GENDER_CODE = KG.GENDER_CODE
JOIN PDX_SAP_USER..VW_PO_HEADER H ON MR.STOCK_NUMBER = H.PO_NUMBER
JOIN ADI_USER_MAINTAINED..SCM_PO_EMPLOYEE_NAME E ON MR.STOCK_NUMBER = E.PO_NUMBER
WHERE M.BUSINESS_SEGMENT_CODE NOT IN ('420','421','422','424')
AND MR.STOCK_CATEGORY NOT LIKE 'A60383%'
AND MR.STOCK_CATEGORY NOT IN ('A60382001','A60380070')
AND M.MATERIAL NOT IN ('AY1480','CD4683')
AND H.PO_TYPE NOT IN ('02','06','10','UB','DB')
AND MR.MRP_INDICATOR IN ('A','N')
AND MR.STOCK_TYPE = 'B'
AND MR.QUANTITY >= 50
AND H.PO_ISSUE_DATE BETWEEN '09/26/2018' AND '10/10/2018'
ORDER BY MR.QUANTITY DESC
I got it to work...a bit of a work around. I had my DBA create a view on our server and selected all from that view. I then used MS Query to bring in the data and replaced the dates with ?. From there in the data source within Excel you can assign those question marks to cells in which you enter your dates. Works like a charm. And not taking credit - all credit goes to:
https://www.youtube.com/watch?v=xPalEw4xw1w
Short answer is that I've researched it at the time as well, trying my best to pass parameters (let's say, 'Sheet1!A1' cell) and couldn't. There is no real way to do it UNLESS you're using the Power Query as well as using an SQL stored procedure.
Do you think you can ask your DBA (or whomever is responsible for the database) to create a stored procedure for you in which you'd pass the date parameters? That's basically your only way to create a parameterised query.

Type conversion failure in update query

I'm fairly new to Access so this is driving me a little crazy.
I'm creating an inventory database and want to count the number of items in stock to update an ordering form. Received items are assigned an order code, and I want to count the number of instances of each order code found within the master table. I have a make table query which does this just fine:
SELECT PrimerList.PrimerName
, First(Primer_Master.FR) AS FR
, Primer_Master.OrderCode
, Count(Primer_Master.OrderCode) AS InStock
INTO PrimerOrder
FROM PrimerList
LEFT JOIN Primer_Master ON PrimerList.ID = Primer_Master.PrimerName
GROUP BY PrimerList.PrimerName
, Primer_Master.OrderCode
, Primer_Master.PrimerName
, Primer_Master.FR
, Primer_Master.Finished
HAVING ((([Primer_Master]![Finished])=No));
I want to use PrimerOrder to update an order list table PrimerOrderList which has all of the different possible order codes, updating the InStock value for records with matching OrderCode:
UPDATE PrimerOrderList
SET PrimerOrderList.InStock = PrimerOrder.InStock
WHERE (((PrimerOrderList.OrderCode)=[PrimerOrder].[OrderCode]));
However, when I try to run it I get parameter boxes which pop-up asking for PrimerOrder.OrderCode and PrimerOrderList.OrderCode. Even if I put in a valid value for each, I get a type conversion failure. I've checked the data types for both tables and don't see how there could be a type conversion failure - both are set to text.
Any insight would be greatly appreciated! Thanks in advance!
You haven't included the PrimerOrder table in your query. Should be:
UPDATE PrimerOrderList INNER JOIN PrimerOrder
ON PrimerOrderList.OrderCode = PrimerOrder.OrderCode
PrimerOrderList.InStock = PrimerOrder.InStock

Mystery Parametric SQL Query with joins + TClientDataSet blank fields

Some times even obvious things seem to go wrong. There's a lot of obvious and a lot that can go definitely wrong when dealing with SQL components and even more when they are just a ring of a whole SQLQuery => Provider => ClientDataSet => DataSource => DataControl chain.
Today's example is so incredibly dumb, yet a time waster.
How to replicate it:
Drop a TZQuery (ZeosLib) with a simple parametric join in its SQL, example:
SELECT
pr.product_id AS product_id,
pr.model AS model,
pd.name AS name,
pr.image AS image,
pr.status AS status,
pr.date_added AS date_added,
pr.date_modified AS date_modified
FROM oc_product pr
LEFT JOIN oc_product_description pd
ON pr.product_id = pd.product_id AND pd.language_id = :language_id
WHERE
pr.status = 1
ORDER BY
pd.name
Of course we have one parameter, :language_id.
Then drop a TDataSetProvider linked to it, then a TClientDataSet, then a TDataSource and finally a TDBGrid, all linked each to the previous. Finally drop a TDBNavigator and link it as well.
Finally, add fields in the TClientDataSet, captions etc.
At program startup we assign the parameter to the TZQuery component with something like:
qryProduct.Params.ParamByName('language_id').AsInteger := 2;
Where 2 is a demo hardcoded value (in the real application it is determined by querying the Windows current language being used).
Now run the the application: perfect!
Now press "Refresh" on the TDBNavigator.
Either you get a nasty: "Key Violation" or a perfectly blank name column in the TDBGrid.
Why?
The solution is so simple yet not so immediately evident.
Looking at the generated refresh SQL (via a TZSQLMonitor) the joined table it's evident the parameter is not working:
2014-01-04 11:19:38 cat: Execute, proto: mysql-5, msg: SELECT pr.product_id AS product_id,
pr.model AS model, pd.name AS name, pr.image AS image, pr.status AS status, pr.date_added
AS date_added, pr.date_modified AS date_modified FROM oc_product pr LEFT JOIN
oc_product_description pd ON pr.product_id = pd.product_id AND pd.language_id = NULL
WHERE pr.status = 1 ORDER BY pd.name
The parameter bit is:
pd.language_id = NULL
A quick glance to the ClientDataSet component would reveal that the parameter name has been captured from the query component. But not the value.
Now, I don't know if this is a problem with the TZQuery component not sending the parameter value at runtime or if the functionality is just not implemented by the ClientDataSet but the errors it generates are far from immediately evident.
The fix, of course, is to manually set the parameter value both in the TZQuery and in the TClientDataSet components before Active is set to true.

What is similar to CHANGE_TRACKING_IS_COLUMN_IN_MASK in CDC?

I am working on an application to synchronize data. We decided to use CDC in place of CT because we need more information, but at the same time we need a function which return the column which changing affects it something like this using CT:
SELECT
CTTable.SYS_CHANGE_OPERATION,
[FNameCh] = CHANGE_TRACKING_IS_COLUMN_IN_MASK(COLUMNPROPERTY(OBJECT_ID('CONTACT_TABLE'),
'contact_name', 'ColumnId'), SYS_CHANGE_COLUMNS),
[LNameCh] = CHANGE_TRACKING_IS_COLUMN_IN_MASK(COLUMNPROPERTY(OBJECT_ID('CONTACT_TABLE'),
'contact_lastname', 'ColumnId'), SYS_CHANGE_COLUMNS),
FROM CHANGETABLE (CHANGES Contacts, #VersionId) AS CTTable
LEFT OUTER JOIN Contacts AS C ON C.CONTACT_ID = CTTable.CONTACT_ID
GO
EDIT:
SELECT * , sys.fn_cdc_has_column_changed ('dbo_CONTACT_TABLE', 'contact_name', __$update_mask) AS FNameCh
From cdc.fn_cdc_get_all_changes_dbo_CONTACT_TABLE(#Start_Lsn,#End_Lsn,'all')
which 1 for if the contact_name is changed and 0 if is not.
Thanks
We can identify using sys.fn_cdc_is_bit_set. Please find the link below for detailed information
http://msdn.microsoft.com/en-us/library/bb500241.aspx