web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

create salesLine through code

(0) ShareShare
ReportReport
Posted on by 30

Dear All,

AS per customer requirement i added one button in Sales Order form "Import Lines".

When user clicked on button "Import Lines" then it will ask for browse file to import.

Then select file and OK.

It's properly inserted lines in to Sales Line.

Then user go to Document Status > Release. Once clicked then it will throw an error

8546.a3.png

8546.a3.png

******

NOTE :

If i closed the Sales Order form and again clicked on Document Status > Release. then it will Released .

It work absolutely fine.

I think it's refresh issue????

******

Here is my code :

Button > ImportLines

Clicked() :

void clicked()
{
Args args = new Args();
;
element.InsertSalesLine();
super();
}

void InsertsalesLine()
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
System.DateTime ShlefDate;
FilenameOpen filename;

DialogField dialogFilename;
Dialog dialog;

PriceDiscTable priceDiscTable;
int row;
int i;
str salesId;

PriceDiscItemRelation itemId, item;
SalesOrderedQty qty;
str name;
SalesUnit UOM, fromUnit,toUnit;
str crateCheck;
HSNCode_IN hsnCode;

#Excel

// For real values
str COMVariant2Str(COMVariant _cv,
int _decimals = 0,
int _characters = 0,
int _separator1 = 0,
int _separator2 = 0)
{
switch(_cv.variantType())
{
case (COMVariantType::VT_BSTR):
return _cv.bStr();

case (COMVariantType::VT_R4):
return num2str(_cv.float(),
_characters,
_decimals,
_separator1,
_separator2);

case (COMVariantType::VT_R8):
return num2str(_cv.double(),
_characters,
_decimals,
_separator1,
_separator2);

case (COMVariantType::VT_DECIMAL):
return num2str(_cv.decimal(),
_characters,
_decimals,
_separator1,
_separator2);

case (COMVariantType::VT_DATE):
return date2str(_cv.date(),
123,
2,
1,
2,
1,
4);

case (COMVariantType::VT_EMPTY):
return "";

default:
throw error(strfmt("@SYS26908",
_cv.variantType()));

}
return "";
}
// end

;
dialog = new Dialog("Sales line Upload");
dialogFilename = dialog.addField(typeId(FilenameOpen));
dialog.filenameLookupFilter(["@SYS28576",#XLSX,"@SYS28576",#XLS]);
dialog.filenameLookupTitle("Upload from Excel");
dialog.caption("Sales line Upload");
dialogFilename.value(filename);

if(!dialog.run())
return;

filename = dialogFilename.value();
application = SysExcelApplication::construct();
workbooks = application.workbooks();

try
{
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error("File cannot be opened.");
}
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();


row = 1;
try
{
do
{
ttsbegin;
row++;

itemId = cells.item(row, 1).value().bStr();
name = cells.item(row, 2).value().bStr();
qty = str2num(ComVariant2str(cells.item(row,3).value()));
UOM = cells.item(row, 4).value().bStr();
crateCheck = cells.item(row, 5).value().bStr();

salesId = salesTable.SalesId;
salesTable = SalesTable::find(salesId);

salesLine.clear();
salesLine.initValue();
salesLine.initFromSalesTable(salesTable);

// Validate Item ID
salesLine.ItemId = itemId;
salesLine.validateField(fieldnum(salesLine, ItemId));
salesLine.modifiedField(fieldnum(salesLine, ItemId));

// Validate HSN code
hsnCode = inventTable::find(itemId).HSNCode_IN;

If(!hsnCode)
{
info(strfmt("HSN code is not present for Item ID : %1",ItemId));
}

// check HSN code is NOT BLANK ****
if(itemId && hsnCode != "")
{
// Validate Quantity
salesLine.SalesQty = qty;
salesLine.validateField(fieldnum(salesLine, SalesQty));
salesLine.modifiedField(fieldnum(salesLine, SalesQty));

// BypassCrate

if(crateCheck == "Yes")
{
salesLine.ByPassCrateCheck = noyes::Yes;
}
else
{
salesLine.ByPassCrateCheck = noyes::No;
}

// Validate Sales unit
salesLine.SalesUnit = UOM;
salesLine.validateField(fieldnum(salesLine, SalesUnit));
salesLine.modifiedField(fieldnum(salesLine, SalesUnit));

// Create Lines ****
salesLine.createLine(NoYes::Yes,// Validate
NoYes::Yes, // initFromSalesTable
NoYes::Yes, // initFromInventTable
NoYes::Yes, // calcInventQty
NoYes::Yes, // searchMarkup
NoYes::No); // searchPrice

// Update forcefully excel Unit in to Sales Unit
salesLine.SalesUnit = UOM;

// salesPrice
while select ItemRelation,ItemCode,AccountRelation,ToDate,Amount from priceDiscTable
where priceDiscTable.ItemRelation == itemId
&& priceDiscTable.ItemCode == TableGroupAll::Table
&& priceDiscTable.AccountRelation == salesTable.PriceGroupId
&& priceDiscTable.ToDate == global::dateNull()
{
if(priceDiscTable)
{
salesLine.SalesPrice = priceDiscTable.Amount;
}
}
// end


// Update Lines ****
salesLine.selectForUpdate(true);
salesLine.update();
i++;
ttscommit;
}// end if
type = cells.item(row+1, 1).value().variantType();
}
while(type != COMVariantType::VT_EMPTY);
application.quit();

info(strfmt("%1",int2str(i)+' '+'Records are inserted sussessfully'));
}
catch
{
Error("Upload Failed");
}
SalesLine_ds.refresh();
SalesLine_ds.research();
// end
}

Can you please let me know how to reslove this issue.

Please give me more shed on this.

Thanks!

Arpan

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Chaitanya Golla Profile Picture
    17,225 on at

    Hi,

    Please include below statement after line:while(type != COMVariantType::VT_EMPTY);

                                                                      application.quit();

    salesTable_ds.executeQuery() (or) salesTable_ds.research(true) (or) salesTable_ds.reread();salesTable_ds.refresh();

  • @rp@n Profile Picture
    30 on at

    Thanks Chaitanya,

    As suggested by you i included all those one by one but it's showing same error in all cases.

    Please give me more shed on this.

    Thanks!

    Arpan

  • @rp@n Profile Picture
    30 on at

    Thanks Sukrut,

    As suggested by you. I did

    It's working fine.

    Thanks!

    Arpan

  • @rp@n Profile Picture
    30 on at

    Hi Sukrut,

    As we discussed , in SalesLine all lines are inserted/ updated properly.

    ******
    Only i am facing one issue. When user clicked on Tax Information button then in "Customer Tax Information" tab In Name and Address it's not showing Customer name and  Address automatically.

    But it's showing in lookup. When user select then it's updated properly.

    4048.a2.png

    4048.a2.png

    Note : Customer wants to update Name and Address in Customer Tax Information tab automatically means when import data in to SalesLine.

    Can you please let me know how to do this.

    Please give me more shed on this.

    Thanks!

    Arpan

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans