padding string in BPL

Hi folks, I'm trying to pad my patient MRN to 10 characters before I do a SQL lookup in my BPL. I've tried various approaches, and have not been successful.

When I add a "code" statement with the following code, I get an error as below.


set context.NewID = "0000000000"
set $EXTRACT(context.NewID,10-len(context.PatientID)) = context.PatientID


ERROR <Ens>ErrException: <OBJECT DISPATCH>zS8+5 ^CHSLI.BPLQueryDatabaseforPatientID.Thread1.1 *Property 'NewID'
in class 'CHSLI.BPLQueryDatabaseforPatientID.Context'
must be MultiDimensional -- logged as '-'
number - @'
set $EXTRACT(context.NewID,10-len(context.PatientID)) = context.PatientID'


Any ideas/help would be appreciated.


  • 0
  • 0
  • 458
  • 8
  • 4


I'm not exactly clear on which you intend to hold the new ID (context.newID maybe by the name?)

Regardless, you can also use the built-in pad method:

set context.NewID = ##class(Ens.Rule.FunctionSet).Pad(context.PatientID,-10,0)

where context.PatientID holds your original Patient ID. -10 means you want to prepend to be 10 characters, and "0" is the character you want prepended.

I like this approach. Didn't know I could call the Method directly from the BPL. Thanks.

Did you mean this ?

/// Takes string <var>value</var>, adds enough instances of <var>padchar</var> to lengthen the string to <var>padpos</var> characters.
/// If <var>padpos</var> is a negative value, the padding is prepended to the <var>value</var> string.
ClassMethod Pad(value As %String = "", padpos As %Integer = 10, padchar As %String = " ") As %String [ Final ]
Set newval=""
If padpos<0 for i=$L(value)+padpos:1:-1 Set newval=padchar_newval Set newval=newval_value }
Else for i=$L(value)+1:1:padpos Set newval=padchar_newval Set newval=value_newval }
quit newval


sources: Class Ens.Util.FunctionSet, Class Ens.Rule.FunctionSet

$EXCTRACT()  and also $LI(), $Piece()
is not allowed at the left side of SET when the target is an object property

use an intermediate variable instead.


set tmp="0000000000"
set $EXTRACT(tmp,10-len(context.PatientID)) = context.PatientID
set context.NewID=tmp

it might be easier that way

set context.NewID=$EXTRACT("0000000000"_context.PatientID,*-9,*)

Provided your patient MRN doesn't contain whitespaces, you can use $justify to pad string to required length and $translate to convert whitespaces into zeros.

w $tr($j("a", 10), " ", 0)

The second approach (set context.NewID=$EXTRACT("0000000000"_context.PatientID,*-9,*)) worked great. When I checked the documentation for the $JUSTIFY, it doesn't say you can specify your pad character.

Looked like only spaces to me: $JUSTIFY(expression,width[,decimal])

you missed the $TRANSLATE(.....," ",0) around

pls. set the hook to your preferred solution

COS shorthand is hard to read in browser;
I personally prefer full size COS + Courier as font.


Cache dies not like an object property as a left side argument,

but you can solve the problem much simpler:

set context.NewID=$tr($j(context.PatientID,10)," ",0)


By the way, if your solution would work, the result would be longer then 10 chars

for example, if context.PatientID=123 then you

would get: "000000123000"

instead of: "0000000123"