COC to add Supplementary items to purchase order/purch line

 Requirement:-    Supplementary items are nothing but hooked items for a master item/main item. means, if a use select one items if that item has its supplementary items that should automatically pop up in purch line. And if user delete master item supplementary items must be delete from lines. If user try to delete supplementary item from line which contains master along with the restrict the delete operation.

I'm doing COC on form data source level for ValidateWrite() because this is the method which trigger upon clicking the save. 

Code :-

[ExtensionOf(FormDataSourcestr(PurchTable, PurchLine))]

final class BASSupplimenteryItemInsert_Extension

{

    public boolean validateWrite()

    {

        boolean ret = next validateWrite();

        FormDataSource formDataSource = this;

        PurchLine purchLine = formDataSource.cursor();

        SuppItemTable supplementItem;


        select count(RecId) from supplementItem

            where supplementItem.ItemRelation == purchLine.ItemId;

        if(supplementItem.RecId != 0)

        {

            DialogButton  dialogButton;

            //Initial focus is on the No button.

            dialogButton = Box::yesNo('Do you want to add supplementery items',dialogButton::Yes, "Proceed or Dont");

            if(dialogButton == DialogButton::Yes)

            {

                PurchLine supplementLine;


                while select SuppItemId from supplementItem

                    where supplementItem.ItemRelation == purchLine.ItemId

                {

                    ttsbegin;

                    PurchTable purchTable = PurchTable::find(purchLine.PurchId);

                    supplementLine.initFromPurchTable(purchTable);

                    supplementLine.PurchId = purchLine.PurchId;

                    supplementLine.ItemId = supplementItem.SuppItemId;

                    // supplementLine.skipDataSourceValidateField(fieldNum(PurchLine,ItemId),true);

                    supplementLine.insert();

                    //supplementLine.createLine(true, true, true, true, true, false);

                    supplementLine.clear();

                    ttscommit;

                }


                //formDataSource.reread();

                formDataSource.refresh();

                // formDataSource.research();

                info("Supplementery items have been added");

            }

            else if (dialogButton == DialogButton::No)

            {

                ret = checkFailed("Supplementery items must be added");

            }

        }

        

        return ret;

    }


    public boolean validateDelete()

    {

        boolean ret;

        ret = next validateDelete();

        FormDataSource formDataSource = this;

        PurchLine selectedPurchLine = formDataSource.cursor();

        purchLine purchLine,purch;

        SuppItemTable supplementItem,supplyItem;


        //Master item validation

        //here ItemRelation field is master item and SuppitemId is Supplement Item

        while select ItemRelation,SuppItemId from supplementItem

            where supplementItem.ItemRelation == selectedPurchLine.ItemId

        {

            delete_from purchLine where purchLine.ItemId == supplementItem.SuppItemId

                && purchLine.PurchId == selectedPurchLine.PurchId;


            //ret = info(strFmt("Master item %1",selectedPurchLine.ItemId));

          

        }


        //Supplement item validation

        while select ItemRelation,SuppItemId from supplementItem

            where supplementItem.SuppItemId == selectedPurchLine.ItemId

        {

            while select ItemId from purchLine

                where purchLine.PurchId == selectedPurchLine.PurchId

                && purchLine.ItemId == supplementItem.ItemRelation

            {

                if(supplementItem.ItemRelation && purchLine.ItemId)

                {

                    ret = checkFailed(strFmt("cannot delete Item %1 has master item %2",selectedPurchLine.ItemId,purchLine.ItemId));

                }

            }

        }

      

        formDataSource.research();

        return ret;

    }


}

Comments

Popular posts from this blog

How to Create a wizard in x++ d365

X++ code to CREATE AND POST A TRADE AGREEMENT IN MICROSOFT DYNAMICS 365 in D365 F & O

x++ code to submit and approve and reject the invent movement workflow in d365 F&O