Skip to content

Instantly share code, notes, and snippets.

@Woody88
Last active March 24, 2018 04:48
Show Gist options
  • Save Woody88/f42e6cad80cf9142f28c472fdd97babe to your computer and use it in GitHub Desktop.
Save Woody88/f42e6cad80cf9142f28c472fdd97babe to your computer and use it in GitHub Desktop.
Example of using my Haskforce library to query and Account object on salesforce
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Example where
import Data.Text (Text)
import Data.Aeson
import Data.Proxy
import GHC.Generics
import Servant.Client
import Haskforce
import Haskforce.API.Resources
data Account = Account
{ name :: Text
} deriving (Generic, Show)
instance SFObject Account where
sobjectName _ = "Account"
instance ToJSON Account
instance FromJSON Account where
parseJSON = genericParseJSON defaultOptions {
fieldLabelModifier = adjustFromJsonField'
}
adjustFromJsonField' :: String -> String -- << this seems tedious
adjustFromJsonField' "name" = "Name"
adjustFromJsonField' x = x
--- cred and tokenRequest would usually be in a config file and would be handled elsewhere.
cred :: UserCred
cred =
UserCred (Just . Username $ "woodmai.cm")
(Just . Userpassword $ "21231!")
(Just . SecretKey $ "WbLqWxHdbet5")
"3MVG9CEn_O.qhb_CMW4shC2zmPCd6wsm30EsxL4XCp5bRtEPTGvFjv_3AjWufKnyiS.o"
"333210859132"
tokenRequest :: TokenRequest
tokenRequest =
TokenRequest (GrantType Password)
(UserPassword)
cred
Nothing
Nothing
Nothing
Nothing
sfclient = login tokenRequest salesforceUrl "v42.0"
getAcc :: IO (Either ServantError (SObject Account))
getAcc = do
(Right client) <- sfclient
getSObjectRow (SObjectId "0016A00000JcdjK") (Proxy @Account) client
acc :: IO (SObject Account, SFClient)
acc = do
(Right client) <- sfclient
(Right h) <- getSObjectRow (SObjectId "0016A00000JcdjK") (Proxy @Account) client
return (h, client)
updateAcc :: IO (Either ServantError NoContent)
updateAcc = do
(SObject (objId, objData), client) <- acc
updateSObjectRow objId (updateName $ sobject objData) client
updateName :: Account -> Account
updateName acc = acc { name = "NewAccount4" }
deleteAccount :: IO (Either ServantError NoContent)
deleteAccount = do
(Right client) <- sfclient
deleteSObjectRow (SObjectId "0016A00000JcdjK") (Proxy @Account) client
----- Execution of Example -----
λ> (Right account) <- getAcc
*Example
λ> account
SObject (SObjectId "0016A00000JcdjKQAR",SObjectData {attributes = SObjectAttr {sobjectType = SObjectType "Account", sobjetUrl = "/services/data/v42.0/sobjects/Account/0016A00000JcdjKQAR"}, sobject = Account
{name = "NewAccount4"}})
λ> getSData account
SObjectData {attributes = SObjectAttr {sobjectType = SObjectType "Account", sobjetUrl = "/services/data/v42.0/sobjects/Account/0016A00000JcdjKQAR"}, sobject = Account {name = "NewAccount4"}}
*Example
λ> sobject . getSData $ account
Account {name = "NewAccount4"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment