2

I'd just like to get an array of the opportunity data within Salesforce Visualforce so that I can manipulate it in Javascript.

Below is the query that I'd like to use to pull in the array:

select name,amount,closedate from Opportunity where stagename = 'Closed Won'

It seems like this should be pretty straightforward if I'm pulling this information in within Visualforce, so I wouldn't need to set up authorization.

  • 1
    Did you try JS remoting? You can easily get JSON array of opportunity records. Any specific reason to go for REST API? – Sam Jan 22 '15 at 2:20
  • You don't even need remoting if you just need the data when the page loads. – metadaddy Jan 22 '15 at 17:33
  • Hey guys, great suggestions. I'm unfamiliar with JS Remoting or other options. What takes the least amount of code or work to implement? – Chris Jan 23 '15 at 10:30
1

Adapting Marty's answer, since you want this in JSON:

A controller method can return a string with a JSON representation of the data - you can then manipulate it in JavaScript however you want.

OpportunitiesIndex.page

<apex:page controller="OpportunitiesController">
    <h3>
        Closed Opportunities from JavaScript
    </h3>
    <ul id="list">
    </ul>
    <script>
        // The controller returns JSON, which is rendered straight 
        // into the page. No need to make it a string then parse it!
        var opportunities = {!opportunities};
        // Just as an example, add each opportunity name to the list
        var ul = document.getElementById("list");
        opportunities.forEach(function(opportunity) {
            // Careful - JavaScript is case sensitive!
            var li = document.createElement("li");
            li.appendChild(document.createTextNode(opportunity.Name));
            ul.appendChild(li);
        });
    </script>
</apex:page>

OpportunitiesController.class

public class OpportunitiesController {
    public String getOpportunities() {
        return JSON.serialize([
            select name,amount,closedate
            from Opportunity
            where stagename = 'Closed Won'
        ]);
    }
}

References

  • Apex Properties shows you how to define properties that can be pulled into a Visualforce page.
  • Building a Custom Controller shows another example of a custom controller used to provide data for a Visualforce page.
2

Chris, getting the data into a Visualforce page is pretty straightforward. All you need is an Apex controller. The sample Visualforce page and Apex controller below illustrates how you can pull the array you defined into a page to be rendered.

OpportunitiesIndex.page

<apex:page controller="OpportunitiesController">
    <!-- The value attribute used in the apex:repeat below
         is a property, which in this case is defined via
         the controller's instance method called getOpportunities() -->
    <apex:repeat value="{!opportunities}" var="opportunity">
        <div>
            <div>
                {!opportunity.Name}
            </div>
            <div>
                ({!opportunity.Amount})
            </div>
        </div>
    </apex:repeat>
</apex:page>

OpportunitiesController.class

public class OpportunitiesController {
    public List<Opportunity> getOpportunities() {
        return [
            select name,amount,closedate
            from Opportunity
            where stagename = 'Closed Won'
        ];
    }
}

References

  • Apex Properties shows you how to define properties that can be pulled into a Visualforce page.
  • Building a Custom Controller shows another example of a custom controller used to provide data for a Visualforce page.
0

Chris when working with Salesforce RestAPI I like to use Postman to test and see the data as it is returned. To use Postman you have to setup Oauth, but the ability to see exactly what is returned is beneficial.

Here is a decent blog post on Salesforce Rest and Postman. https://forceadventure.wordpress.com/2013/01/31/creating-a-custom-rest-api-in-salesforce/

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.