Skip to content

Instantly share code, notes, and snippets.

@Woody88
Last active March 25, 2018 04:20
Show Gist options
  • Save Woody88/7a0766fac8efbe789dc11660b3c3bb3d to your computer and use it in GitHub Desktop.
Save Woody88/7a0766fac8efbe789dc11660b3c3bb3d to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeApplications #-}
import Data.Text (Text)
import Data.Aeson
import Data.Proxy
import GHC.Generics
import Servant.Client
import Haskforce
import Haskforce.API.Resources
-- Defining the structure of Account and the fields that we would like to
-- retrieve/update from salesforce.
data Account = Account
{ name :: Text
} deriving (Generic, Show)
-- Salesforce's SObject Rows endpoint needs to know what object
-- we are referring too and this is how we define the name of the Object
instance SFObject Account where
sobjectName _ = "Account"
-- This we handle parsing our data type from/into json
instance ToJSON Account
instance FromJSON Account where
parseJSON = genericParseJSON defaultOptions {
fieldLabelModifier = adjustFromJsonField
}
-- Most of salesforce objects field name starts with a capital letter
-- and data record does not allow our field/function to begin with a capital letter
-- fortunately aeson allows us to define how we would like to replace some field names
-- if we need to. This is what this function is for.
adjustFromJsonField :: String -> String -- << this seems tedious
adjustFromJsonField "name" = "Name"
adjustFromJsonField x = x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment