4

Is there a way to write to a Custom Label via Apex? The online documentation doesn't outright say that you can't, but I'm guessing it's called a 'custom label' rather than a 'custom variable' for a reason.

My naive efforts to do

Label.myLabel = 'foo';
System.Label.myLabel = 'bar';

result in the following error:

"Error: Compile Error: Expression cannot be assigned at line 12 column"

3
  • also found another link developer.salesforce.com/forums/…
    – sfdc
    Jun 9 '14 at 18:55
  • I'm curious to hear of the use case for this. Is there any way you can just use a variable for this rather than a label? Jun 9 '14 at 18:56
  • I ended up using a different method, but here's my situation. I have a scheduled job that schedules 5 other jobs. Each of the five needs a parameter (or I have 5 nearly identical pieces of code to maintain). Passing info along through a custom label seemed worth a try.
    – Jagular
    Jun 9 '14 at 19:08
4

Custom labels can only be created/modified via the setup web interface or via the metadata API. There's no direct way to invoke metadata API operations from apex other than some 3rd party libraries to make webservice calls.

That probably doesn't do what you want however, and it sounds like custom labels aren't an optimal fit for your use case. Have you looked at using list custom settings?

2
2

I think you can't assign the expression to custom label.I found an idea related to this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.