So here is an object what I am trying to accomplish.

If there is any updates on the account, do an API call. This call will grab account information and pushed out to external database(my own database) by request API with information and from the result(API call) update couple fields on the salesforce.

  1. My solution is using a trigger if there is any change then call API using username and password.
  2. API will return token have life span.
  3. Using the token other API utility function can be called.
  4. Pass the account information with json to API and updates database.
  5. Returns custom values back as json and updates some fields in salesforce.

my questions are 1. When I use apex class How can salesforce application have username password sync with my API and salesforce user to authenticate to return valid token. 2. Once I get the token where can I save to use it again.

my code to request is // this is sudo like code it will not work as it is i am trying to

global class IpdReqDev {
    String url;
    String data;
    String token{get;set;}
    Public String mytext {get;set;}
    public IpdReqDev() {
            url = 'some landing api url/index.php?action=api';

    public void authenticate(){
           param = '{"func":"auth","username":"myusername","pass":"mypassword"};
           somejson_contains_token = establish_connection();
           //parse json
           return token;

    public String establish_connection(){
            Http h = new Http();
            // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
            HttpRequest req = new HttpRequest();
            //String url;
            url +="&"+data
            // Send the request, and return a response
            HttpResponse res = h.send(req);
            return res.getBody();

Is this even possible for what I am trying to accomplish?


This is really a very open question. There are various considerations here and accordingly different solutions.

If you use same username and password for each user then you can have your API username/ password embedded in code (to avoid man-handling) or even put these values in Custom labels. If on the other hand, username/ password is specific to each customer then you should look into using Custom setting (preferably Hierarchy) or even User object (via custom fields) to store user's specific details.

Token generated by external API can be persisted within custom setting or User object, if you want to persist it for some duration to reuse it for subsequent transactions.

Alternatively, you can also consider using Outbound messaging for such scenarios. For more details refer:- https://help.salesforce.com/HTViewHelpDoc?id=workflow_managing_outbound_messages.htm&language=en_US

|improve this answer|||||
  • You are right there are multiple ways to implement this. I would have choose to have token to be hidden value and use that value to interact with external API. this way each user can have their own token and can determine which user is request operation. – Tom Oct 30 '15 at 22:13
  • You can also consider using Salesforce org id for this purpose. As org id is unique it can help you identify a client org correctly. – Anshul Oct 31 '15 at 22:12
  • what do you mean by Salesforce org id?? I need to track each individual who is making the API call to my API service – Tom Nov 4 '15 at 20:12
  • oh.. I guess I misunderstood. I thought the idea is to track each individual client organization invoking web service. You can ignore that suggestion. – Anshul Nov 4 '15 at 20:42

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.