Usina BRAdvPL Guide
Sign in
← All topics
PRACTICAL GUIDEUnder review

Environment, company, branch and module handling

Defines ownership of Protheus environment setup and closure.

Environment, company, branch and module handlingAdvPL
01 · OVERVIEW

Overview

RpcSetEnv can choose company, branch, user, credential, module and tables for automatic routines that must own their context. Check its logical return. Menus, prepared Schedule and PrepareIn services may already own an environment: do not reinitialize or close it with RpcClearEnv. Documentation records an exception for prepared REST/SOAP contexts from 12.1.2510. From LIB 20251117 the credential argument accepts the specific token generated by totvs.framework.users.rpc.getAuthToken(); arbitrary tokens are not implied. The block only prepares opening, not a complete lifecycle. (Sources: refs. 1–1.)

03 · PRACTICAL EXAMPLE

Commented example

#Include "Protheus.ch"

User Function Ed695()
    // Credential comes from an authorized caller; never log it.
    // Uninvoked block: does not switch the current environment.
    Local bOpen := {|cCompany,cTargetBranch,cUser,cCredential,cModule| ;
        RpcSetEnv(cCompany,cTargetBranch,cUser,cCredential,cModule)}
    ConOut(ValType(bOpen))
Return Nil
Expected result

Creates block B without authenticating, opening or closing an environment. Real integration must check .T. and clean up only owned context. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

BEST PRACTICES
  • Do not confuse work-area restoration with company switching. Keep credentials out of source/logs; verify rights, module and context ownership.
  • Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Related content

REFERENCES
  1. TOTVS. RpcSetEnv. Parâmetros, credenciais, restrições e encerramento. 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