2

I have a button calling a javascript function on an opportunity object that passes its associated account's id to an apex function. What is the best way in apex to retrieve that Account object? I want to send it to an external web service. Currently JS:

{!requireScript("/soap/ajax/20.0/connection.js")} 
{!requireScript("/soap/ajax/20.0/apex.js")} 
var idd = '{!Opportunity.AccountId}';
retStr = sforce.apex.execute("ParseManager", "sendAccountToParse", {a:id});
alert(retStr);

apex function in ParseManager Class:

webservice static String sendAccountToParseS(String a){

        // retrieve account object from id
        // retrieve relevant properties from account object
        // send to external web service (I know how to do this)
        // HTTPResponse res = http.send(req);

        String confirmationString = "";

        return confirmationString;
    }
2

First, change what you're passing as your variables are different:

retStr = sforce.apex.execute("ParseManager", "sendAccountToParse", {a:idd}); // idd instead of id

Then since you've got the Account Id passed into your Apex method, you can simply write a SOQL query to retrieve all related data for the account:

webservice static String sendAccountToParseS(String a)
{
    List <Account> accounts = [SELECT Id, Name, Owner.Name, Phone, Type, ANYTHING_ELSE FROM Account WHERE Id = :a];

    if (accounts.isEmpty() == false)
    {
        // we've got a matching account
        Account account = accounts[0];

        // The account variable now holds all info that was in the query above
        // you can serialize it before sending as well
        // String jsonBody = JSON.serialize(account);
        // send the request
    }
    else
    {
        // handle error when there is no matching account for X reason
    }

    String confirmationString = "";

    return confirmationString;
}
  • 2
    Thank you!!! I made a typo on the first part copying my code over, both parts were id. I was unsure if I needed to do a SOQL query or if there was a better approach. This is exactly what I needed, thanks! – mkoscak Jul 8 '15 at 23:38
2

You can skip the Apex Code altogether if you want, and send it through JavaScript directly.

Start with the AJAX Toolkit:

{!RequireScript("/soap/ajax/34.0/connection.js")}
sforce.connection.sessionId = "{!$Api.Session_ID}";

Next, get the account:

var account = sforce.connection.retrieve("Field1,Field2...", "Account", ["{!Opportunity.AccountId}"]);

Finally, send it through the built-in JavaScript proxy:

sforce.connection.remoteFunction(
{ url: "some-endpoint", requestHeaders: {...}, method: "GET", 
 onSuccess: function(response) { ... }
 onFailure: fucntion(response) { ... }
);

If you'd rather not do that, you can still send it through Apex Code. Which leads you back to your original question of querying an account.

Account record = [SELECT Name, Industry, AccountNumber... FROM Account WHERE Id = :a];

Notice that both cases suggest you already know which fields you want. If you don't, you'll need to do a describe beforehand. See SObjectType methods or sforce.connection.describeSObject.

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.