0

So the objective is to create a visualforce page form that creates a JSON for HTTP post method. I was wondering what was the best way to go about this.

'{"listingAction":"sale","propertyDetails":{"propertyType":["house"],"bedRooms":5,"bathRooms":5,"address":{"displayOption":"fullAddress","state":"vic","streetNumber":"690","street":"Nankoorstreet","suburb":"Bendigo","postcode":"3555"},"area":{"unit":"squareMetres","value":100}},"contacts":[{"domainAgentId":"234567","domainAgencyId":"11111","firstName":"Michael","lastName":"Cuming","phone":"0455287262","fax":"0282828282","mobile":"04557829292","email":"[email protected]","receiveEmails":true}],"providerAdId":"providerId123","DomainAgencyID":"36111","description":"Created in salesforce.","summary":"Listingsummaryhere","price":{"from":1000000,"to":1100000},"auction":{"dateTime":"2020-01-28T10:30Z","location":"Onsite"},"inspectionDetails":{"inspections":[{"from":"2020-01-14T10:30Z","to":"2020-01-14T11:00Z"},{"from":"2020-01-21T10:30Z","to":"2020-01-21T11:00Z"}]}}'

this is the json body it is expecting, Any help getting me closer to a solution would be amazing.. I was reading maybe JSONGenerator would be the way to go. but this is a first for me so any tips would be greatly appreciated.

0

1 Answer 1

1

JSONGenerator is almost certainly not the way to go, nor is Visualforce.

A much better option would be to use JSON.serialize() with either a Map<String, Object> or a series of classes to reproduce the structure of your JSON along the same lines as what JSON2Apex would give you (I have no affiliation to that tool).

A brief example of using a Map<String, Object> to generate JSON

Map<String, Object> forJSON = new Map<String, Object>{
    // property:value pairs are straightforward
    'listingAction' => 'sale',
    // nested JSON objects need a nested Map<String, Object>
    'propertyDetails' => new Map<String, Object>{
        // JSON arrays = Apex Lists
        'propertyType' => new List<String>{'House', 'Apartment'},
        'bedrooms' => 5,
        // so on and so forth...
        ...
    }
}

System.debug(JSON.serialize(forJSON));
2
  • Awesome thank you for the help, that's brilliant and how would you go about creating a form in salesforce for the input? or maybe from a custom object and take the fields from that? Commented May 3, 2022 at 8:01
  • @MikeCuming That's a different ask, and should be its own question (stackexchange sites like this one work on a "one question per post" model). Before you do though, you should try to research that and make an attempt. Lightning Web Components are the current UI framework (Aura components and Visualforce were the two predecessors). If you're new to Salesforce (or even just new to a part/feature) you should go through some modules/trails on trailhead.salesforce.com to get some base knowledge to start building upon.
    – Derek F
    Commented May 3, 2022 at 14:20

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