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 :
Finance | Project Operations, Human Resources, ...
Suggested Answer

how to get custInvoiceJour when invoice is posted?

(0) ShareShare
ReportReport
Posted on by 932

I want to attach file to invoice so : 

1. I add new field in salesparmTable 

2.I add this field in salesEditLines form : 

pastedimage1637665647871v1.png

3.When I click on button ok and checkBox"Archiver doc" =yes : 

pastedimage1637665743070v2.png

4.then when I post invoice I don't find the attach file  in account receivable > invoices > invoice journal : 

pastedimage1637666086413v3.png

this is my code : 

[FormControlEventHandler(formControlStr(SalesEditLines, OK), FormControlEventType::Clicked)]
    public static void OK_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        AsciiStreamIo                                   file;
        Array                                           fileLines;
        FileUploadTemporaryStorageResult                fileUpload;
        CustInvoiceJour   custInvoiceJour;
        DocuType          docuType;
        DocuRef           docuRef;
        DocuValue         docValue;
        FormCommandButtonControl  callerButton = sender as FormCommandButtonControl;  //Retrieves the button
        FormRun form = sender.formRun(); //Gets the running form
        //Get the salesParmTable datasource that was selected in  form
        FormDataSource salesParmTable_ds = form.dataSource(formDataSourceStr(SalesEditLines, SalesParmTable)) as FormDataSource;
        SalesParmTable salesParmTable = salesParmTable_ds.cursor();
            
        Info(strFmt("account: %1, check: %2 ",salesParmTable.InvoiceAccount,salesParmTable.ILBArchiveDoc));
        select * from custInvoiceJour where custInvoiceJour.InvoiceAccount==salesParmTable.InvoiceAccount && custInvoiceJour.SalesId==salesParmTable.SalesId;
        Info(strFmt("recId: %1 ",custInvoiceJour.RecId));
        if( salesParmTable.ILBArchiveDoc == NoYes::Yes )
        {
            fileUpload = file::GetFileFromUser() as FileUploadTemporaryStorageResult;
            Info(strFmt("nom du fichier %1",fileUpload.getFileName()));
            Info("good");
            ttsbegin;
            docValue.clear();
            docValue.FileName = fileUpload.getFileName();
            docValue.FileType = docuType.TypeId;
            docValue.insert();
            Info(strFmt("docValue %1",docValue.RecId));
            docuRef.clear();
            docuRef.RefCompanyId = custInvoiceJour.dataAreaId;
            docuRef.ValueRecId = docValue.RecId;
            docuRef.RefTableId = custInvoiceJour.TableId;
            docuRef.RefRecId = custInvoiceJour.RecId;
            docuRef.Name = 'Automatic file  for sales order 001512';
            docuRef.Notes = 'Added from X   by baw';
            docuRef.insert();
            ttscommit;
            info("Document note has been added successfully");
        }
        
    }

I have the same question (0)
  • BASMA Profile Picture
    932 on at

    have any idea about this?

  • Anton Venter Profile Picture
    20,345 Super User 2025 Season 2 on at

    Hi Basma,

    Your code is executed before the CustInvoiceJour record is created. Investigate and find a place to extend with your code after the CustInvoiceJour record has been created when posting. You will have to do this because it will take some time to figure it out.

  • Alex VN Profile Picture
    1,994 on at

    Hi,

    I think you are using onclicked event of the ok button but clicked the Ok button does not mean that you will get a CustInvoiceJour as there is other validation and actions need to be done before the invoice is posted.

    Hence I think you might need to check for the code when the CustInvoiceJour record is created and hook on the event after that.

    Regards,

    Anh Ong

  • BASMA Profile Picture
    932 on at

    Hello, thanks for reply ,

    I don't find exactly when the CustInvoiceJour record is created

    how to hook on the event after?

  • Alex VN Profile Picture
    1,994 on at

    Hi,

    I think for this one, the most simple place is Insert method on CustInvoiceJour table. You might also refer to SalesInvoiceJournalCreate & SalesInvoiceJournalPost.

    Hope this helps.

    Regards,

    Anh Ong

  • Suggested answer
    Suresh M. Parmar Profile Picture
    340 on at

    Hello Basma,

    As I have seen the requirement you are attaching the document at the time of invoicing. The attachment can be done after posting the invoice, that's standard functionality.

    pastedimage1637815845741v1.png

    If you are really want to use it at the time of invoicing then you need to call your code after invoice post only.

    What if the user wants to add multiple attachments or delete unwanted documents which are uploaded by mistake?

    Thanks,

    Suresh Parmar

  • Suggested answer
    Ludwig Reinhard Profile Picture
    Microsoft Employee on at

    Hi,

    The standard e-invoice feature can create the attachment out of the box without having to code something.

    Please have a look at this feature.

    Best regards,

    Ludwig

  • BASMA Profile Picture
    932 on at

    Thanks for reply ,

    @Ludwig Reinhard, I Have idea about e-invoice , but it's a task affected to me to code  automatic attach after the invoice is posted.

    @Suresh Parmar, when the user wants to add multiple attachments or delete unwanted documents which are uploaded by mistake he can do it manually.

  • BASMA Profile Picture
    932 on at

    I create an extension of table custInvoiceJour like this : 

    [ExtensionOf(tablestr(CustInvoiceJour))]
    final class ILBCustInvoiceJour_Extension
    {
    
        public void insert()
        {
            next insert();
    
        }
    
    }

    but I haven't any idea to pass datasource to class extension  because I need  a field from one datasource.

    this is the code that I set it in onclicked enventhandler : 

        public static void OK_OnClicked(FormControl sender, FormControlEventArgs e)
        {
            
            FormCommandButtonControl  callerButton = sender as FormCommandButtonControl;  //Retrieves the button
            FormRun form = sender.formRun(); //Gets the running form
            //Get the salesParmTable datasource that was selected in  form
            FormDataSource salesParmTable_ds = form.dataSource(formDataSourceStr(SalesEditLines, SalesParmTable)) as FormDataSource;
            SalesParmTable salesParmTable = salesParmTable_ds.cursor();
         }

  • BASMA Profile Picture
    932 on at

    Hello , 

    I create an extension of table custInvoiceJour : 

    then I use COC and set my code after next insert() like this : 

     public void insert()
        {
            next insert();
            SalesParmTable                                  salesparmTable;
            DocuType          docuType;
            DocuRef           docuRef;
            DocuValue         docValue;       
            select * from salesparmTable where salesparmTable.InvoiceAccount == this.InvoiceAccount 
                                         && salesParmTable.SalesId  == this.SalesId
                                         && salesParmTable.Ordering == DocumentStatus::Invoice;
            Info(strFmt("account %1, salesId %2, archive doc %3 ",salesparmTable.InvoiceAccount ,salesparmTable.SalesId, salesparmTable.ILBArchiveDoc));
            select * from docuType where docuType.ILBAutoArchive==NoYes::Yes;
            if(docuType && salesParmTable.ILBArchiveDoc == NoYes::Yes )
            {
                AsciiStreamIo                                   file;
                FileUploadTemporaryStorageResult                fileUpload;
                fileUpload = file::GetFileFromUser() as FileUploadTemporaryStorageResult;
                //Info(strFmt("nom du fichier %1",fileUpload.getFileName()));
                ttsbegin;
                docValue.clear();
                docValue.FileName = fileUpload.getFileName();
                docValue.FileType = docuType.TypeId;
                docValue.insert();
                Info(strFmt("docValue %1",docValue.RecId));
                docuRef.clear();
                docuRef.RefCompanyId = this.dataAreaId;
                docuRef.ValueRecId   = docValue.RecId;
                docuRef.RefTableId   = this.TableId;
                docuRef.RefRecId     = this.RecId;
                docuRef.TypeId       = docuType.TypeId;
                docuRef.Name         = 'Automatic file  for sales order added by baw';
                docuRef.Notes        = 'Added from X   ';
                docuRef.insert();
                ttscommit;
                info("Document note has been added successfully");
            }
            else if (!docuType && salesParmTable.ILBArchiveDoc == NoYes::Yes)
            {
                error("Erreur du paramétrage d’archivage");
            }
        }

    When I execute this code I get the following message error : 

    "Stack trace: Cannot run form on non interactive session."

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans