How to Insert a NEW Sales Order Using Code

sunnyksunnyk Member Posts: 280
Hi, I am trying to import TAB separated file consist of 4 columns. Each row is a New Sales Order. at the
Integer - Import::OnBeforeInsertRecord() i Wrote the following Code,
SalesHeader.SetHideValidationDialog(TRUE);
SalesHeader.INIT;
SalesHeader."Document Type" := SalesHeader."Document Type"::Order;
SalesHeader.INSERT(TRUE);
but for second record i got Error, that Sales Header already Exists.

Not sure what is wrong in this.

Answers

  • AKAK Member Posts: 226
    You forgot to assign a Number to the Sales Header.
    SalesHeader."No." := NumberReadFromFile;
    
  • loggerlogger Member Posts: 126
    Hi @sunnyk ,
    Unexpectedly INIT does not initialize primary key fields - "Primary key and timestamp fields are not initialized.". So, your code should be:

    SalesHeader.INIT;
    SalesHeader."Document Type" := SalesHeader."Document Type"::Order;
    SalesHeader."No." := '';
    SalesHeader.INSERT(TRUE);

    To get much info about INIT, read https://msdn.microsoft.com/en-us/library/dd354939.aspx
    Let's go!
  • latronamarranlatronamarran Member Posts: 24
    or you can try CLEAR(SalesHeader) instead of INIT + Primary Key = '';
  • sunnyksunnyk Member Posts: 280
    I used a Temp Table to fill in the records than from that Temp Table start creating the Sales Orders & It works that way. Thanks all for the help.
Sign In or Register to comment.