0

I'm writing a bunch of Apex triggers that access an external web site. This site issues a token which is valid for a period of time, so I'd like to store it someone globally and reuse it, rather than having to authenticate on every call.

It seems I won't be able to use a static variable in the trigger, as this goes out of scope as soon as the trigger finishes.

I have also found info about reading from global variables, but nothing about creating or writing to them. Is this possible? If not, where can I securely store global (app-level) state?

6
  • 3
    you could use Custom Setting to store the token and its expiration period.So next time you need it,you can retrieve the value from the Custom setting and validate its expiration.if it has expired,you can call the site and update the token and its expiration period and use it.Tutorial on Custom Setting can be found here
    – Praveen
    Jun 10, 2016 at 9:41
  • Praveen, if I were you I'd post your comment as an answer. Maybe describe how to create and access a custom setting and link to the documentation and you've got a perfect answer. Jun 10, 2016 at 9:43
  • If above comment helps,i'll post it as the answer so that we can close the question.
    – Praveen
    Jun 10, 2016 at 9:45
  • @lex Point noted.Will post it as the answer with proper descriptions.
    – Praveen
    Jun 10, 2016 at 9:47
  • @Praveen yes - helpful. can you post as an answer for future reference?
    – steve cook
    Jun 10, 2016 at 10:17

1 Answer 1

1

You could use Custom Setting to store the token and its expiration period. So next time you need it,you can retrieve the value from the Custom setting and validate its expiration. if it has expired,you can call the site and update the token and its expiration period and use it.

Setup Custom Setting: (click here to know more)

1)Go to setup -> Develop -> Custom Settings

2)Click New ->

Object Name = Site_Setting (for example)

label = Site Setting

Setting Type = List

Visibility = Public(if you are using for managed package,then use Protected)

3)After creating the Custom setting,Click Manage to create fields to store token and its expiration period

Access the custom setting in Apex:(click here to know more)

Create a custom setting data to store the necessary values:

Site_Setting__c setting = new Site_Setting__c(Name = 'Site Token',Token__c='xxxx',Expiration_Period__c='xxxx');
insert setting;

To access it next time:

Site_Setting__c cs = Site_Setting__c.getInstance('Site Token');
// you then have access to cs.Token__c and cs.Expiration_Period__c

For more info on usage, you can refer this awesome Tutorial on custom setting by jeff.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .