Bobby Palko
04/17/2024, 3:44 PMstatic for a class variable. I have instance methods that attempt to reference this static variable, but in doing so I'm getting an error:
Variable CANCEL_ERROR_TEXT is undefined in static context.
I've tried defining a static block and just declaring it as static.CANCEL_ERROR_TEXT, referencing it as static.CANCEL_ERROR_TEXT and Customer::CANCEL_ERROR_TEXT, and just about everything in the Adobe docs. The only thing that does the trick is to place the variable in the init method, which doesn't seem right to me. Am I missing something blatantly obvious here?websolete
04/17/2024, 3:48 PMwebsolete
04/17/2024, 3:48 PMRodney
04/17/2024, 3:49 PMcomponent {
static {
public static.CANCEL_ERROR_TEXT = "Oops!";
}
}
You also need to import the component into the page you want to use it on.Rodney
04/17/2024, 3:51 PMBobby Palko
04/17/2024, 3:58 PMRodney
04/17/2024, 4:06 PMimport Models.static.MESSAGES;
component {
function foo() {
writeOutput(Customer::CANCEL_ERROR_TEXT);
}
}Bobby Palko
04/17/2024, 4:06 PMcomponent name="Customer" initmethod="new" output=false extends="Container.API" {
// component properties
static {
static.PARENT_CHILD_CANCEL_ERROR = "Customer with an active Parent/Child relationship may not be canceled.";
}
public Customer function new() {
this = super.new(argumentCollection=arguments);
this.Since = Now();
this.balance_status_dt = Now();
this.external_uuid = CreateUUID();
return this;
}
// other methods...
public struct function cancel(boolean tag=false, boolean markFraud=false, boolean adminCancel=false, string maxMindTag="", refund=false) {
if (this.hasActiveParentChildRelationship()) {
// this doesn't throw as expected,
throw(type="VALIDATION", message=Customer::PARENT_CHILD_CANCEL_ERROR);
}
var res = this.cancel_stuff(argumentCollection=arguments);
// more stuff
return res;
}
}
does not seem to workRodney
04/17/2024, 4:10 PMBobby Palko
04/17/2024, 4:13 PMCustomer::PARENT_CHILD_CANCEL_ERROR to static.PARENT_CHILD_CANCEL_ERROR makes no differenceRodney
04/17/2024, 4:19 PMBobby Palko
04/17/2024, 4:20 PMRodney
04/17/2024, 4:21 PMRodney
04/17/2024, 4:23 PMimport static.MESSAGES as the first line of your Customer component, before the component declaration. Then reference the message using MESSAGES::PARENT_CHILD_CANCEL_ERROR.Bobby Palko
04/17/2024, 4:24 PMaliaspooryorik
websolete
04/17/2024, 4:30 PMRodney
04/17/2024, 4:33 PMRodney
04/17/2024, 4:35 PMRodney
04/17/2024, 4:36 PMwebsolete
04/17/2024, 4:50 PM