Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / copy multipel sales or...
Finance forum
Unanswered

copy multipel sales order from SalesParmTable

Posted on by 52
Hello everyone.
I need to duplicate the sales orders (from SalesParmTable) for which the user is generating a proforma for a certain project, and then I need to re-insert those sales orders in SalesParmtable once the proforma has been created.
below is my code 
//Hold the SalesParmTable record         protected void endParmUpdate()    {        SalesParmTable salesParmTableCopy;        SalesParmTable salesParmtable;        SalesParmLine  salesParmLineCopy;        SalesParmLine  salesParmLine;        SalesParmUpdate salesParmUpdate;        RecordInsertList list = new RecordInsertList(tablenum(SalesParmLine));        RecordInsertList listSalesParmTable = new RecordInsertList(tablenum(SalesParmTable));        formletterParmUpdate   formletterParmUpdate = this.formletterParmUpdate;        FormletterParmTable     formletterParmTable = this.formletterParmTable;               if (formletterParmUpdate.TableId != tableNum(FormletterParmUpdate)) //Ensure that the map is initialized        {                        ttsbegin;            select firstonly forupdate formletterParmUpdate                where formletterParmUpdate.ParmId == parmId;                       select salesParmUpdate                where salesParmUpdate.ParmId == parmId;              if (formletterParmUpdate)            {                if (this.canDeleteFormletterUpdate() && this.proforma && salesParmUpdate.xxProFormaInvoiceNumber == NoYes::Yes)                {                   while select salesParmtable order by SalesId                        where salesParmtable.ParmId == parmId                    {                        salesParmTableCopy.data(salesParmtable);                        listSalesParmTable.add(salesParmTableCopy);                        while select * from salesParmLine                            where salesParmtable.ParmId == salesParmLine.ParmId                            && salesParmtable.TableRefId == salesParmLine.TableRefId                        {                            salesParmLineCopy.data(salesParmLine);                            list.add(salesParmLineCopy);                        }                    }                }                          }            ttscommit;        }                    next endParmUpdate();        if (formletterParmUpdate.TableId != tableNum(FormletterParmUpdate)) //Ensure that the map is initialized gg        {            ttsbegin;                   if (formletterParmUpdate)            {                if (this.canDeleteFormletterUpdate() && this.proforma && salesParmUpdate.xxProFormaInvoiceNumber == NoYes::Yes)                {                    salesParmTableCopy.selectForUpdate(true);                    //salesParmTableCopy.insert();                    listSalesParmTable.insertDatabase();                    list.insertDatabase();                    salesParmTableCopy.reread();                }            }            ttscommit;        }    }
The issue is that, despite their being several records with the parmId, I always get just one record from SalesParmTable.
Any kind of help would be very much appreciated.
  • Community member Profile Picture
    Community member 52 on at
    copy multipel sales order from SalesParmTable
    I selected multiple sales orders to use to generate a proforma. Upon opening the SalesEditLine, I saw that those identical sales orders have the same parmId in the SalesParmTable.
    It seems like they were deleted before I ran my code because all I get when I try to copy them is only one sales order.
  • Martin Dráb Profile Picture
    Martin Dráb 223,370 Super User on at
    copy multipel sales order from SalesParmTable
    What exactly do you mean by "I picked multiple sales orders"?
     
    Also, are you saying that you expect mutiple SalesParmTable records created by they aren't, or they're deleted before you run your code? If you don't know, use the debugger to find it out.
     
     
  • Community member Profile Picture
    Community member 52 on at
    copy multipel sales order from SalesParmTable
    Hello Martin You are correct, even though I picked multiple sales orders, there is only one sales order when I check with SQL.
    i tried my code in SalesInvoiceJournalPost endUpdate() but with the same result.
    my task is When a user creates a proforma, is to copy every line in the header (SalesParmTable) and every line in the SalesParmLine when a user creates a proforma, and re-insert it when the SalesEditLines form is closed.
  • Martin Dráb Profile Picture
    Martin Dráb 223,370 Super User on at
    copy multipel sales order from SalesParmTable
    If I understand it correctly, this is the code you're struggling with.
    [ExtensionOf(classStr(FormletterService))]
    final class MyFormletterService_Extension
    
    protected void endParmUpdate()
    {
        SalesParmTable salesParmTable;
    
        while select salesParmTable
            where salesParmtable.ParmId == parmId
        {
            info(salesParmTable.SalesId);
        }
    
        next endParmUpdate();
    }
    The rest of code isn't relevant in this case and therefore it would just distract us from the current issue.
     
    How did you verify that there multiple records in database for the given parm ID and company at that time? Did you, for example, stop execution on breakpoint and run a SQL query without transaction isolation? I doubt you'd get a different result then from X++.
     
    Putting your code FormletterService sounds wrong to me. It's a generic class for all postings for both purchase and sales, while your code is clearly just for sales and likely just for a particular type of posting (invoice). If so, you should use a class for invoice posting of sales orders, such as SalesInvoiceJournalPost.
  • Community member Profile Picture
    Community member 52 on at
    copy multipel sales order from SalesParmTable

    Hello Martin I thank you for your quick reply, i have extended the FormletterService.
    When I debug, I can see that I have the correct parmId, but SalesParmTable does not loop through; instead, it appears as though there is just one record, even though there are multiple records with that parmId when I check SalesParmTable.

     i can finally reply but i can't insert any code.

    //Hold the SalesParmTable record 
            protected void endParmUpdate()
        {
            SalesParmTable salesParmTableCopy;
            SalesParmTable salesParmtable;
            SalesParmLine  salesParmLineCopy;
            SalesParmLine  salesParmLine;
            SalesParmUpdate salesParmUpdate;
            RecordInsertList list = new RecordInsertList(tablenum(SalesParmLine));
            RecordInsertList listSalesParmTable = new RecordInsertList(tablenum(SalesParmTable));
            formletterParmUpdate   formletterParmUpdate = this.formletterParmUpdate;
            FormletterParmTable     formletterParmTable = this.formletterParmTable;
           
            if (formletterParmUpdate.TableId != tableNum(FormletterParmUpdate)) //Ensure that the map is initialized
            {
                
                ttsbegin;
                select firstonly forupdate formletterParmUpdate
                    where formletterParmUpdate.ParmId == parmId;
               
                select salesParmUpdate
                    where salesParmUpdate.ParmId == parmId;
      
                if (formletterParmUpdate)
                {
                    if (this.canDeleteFormletterUpdate() && this.proforma && salesParmUpdate.xxProFormaInvoiceNumber == NoYes::Yes)
                    {
                       while select salesParmtable order by SalesId
                            where salesParmtable.ParmId == parmId
                        {
                            salesParmTableCopy.data(salesParmtable);
                            listSalesParmTable.add(salesParmTableCopy);
                            while select * from salesParmLine
                                where salesParmtable.ParmId == salesParmLine.ParmId
                                && salesParmtable.TableRefId == salesParmLine.TableRefId
                            {
                                salesParmLineCopy.data(salesParmLine);
                                list.add(salesParmLineCopy);
                            }
                        }
                    }              
                }
                ttscommit;
            }
            
        
            next endParmUpdate();
            if (formletterParmUpdate.TableId != tableNum(FormletterParmUpdate)) //Ensure that the map is initialized gg
            {
                ttsbegin;
           
                if (formletterParmUpdate)
                {
                    if (this.canDeleteFormletterUpdate() && this.proforma && salesParmUpdate.xxProFormaInvoiceNumber == NoYes::Yes)
                    {
                        salesParmTableCopy.selectForUpdate(true);
                        //salesParmTableCopy.insert();
                        listSalesParmTable.insertDatabase();
                        list.insertDatabase();
                        salesParmTableCopy.reread();
                    }
                }
                ttscommit;
            }
        }

  • Martin Dráb Profile Picture
    Martin Dráb 223,370 Super User on at
    copy multipel sales order from SalesParmTable
    Please paste your code once more in a reply; this site has a bug causing formatting problems in the first post.
     
    Then tell us which class you extended and what you found when you debugged your code before giving up and asking here.

Helpful resources

Quick Links

Upcoming TechTalk: Copilot Capabilities…

Review the Copilot features that are currently available in Dynamics 365 Finance…

Demystifying Copilot: Service Edition with Sundar Raghavan

Sundar answers more questions about Copilot for Service...

Dynamics 365 Business Central vs Finance and SCM

Take a look at the key differences between Business Central and…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,405 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 223,370 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,140

Featured topics

Product updates

Dynamics 365 release plans