Usina BRAdvPL Guide
Sign in
← All topics
PRACTICAL GUIDEUnder review

RpcSetEnv and environment preparation

Defines who opens, uses and closes an automatic environment.

RpcSetEnv and environment preparationAdvPL
01 · OVERVIEW

Overview

First identify the caller: menu, Schedule and REST PrepareIn can already provide a prepared environment. Preserve that context and its policies. For an isolated job that needs one, obtain credentials through a protected channel, call RpcSetEnv, check its return and process only on success. Track ownership and close only your environment with RpcClearEnv, including error cleanup. Select("SX2") alone proves neither identity, branch nor permission. This example only demonstrates a fictional ownership flag without opening/closing connections. (Sources: refs. 1–2.)

03 · PRACTICAL EXAMPLE

Commented example

#Include "Protheus.ch"

User Function Ed638()
    // Teaching flag; real ownership comes from actual opening.
    Local lOwnedEnvironment := .F.
    If lOwnedEnvironment
        // Actual cleanup must cover failure paths too.
        ConOut("OWNED_CLEANUP")
    EndIf
Return Nil
Expected result

Skips cleanup because it opened no environment. This is a decision model, not a ready job wrapper. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

BEST PRACTICES
  • Do not switch companies or reauthenticate framework-prepared REST/SOAP. Check release and LIB.
  • Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Related content

REFERENCES
  1. TOTVS. RpcSetEnv: preparo, restrições de contexto e versão. Accessed on 2026-10-28.
  2. Atilio. dan-atilio/AdvPL. Maratona de Exemplos/Exemplo_426_Funcao_RPCSetEnv.prw. Arquivo completo consultado; adaptação editorial. Accessed on 2026-10-28.
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