2
votes

i,m trying to write a trigger in SalesForce Opportunity which have to send the updated StageName to external webservice using predefined username and password.

here is sample code:

trigger isStageChanged on Opportunity (after update) {
    for (Opportunity o : Trigger.new) {
        Opportunity beforeUpdate = System.Trigger.oldMap.get(o.Id); 

        if(beforeUpdate.StageName != o.StageName) {
            // Call WS
        }
    }

}

"Call ws" means to use link like: http://mydomain.com/webservice/index.php?username=MY_USERNAME&password=MY_PASS&stage=opportunityNewStage

The question is: Where can i store MY_USERNAME and MY_PASS in case i want to use this trigger on different SalesForce customers and to let them configure them after install? When i red the APEX Code Developers Guide i red that i can use a Web Services API to deploy my trigger and that way i can store user and pass on my server. Is it possible? On my server i'm using php.

I hope i am clear in my question.

1

1 Answers

3
votes

You should probably store the username and password using custom settings. This will allow their administrator to easily configure the username and password with values they provide.

Note that you can't make a callout from inside a trigger as it holds up database operations, instead you need to put your callout in a public/global method of another class, using the @future annotation:

@future public void DoMyCallout(list<id> liOpportunities)
{
    // load your oppties if need be, then do the callout
}

@future indicates that this method will run asynchronously, so you can call it from the trigger but it will run in another thread and so not hold up the database operation in progress.