Usina BRAdvPL Guide
Sign in
← All topics
PRACTICAL GUIDEUnder review

MVC master-detail with AddFields and AddGrid

MPFormModel():New(); AddFields(); AddGrid(); SetRelation()

Defines master/detail with explicit IDs and relation.

MVC master-detail with AddFields and AddGridAdvPL
01 · OVERVIEW

Overview

AddFields creates the first Field submodel as master; AddGrid creates detail owned by that ID. SetRelation defines child/master linkage and ordering. The community example uses SBM/SB1; this adaptation uses fictitious ZZ1/ZZ2 tables to avoid suggesting standard-register changes. The child branch comes from xFilial("ZZ2") because sharing can differ from the master. Create and verify the schema/index in staging before evaluating the block. (Sources: refs. 1–5.)

02 · SYNTAX

Syntax

MPFormModel():New(); AddFields(); AddGrid(); SetRelation()
03 · PRACTICAL EXAMPLE

Commented example

#Include "Protheus.ch"
#Include "FWMVCDef.ch"

User Function Ed554()
    // Requires test dictionary ZZ1/ZZ2.
    Local bBuild := {|| BuildModel()}
    ConOut(ValType(bBuild))
Return Nil

Static Function BuildModel()
    Local oModel := MPFormModel():New("ED554M")
    Local oStruct := FWFormStruct(1,"ZZ1")
    oModel:AddFields("ZZ1MASTER",Nil,oStruct)
    oModel:AddGrid("ZZ2DETAIL","ZZ1MASTER",FWFormStruct(1,"ZZ2"))
    oModel:SetRelation("ZZ2DETAIL",{{"ZZ2_FILIAL",'xFilial("ZZ2")'},;
        {"ZZ2_COD","ZZ1_COD"}},"ZZ2_FILIAL+ZZ2_COD+ZZ2_ITEM")
    oModel:SetPrimaryKey({"ZZ1_FILIAL","ZZ1_COD"})
Return oModel
Expected result

Prints B; the helper is not evaluated and creates/writes no tables. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

BEST PRACTICES
  • Verify field types/widths, primary key, relation and actual index before activation. Test validation, cancellation and persistence separately; the View must bind the correct submodel IDs.
  • Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Related content

REFERENCES
  1. TOTVS. MPFormModel. TDN. Accessed on 2026-10-27.
  2. TOTVS. FWFormModel. TDN. Accessed on 2026-10-27.
  3. TOTVS. FWFormView. TDN. Accessed on 2026-10-27.
  4. TOTVS. FWFormStruct. TDN. Accessed on 2026-10-27.
  5. Atilio. zMVCMd3.prw; commit 7d275a7. Accessed on 2026-10-27.
Status
Under review
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR
0 approved comment(s)

Comments

There are no approved comments yet.

Sign in with Google or Microsoft to comment.

Powered by Usina Docs · Alpha