如何将绑定变量传递给消息目录中的消息解释文本?

How do I pass bind variables to Message Explain text in message catalog?

我知道当我使用消息目录中的消息时,我可以在消息文本中使用绑定变量。例如,

消息正文:

Employee (%1) has already worked %2% this month.

描述:

Employees cannot work over %3% for any given month. Decrease percent for row %5 to %4%.

我注意到当我使用 MessageBox 时,我可以将一个值传递给第三个、第四个和第一个绑定,但是当我使用任何其他函数时(MsgGetMsgGetExplainText,或 CreateException),解释文本中的绑定不会被替换。我这样做是因为我有时需要停止处理 Error,从消息目录(即 Error(MsgGet())传递文本。


例如:

使用MessageBox()

使用Error(MsgGet())

这些分别由以下内容产生:

MessageBox(0, "", 20007, 6, "MESSAGE NOT FOUND: Too many hours. Decrease percentage.", &emplid, Round(&percentInMonth * 100, 0), Round(&workPercentCap * 100, 0), &neededPercent, &rowNumber);

Error (MsgGet(20007, 6, "MESSAGE NOT FOUND: Too many hours. Decrease percentage.", &emplid, Round(&percentInMonth * 100, 0), Round(&workPercentCap * 100, 0), &neededPercent, &rowNumber));

如何解决这个问题?我是 运行 PeopleTools 8.54.

MsgGetExplainText 的 8.54 PeopleBooks 中提到了以下内容:

Note: This substitution only takes place in message explain text when the MsgGetExplainText function is used. If you use a message box, the parameter substitution will not occur in the explain text.

这是一种不清楚的说法,表示您不能那样做。这也适用于 MsgGetCreateException,即使在他们的文档中没有指定。


但是,IT Toolbox 找到了解决方法。

该线程中的用户发布了接近以下代码的内容作为解决方法(在此处进行了改编以适合您的问题):

Local string &errorMessage = MsgGet(20007, 6, "MESSAGE NOT FOUND: Too many hours. Decrease percentage.", &emplid, Round(&percentInMonth * 100, 0), Round(&workPercentCap * 100, 0), &neededPercent, 1);
&errorMessage = &errorMessage | Char(10) | Char(10);
&errorMessage = &errorMessage | MsgGetExplainText(20007, 6, "MESSAGE NOT FOUND: Too many hours. Decrease percentage.", &emplid, Round(&percentInMonth * 100, 0), Round(&workPercentCap * 100, 0), &neededPercent, 1);
Local string &temp = MsgGet(0, 0, "");
Error &errorMessage;

无论出于何种原因,变量的绑定都是在事后发生的,也就是说,如果您在 MsgGet 的 return 值上调用 Error,结果是一条错误消息使用 unbound 参数。

您需要将 MsgGet MsgGetExplainText 的结果相互附加,以模仿上述 MessageBox 的功能。

注意看似无用的语句Local string &temp = MsgGet(0, 0, "");是必须的,否则会重复消息的最后一行,但没有参数替换:

这会清除 return 来自 MsgGetMsgGetExplainText 的未绑定解释文本。


如果您想改为抛出 Exception,则适用相同的原则。您需要使用解决方法。如果您不想向最终用户显示,您还可以从消息中删除堆栈跟踪:

Local string &errorMessage = MsgGet(20007, 6, "MESSAGE NOT FOUND: Too many hours. Decrease percentage.", &emplid, Round(&percentInMonth * 100, 0), Round(&workPercentCap * 100, 0), &neededPercent, 1);
&errorMessage = &errorMessage | Char(10) | Char(10);
&errorMessage = &errorMessage | MsgGetExplainText(20007, 6, "MESSAGE NOT FOUND: Too many hours. Decrease percentage.", &emplid, Round(&percentInMonth * 100, 0), Round(&workPercentCap * 100, 0), &neededPercent, 1);
Local string &temp = MsgGet(0, 0, "");

rem "Remove" the Stack-Trace;
try
   throw CreateException(0, 0, &errorMessage);
catch Exception &e
   Error &e.ToString( False); rem False means no stack trace;
end-try;

此外,可以 extend Exception class 并以这种方式替换绑定值。然而,它有点复杂。这是一个例子:

class CustomException extends Exception
   method CustomException(&messageSet As integer, &messageNum As integer, &message As string, &substitutions As array of string);
end-class;

method CustomException
   /+ &messageSet as Integer, +/
   /+ &messageNum as Integer, +/
   /+ &message as String, +/
   /+ &substitutions as Array of String +/
   %Super = CreateException(&messageSet, &messageNum, &message);
   Local integer &i;
   For &i = 1 To &substitutions.Len
      %Super.SetSubstitution(&i, &substitutions.Get(&i));
   End-For;
   rem Setting Context to something else replaces the stack trace with whatever text set it to;
   %Super.Context = "";
   %Super.DefaultText = &message;
end-method;

要实现此功能,您可以传入要替换为消息的绑定变量数组,然后遍历该数组并为每个变量调用 SetSubstitution()

此外,如上所述,如果您希望 "throw" 的异常类似于错误消息(即显示错误消息 而没有 堆栈跟踪),你可以删除 ContextCustomException:

%Super.Context = "";

这对我有用 -

&string = MsgGetExplainText(25070, 144, "Error Message", &BINDVARIABLE); &string1 = MsgGetExplainText(99999, 1, """"); Error (&string);