Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

update line quantity using x++ code

Posted on by 1,457

I was trying to update line quantity using dialog form. here I have use using purch id i got my line number filter but for specific line number if i want to update quantity  using ok button . here record should not updated if the sum of purchase quantity and  remainder quantity tab shown in my form is greater than purchLine.PurchQty.

so i am using purchLine.invoicedInTotal() for purchase quantity and purchLine.RemainPurchFinancial for remainder quantity  and there sum should not be greater than  purchLine.PurchQty.  if  sum is greater my code should show message "record cannot be updated" it sum is smaller or equal the record should get update when i click ok. what  i am using if statement in my click method the code is below 

dont know how to get it done , I am complete begineer in forms can anyone plz help me out.

 1145.pastedimage1666074535795v1.png

  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    RE: update line quantity using x++ code

    using this code I am able to update invoiced field but i need to update invoice remainder field also.

    void clicked()
    {
        PurchLine  purchLine;
        VendInvoiceTrans    vendInvoiceTrans;
        PurchQty qtyPurch = 0;
          ttsBegin;
        
        select  PurchId,LineNumber from purchLine
            where purchLine.PurchId == PurchaseId.valueStr()
             &&   purchLine.LineNumber == str2int64(LineNumber.valueStr());
        
        if(purchLine.InventTransId)
        {
        
           if(RemainderQuantity.realValue()   PurchaseQuantity.realValue()  > purchLine.PurchQty)         
            {
             info(strFmt("value is greater than quantity %1",purchLine.PurchQty));
            }
            
            else
            {
                
          purchLine.PurchQty = RemainderQuantity.realValue()   PurchaseQuantity.realValue();    
         
                select forUpdate vendInvoiceTrans
                 where vendInvoiceTrans.InventTransId == purchLine.InventTransId;
            vendInvoiceTrans.Qty =  RemainderQuantity.realValue() ;
                
            purchLine.RemainPurchFinancial = PurchaseQuantity.realValue();
            vendInvoiceTrans.update();  
            
            info(strFmt(" Quantity updated %1",purchLine.PurchQty));      
            }
        }
          ttsCommit;
        super();
    }
     

    pastedimage1667898824228v1.png

    how i can get value in invoice remainder field and my condition is getting filled that sum of both field should be not more than quantity 

  • Verified answer
    martinarul Profile Picture
    martinarul 217 on at
    RE: update line quantity using x++ code

    Hi Dineshkarlekar,

    You cannot update the invoice quantity just like that, if you update the invoice quantity it will have financial impact.

    Elaborate your issue.

  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    RE: update line quantity using x++ code

    I want to update  invoiced quantity if my sum is less or equal to purchQty ,

    This field is using method

     

    PurchLine.InvoicedInTotal()

    this method takes data from code 

    display PurchQty   invoicedInTotal()
    {
        PurchLine   purchLine;
    
        if (!purchLine.InventTransId)
        {
            return 0;
        }
        return purchLine.invoicedInTotalServer();
    }

    again it takes data from another method having code

    server PurchQty invoicedInTotalServer()
    {
        return this.type().invoicedInTotal();
    }

    which takes data from code 

    display PurchQty invoicedInTotal()
    {
        PurchQty qtyPurch = 0;
    
        if (purchLine.InventTransId)
        {
            qtyPurch = (select sum(Qty) from vendInvoiceTrans
                            where vendInvoiceTrans.InventTransId == purchLine.InventTransId).Qty;
        }
    
        return qtyPurch;
    }

    do I need to update here if  I want to update Invoice field in table. do you have any suggestion on this.

    pastedimage1667889561821v1.png

  • Verified answer
    martinarul Profile Picture
    martinarul 217 on at
    RE: update line quantity using x++ code

    Hi Dineshkarlekar,

    Check if you have any pending invoices against that particular PO and delete them if present, then try your process

  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    RE: update line quantity using x++ code

    hi Grygory Ivanov,
    Thanks for reply , I will definately try this using your code

    now I have written my code on clicked method every thing is going good but I am getting error when

    I try to update my quantity can you please help me out with it.

    void clicked()
    {
        PurchLine  purchLine;
        
          ttsBegin;
        
        select  PurchId,LineNumber from purchLine
            where purchLine.PurchId == PurchaseId.valueStr()
             &&   purchLine.LineNumber == str2int64(LineNumber.valueStr());
        if(purchLine)
        {
        
            if(RemainderQuantity.realValue()   PurchaseQuantity.realValue()  > purchLine.PurchQty)
            {
             info(strFmt("value is greater than quantity %1",purchLine.PurchQty));
            }
            else
            {
            purchLine.PurchQty = RemainderQuantity.realValue()   PurchaseQuantity.realValue();
            purchLine.update();
            info(strFmt("Quantity updated"));
            }
        }
          ttsCommit;
        super();
    }

    pastedimage1667803371801v1.png

  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,457 on at
    RE: update line quantity using x++ code

    hi Martin , thanks for reply

    , I will do check with that .

  • Verified answer
    Grygory.Ivanov Profile Picture
    Grygory.Ivanov 160 on at
    RE: update line quantity using x++ code

    Hello Dineshkarlekar,

    You don't really need a form that is called Dialog for this. You can just create dialog-type object on the go within a job or a class.
    learn.microsoft.com/.../using-classes-to-create-a-dialog

    So at first you initialize your dialog fields. It seems you need purchId entered by the user - so you just need that field and line number then.

    It is hard to tell what exactly you are trying to update in the end from your explanation - but I assume it is PurchQty field. You can replace that bit in your code with what you need. If you are trying to update delivry reminder - there is a standard AX 2012 function for that, too.

    So the code below should get you closer to what you wanted (this is AX 2012 Job object):

    static void community_forum_468941(Args _args)
    {        
        PurchLine               purchLine;
        
        Dialog                  dialog  = new Dialog();    
        DialogField             dialogFieldPOid, dialogPOLineNum;
        
        LineNum                 purchLineNum;
        PurchId                 purchOrderNum;
    
        ;
        //we create a Dialog object first and initialize fields there
        dialog.caption("Update PO record");
        dialogFieldPOid = dialog.addField(extendedTypeStr(PurchId),"Purchase order to check");
        dialogPOLineNum = dialog.addField(extendedTypeStr(LineNum),"PO line number to check");
        
        //then, we check if OK or Cancel was pressed and if OK, proceed to the logic
        if(dialog.run())
        {
            //grabbing user-entered values from Dialog fields
            purchOrderNum   = dialogFieldPOid.value();
            purchLineNum    = dialogPOLineNum.value();
            
            //working on the actual update - creating transaction update pair: https://learn.microsoft.com/en-us/dynamicsax-2012/developer/x-standards-ttsbegin-and-ttscommit
            ttsBegin;
            
            //finding the proper purchase order and its line based on user-entered values. Note that we use FORUPDATE statement to allow updates to the object later
            select forUpdate purchLine 
                where   purchLine.PurchId &&
                        purchLine.LineNumber == purchLineNum;
            
            //if we were able to find the record, execute validation and business logic. if not - throw meaningful error message
            if(purchLine)
            {
                if((purchLine.RemainPurchFinancial    purchLine.invoicedInTotal()) >= purchLine.PurchQty)  
                {
                    info(strFmt("Record for Purchase order %1 with line number %2 cannot be updated.",purchOrderNum,purchLineNum));
                }
                else
                {
                    purchLine.PurchQty = purchLine.RemainPurchFinancial    purchLine.invoicedInTotal();
                    purchLine.update();
                }
            }
            else
            {
                error(strFmt("Record for Purchase order %1 with line number %2 is not found.",purchOrderNum,purchLineNum));
            }
            ttsCommit;
        }
        else
        {
            return;
        }   
    }
    [View:/cfs-file/__key/communityserver-discussions-components-files/33/Job_5F00_community_5F00_forum_5F00_468941.xpo:320:240

    Hope this helps with your learning!

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: update line quantity using x++ code

    Please start by compiling your code and fixing compilation errors.

    Then notice that you've never populated the purchLine variable, therefore purchLine.PurchQty will always be zero.

    Now let me move your question to the right forum.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans