formDataFieldStr for modified and formDatasourcestr for validateWrite and validateDelete method using chain of command

// formDataFieldStr 

[ExtensionOf(formDataFieldStr(PurchTable, PurchLine, ItemId))]

final class BASSupplement_Extension
{
        public void modified()
    {
        FormDataObject formDataObject = any2Object(this) as FormDataObject;
        FormDataSource formDataSource = formDataObject.datasource();
        PurchLine purchLine;
        SuppItemTable supplementItem;

 

        next modified();

 

        purchLine = formDataSource.cursor();

 

        while select * from supplementItem
            where supplementItem.ItemRelation == purchLine.ItemId
        {
            PurchPurchaseOrderLineV2Entity    purchLineEntity;
            purchLineEntity.initValue();
            purchLineEntity.PurchaseOrderNumber = purchLine.PurchId;
            purchLineEntity.ItemNumber = supplementItem.SuppItemId;
            purchLineEntity.insert();
            purchLineEntity.clear();
        }

 

        //formDataSource.research();
        //formDataSource.refresh();

 

    }

 

}


// FormDataSourcestr 

[ExtensionOf(FormDataSourcestr(PurchTable, PurchLine))]

final class BAS_SuppItem_Extension

{

// validateWrite

 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;

    }


// validateDelete

  public boolean validateDelete()
    {
        boolean ret;
        ret = next validateDelete();
        FormDataSource formDataSource = this;
        PurchLine selectedPurchLine = formDataSource.cursor();
        purchLine purchLine;
        SuppItemTable supplementItem;
        //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 post trade agreement journals automatically using x++ code

How to Create a wizard in x++ d365

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