Select Sum method in X++ method

Hi all,

I’m trying to create a method in the SalesLine table which will return a sum total of the amount of confirmations associated with a SalesLine. However I’m running into a bit of bother with the return type - I assumed this would be an integer?

This is the method as it stands:

display int allConfirmations(SalesLine t)

{

CustConfirmTrans custConfirmTrans;

select sum(ConfirmId) from custConfirmTrans

where custConfirmTrans.InventTransId == t.InventTransId && custConfirmTrans.SalesId == t.SalesId;

return CustConfirmTrans.ConfirmId;

}

Is there something I’m missing here? It did occur to me I might be getting the return statement wrong also.

Hi,

I don’t know your AX version but it seems that you want to sum a string (confirmId). A display method does not take any parameters. you should use salesline instead of custConfirmTrans to sum amount values

try something like the following:

display AmountCur yourSum()

select sum(lineAmount) from SalesLine where blablabla

return salesLine.LineAmount;

Hope it helps, please verify solution if it solves your problem

regards,

Thomas

Hi Thomas,

This is actually a modification of an already working display method which takes SalesLine as a parameter - I believe it was so it would display the correct value for each line on a form?

I use AX 2012 R2, apologies for not stating it in the original post, I thought tagging the post would suffice

I understand what you mean about the string value - I didn’t realise Sum was trying to add all the contents together, I was actually looking for a method to count them? I have just tried changing Sum to count and it returns the same error however - is there a better field to be checking for counting them?

P.S. Your current solution would return the amount of lines on the sales order however, not the amount of confirmations associated with the line surely?

Are you looking for a count, if not please elaborate your requirement. Also specify the version of AX.

use select count(RecId) from table

Hi Kranthi,

I am looking for a count yes - will edit the original post now with your changes

I am still getting the same error however with the suggested changes, can you suggest what I’m doing wrong?

What error you are getting? can you show us the changes?

This worked perfectly! Thanks very much, very concise solution haha - I knew it was something stupid I was doing :slight_smile: