API Sample Code

From SmartWiki
Revision as of 14:12, 23 March 2016 by Alvin Thompson (talk | contribs)

Jump to: navigation, search

Sample Code from JSON API (SmartConnect)

The following are excerpts from SmartConnect, SmartSimple's RESTful JSON API. To review how the JSON API works in greater detail, and interact with some sample functions and test data, be sure to visit the Interactive Demonstration Page. This webpage is connected to a specific demonstration instance that contains sample data for API testing.

Interactive Demonstration Page


Get a Company Record

Request Code

----------HTTP POST Request----------


Parameters (case sensitive)
url: "/API/1/company/"
*companyid : 825646
*alias : api
*username : smartconnectdemo@smartsimple.com
*password : *****************


apitoken : CllAZXoBSn9PUHNVejJxfUA2D3dEcmNnAjI~


--Begin custom parameter (replace '{parameter}' with actual value)--
recordid : 825646 
--End custom parameter--


*Only required if calls are originated from remote server

----------Jquery Ajax Request----------
function api_call() {
    request = $.ajax(


{ url: "/API/1/company/",


{type: "POST",
 data: {
apitoken: "CllAZXoBSn9PUHNVejJxfUA2D3dEcmNnAjI~",
recordid : 825646 
    },
    dataType: "html"
  });
  request.done(function( data )
  { //TODO
  });
  }

Response Code

{
    message: {},
        records: [
            {
                sf_Name: "SmartSimple Software Inc",
                cf_Mission Statement: "SmartSimple was designed from the start to help organizations connect with their communities and streamline<br />
                critical processes. We're able to do this because our products are almost endlessly configurable. Once a system is implemented, <br />
                our clients are able to administer their software on their own, with minimal help from us.",
                sf_Company ID: "825646",
                sf_Address: "111 Peter Street",
                sf_Country: "Canada",
                cf_EIN Number: null,
                sf_Zip / Postal Code: "M5V 2H1",
                sf_City: "Toronto"
            }
        ]
}


Get a Contact Record

Request Code

----------HTTP POST Request----------
Parameters (case sensitive)
url: "/API/1/user/"
*companyid : 825646
*alias : api
*username : smartconnectdemo@smartsimple.com
*password : *****************


apitoken : fzJnYl1lakhFcVJnWwF5Y2xEelNDdXVOAgY~


--Begin custom parameter (replace '{parameter}' with actual value)--
recordid : 825646 
--End custom parameter--


*Only required if calls are orginated from remote server


----------Jquery Ajax Request----------
  function api_call() {
    request = $.ajax(


{ url: "/API/1/user/",


{type: "POST",
 data: {
apitoken: "fzJnYl1lakhFcVJnWwF5Y2xEelNDdXVOAgY~",
recordid : 825646 
    },
    dataType: "html"
  });
  request.done(function( data )
  { //TODO
  });
  }


Get a List of Organizations

Request Code

----------HTTP POST Request----------
Parameters (case sensitive)
url: "/API/1/company/"
*companyid : 825646
*alias : api
*username : smartconnectdemo@smartsimple.com
*password : *****************


apitoken : SnZnAgVGVwpSUERfDVUMVXpGYWViXQFTAgc~


--Begin custom parameter (replace '{parameter}' with actual value)--
criteria : {parameter} //Optional: json array  each condition contains nodes "andor", "field", "operator", "value" eg
[{"andor":"and","field":"FIELDNAME","operator":"like","value":"PATTERN"},{"andor":"or","field":"FIELDNAME","operator":"like","value":"PATTERN"}]
sortby : {parameter} //Optional: json array  each sort by field contains nodes "field", "direction" eg. [{"field":"FIELDNAME","direction":"asc|desc"}]
getstorevalue : {parameter} //Optional: int type: 0 = no (get display value), 1 = yes (get stored value)
--End custom parameter--


*Only required if calls are orginated from remote server




----------Jquery Ajax Request----------


  function api_call() {
    request = $.ajax(


{ url: "/API/1/company/",


{type: "POST",
 data: {
apitoken: "SnZnAgVGVwpSUERfDVUMVXpGYWViXQFTAgc~"
    },
    dataType: "html"
  });
  request.done(function( data )
  { //TODO
  });
  }


Response Code

{
    message: {},
        records: [
            {
                sf_Name: "SmartSimple Software Inc",
                cf_Mission Statement: "SmartSimple was designed from the start to help organizations connect with their communities and streamline<br />
                critical processes. We're able to do this because our products are almost endlessly configurable. Once a system is implemented, our<br />      
                clients are able to administer their software on their own, with minimal help from us.",
                sf_Company ID: "825646",
                sf_Address 2: "Suite 405",
                sf_Address: "111 Peter Street",
                sf_Country: "Canada",
                sf_Zip / Postal Code: "M5V 2H1",
                sf_State / Province: "Ontario",
                sf_City: "Toronto"
            },
            {
                sf_Name: "Grant Seeking Organizations",
                cf_Mission Statement: "",
                sf_Company ID: "825805",
                sf_Address 2: "",
                sf_Address: "4555 Westchester Street",
                sf_Country: "United States",
                sf_Zip / Postal Code: "",
                sf_State / Province: "NY",
                sf_City: "New York City"
            },
            {
                sf_Name: "Prospect Heights Educational Campus",
                cf_Mission Statement: "",
                sf_Company ID: "825806",
                sf_Address 2: "",
                sf_Address: "5108 4th Ave. At 51st St.",
                sf_Country: "--- Select One ---",
                sf_Zip / Postal Code: "",
                sf_State / Province: "New York",
                sf_City: "Brooklyn"
            },
            {
                sf_Name: "Acme Insurance",
                cf_Mission Statement: "",
                sf_Company ID: "826093",
                sf_Address 2: "",
                sf_Address: "111 Peter Street, Suite 405",
                sf_Country: "--- Select One ---",
                sf_Zip / Postal Code: "",
                sf_State / Province: "",
                sf_City: ""
            }
        ],
        header: [
            {
                id: "sf_Name",
                index: "1",
                name: "Name",
                caption: "Organization Name"
            },
            {
                id: "sf_Address",
                index: "2",
                name: "Address",
                caption: "Address"
            },
            {
                id: "sf_Address 2",
                index: "3",
                name: "Address 2",
                caption: "Address 2"
            },
            {
                id: "sf_City",
                index: "4",
                name: "City",
                caption: "City"
            },
            {
                id: "sf_State / Province",
                index: "5",
                name: "State / Province",
                caption: "State"
            },
            {
                id: "sf_Country",
                index: "6",
                name: "Country",
                caption: "Country"
            },
            {
                id: "sf_Zip / Postal Code",
                index: "7",
                name: "Zip / Postal Code",
                caption: "Zip / Postal Code"
            },
            {
                id: "sf_Company ID",
                index: "8",
                name: "Company ID",
                caption: ""
            },
            {
                id: "cf_1523156",
                index: "9",
                name: "Mission Statement",
                caption: "Mission Statement"
            }
        ]
    }
}


Get a List of Contacts

Request Code

----------HTTP POST Request----------
Parameters (case sensitive)
url: "/API/1/user/"
*companyid : 825646
*alias : api
*username : smartconnectdemo@smartsimple.com
*password : *****************


apitoken : TmZ3NmRFdUIBZ1tFFwYeAlB!e3FlV3FjAgMG


--Begin custom parameter (replace '{parameter}' with actual value)--
criteria : {parameter} //Optional: json array  each condition contains nodes "andor", "field", "operator", "value" eg
[{"andor":"and","field":"FIELDNAME","operator":"like","value":"PATTERN"},{"andor":"or","field":"FIELDNAME","operator":"like","value":"PATTERN"}]
sortby : {parameter} //Optional: json array  each sort by field contains nodes "field", "direction" eg. [{"field":"FIELDNAME","direction":"asc|desc"}]
getstorevalue : {parameter} //Optional: int type: 0 = no (get display value), 1 = yes (get stored value)
--End custom parameter--


*Only required if calls are orginated from remote server




----------Jquery Ajax Request----------
  function api_call() {
    request = $.ajax(


{ url: "/API/1/user/",


{type: "POST",
 data: {
apitoken: "TmZ3NmRFdUIBZ1tFFwYeAlB!e3FlV3FjAgMG"
    },
    dataType: "html"
  });
  request.done(function( data )
  { //TODO
  });
  }


Additional SmartConnect API Sample Code

We have built a resource that allows you to build and run some of the most common API functions. This resource is connected to a specific demonstration instance that contains sample data for API testing.

Interactive Demonstration Page


Deprecated Sample Code from Desktop API

The following are excerpts of SmartSimple's Deprecated Desktop API code written in Microsoft Visual Basic:


Dim ss_param As New SmartComClient.Parameters
 
Call ss_param.addField("opportunityid", "0")
Call ss_param.addField("companyid", "404091") 
Call ss_param.addField("branchid", "404091") ' 
Call ss_param.addField("opportunitytype", "11642") 
Call ss_param.addField("name", "test01err")
Call ss_param.addField("stageid", "9955") 
Call ss_param.addField("cf_1333618", "name1")
Call ss_param.addField("cf_1333619", "name2")
Call ss_param.addField("cf_1590372", "Txt_ACMAddress1.Text")
Call ss_param.addField("cf_1590373", "Txt_ACMAddress2.Text")
Call ss_param.addField("cf_1590374", "Txt_ACMProvince.Text")
Call ss_param.addField("cf_1590375", "Txt_ACMPostalCode.Text")
Call ss_param.addField("cf_1590376", "DDL_CITitle.SelectedValue")
Dim rtn As Long
rtn = tk.updateTable(sess, "levelone", 123123, ss_param)  '123123 is the applicationid
 If rtn = -1 Then
     MsgBox ("Record Not Updated: " & ss_api.getLastErr)
 Else

Option Explicit

Dim SmartSimple As New SmartComClient.Toolkit
Dim Session As SmartComClient.Session
Dim Session2 As SmartComClient.Session
Dim Userid As Long


Private Sub AccessCompanytable_Click()
    Dim CompanyFields As FieldInfos
    Dim Fields() As FieldInfo
    
    Set CompanyFields = SmartSimple.getFieldsInfo(Session, "companies", 0)
    Fields = CompanyFields.getFieldInfos
    MsgBox "There are " & UBound(Fields) + 1 & " associated with this table"

End Sub

Private Sub AccessContactsTable_Click()
    Dim ContactFields As FieldInfos
    Dim Fields() As FieldInfo
    
    Set ContactFields = SmartSimple.getFieldsInfo(Session, "contacts", 0)
    Fields = ContactFields.getFieldInfos
    MsgBox "There are " & UBound(Fields) + 1 & " associated with this table"

End Sub

Private Sub AccessUTAlevel1Data_Click()

    Dim SupportRecords As ADODB.recordset
 
    'check if user login already
 
    If Session Is Nothing Then
       Set Session = SmartSimple.PromptLogin()
    End If
    If Session Is Nothing Then
        MsgBox SmartSimple.getLastErr
        Exit Sub
 End If


    Set SupportRecords = SmartSimple.GetRecordSet(Session, "levelone", "100073", "name,description", "")
        
    MsgBox SupportRecords.RecordCount & " Support Tickets in Recordset"
    SupportRecords.MoveLast
    SupportRecords.MovePrevious
    MsgBox SupportRecords.Fields(0).Value & ": " & SupportRecords("name") & " is the last name in the list"
    SupportRecords.MoveFirst
    MsgBox SupportRecords.Fields(0).Value & ": " & SupportRecords("Name") & " is the first name in the list"
    
    
End Sub

Private Sub AddNewContactRecord_Click()
 Dim para As New SmartComClient.Parameters
 
 'check if user login already
 
 If Session Is Nothing Then
    Set Session = SmartSimple.PromptLogin()
 End If
 If Session Is Nothing Then
  MsgBox SmartSimple.getLastErr
   
  Exit Sub
  
 End If
 
 para.addField "userid", 0
'If value is 0, anew record will be added
  
 para.addField "firstname", "Walter"
 para.addField "lastname", "Zimmerman"
 para.addField "phone", "416.555.1212"
 MsgBox SmartSimple.updateTable(Session, "contacts", para)

End Sub

Private Sub AddNewlevel1_Click()
Dim readOnlyRs As ADODB.recordset
Dim writeableRs As ADODB.recordset
Dim rtn As Long
Set Session = SmartSimple.LoginUser("alias", "SmartSimple.xxx", "userID/e-mail address", "password")
Set readOnlyRs = SmartSimple.GetRecordSet(Session, "levelone", "100073", "name,description", "")
Set writeableRs = SmartSimple.CloneUpdateRecordset(readOnlyRs)

 writeableRs.AddNew
 writeableRs("name").Value = "Test level 1"
 writeableRs("description").Value = "Test Level 1 Description"
 writeableRs("opportunityid").Value = 0
 writeableRs.Update

 rtn = SmartSimple.updateTableByRecordSet(Session, "leveline", writeableRs, False)
'False will loop thru whole recordset, tru will process current record only.

End Sub

Private Sub CheckSettings_Click()
    Dim LastLogin As String
    
    LastLogin = "Alias:" & SmartSimple.getLastLoginToken(Alias) & _
    " URL:" & SmartSimple.getLastLoginToken(URL) & _
    " User Name:" & SmartSimple.getLastLoginToken(UserName)
    MsgBox (LastLogin)
End Sub



Private Sub CreateFieldsInTable_Click()
    Dim CompanyFields As FieldInfos
    Dim Fields() As FieldInfo
    Dim FieldName As String
    Dim I As Integer
    'MS-Word Objects
    Dim FieldTable As Table
    Dim TableColumn As Column
    Dim NoRows As Integer
    Dim Row As Integer
    
    'Extract fields
    Set CompanyFields = SmartSimple.getFieldsInfo(Session, "contacts", 0)
    Fields = CompanyFields.getFieldInfos
    
'Create MS-Word table based on number of fields
NoRows = UBound(Fields)
Set FieldTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=NoRows, NumColumns:=5)
    For I = 0 To UBound(Fields) - 1
        FieldTable.Cell(I, 1).Select
        Selection.TypeText I
        FieldTable.Cell(I, 2).Select
        Selection.TypeText Fields(I).Name
        FieldTable.Cell(I, 3).Select
        Selection.TypeText Fields(I).FieldType
        FieldTable.Cell(I, 4).Select
        Selection.TypeText Fields(I).ID
        FieldTable.Cell(I, 5).Select
        Selection.TypeText Fields(I).FieldOptions
        Next I
End Sub

Private Sub DisplayError_Click()
    If SmartSimple.getLastErr <> "" Then
        MsgBox SmartSimple.getLastErr
    Else
        MsgBox "No Error Found"
    End If
End Sub

Private Sub DisplayLevel2_Click()
  Dim SupportRecords As ADODB.recordset
  Dim SupportActivities As ADODB.recordset
  Dim TicketName As String
  Dim TicketID As Long

Set Session = SmartSimple.LoginUser("alias", "SmartSimple.xxx", "userID/e-mail address", "password")
    
 Set SupportRecords = SmartSimple.GetRecordSet(Session, "levelone", "100073", "name,description", "")
 TicketID = SupportRecords.Fields(0).Value
 
 Set SupportActivities = SmartSimple.GetRecordSet(Session, "leveltwo", "100073", "subject,description", "objectid = " & TicketID)
 MsgBox "Number of Actions on the First Ticket: " & SupportActivities.RecordCount

End Sub

Private Sub DisplayUserDetails_Click()
    Dim para As SmartComClient.Parameters

    If Session Is Nothing Then
        Set Session = SmartSimple.PromptLogin()
    End If
    
    If Session Is Nothing Then
        MsgBox SmartSimple.getLastErr
        Exit Sub
    End If
 
Set para = SmartSimple.getData(Session, "contacts", 0, "userid,firstname,lastname,phone,rolelist", " email='" & "sampleuser@address.com" & "'")
   
    If para Is Nothing Then
         MsgBox SmartSimple.getLastErr
         ' Error return  null object, normally invalid password or wrong url
       
       Else
         ' Storing primary key to a global variable used later for update
         
         Userid = Val(para.getValue("userid"))
         
         MsgBox "First Name: " & para.getValue("firstname") & vbCrLf _
         & "Last Name: " & para.getValue("lastname") & vbCrLf _
         & "Phone: " & para.getValue("phone")
    End If
   
End Sub

Private Sub DownLoadRecords_Click()
Dim Records As String

    'Records = SmartSimple.getDataFile(Session, "contacts", "firstname,lastname,title,phone", "isexternal=0")
      

End Sub


Private Sub GetCollection_Click()

'check if user login already
 
    If Session Is Nothing Then
       Set Session = SmartSimple.PromptLogin()
    End If
    If Session Is Nothing Then
        MsgBox SmartSimple.getLastErr
        Exit Sub
    End If

       
End Sub

Private Sub GetNotes_Click()
Dim Roles As SmartComClient.Roles

Dim assoicate As New SmartComClient.associate

    If Session Is Nothing Then
       Set Session = SmartSimple.PromptLogin()
    End If
    If Session Is Nothing Then
        MsgBox SmartSimple.getLastErr
        Exit Sub
End If


End Sub

Private Sub GetRecordSet_Click()
    
    Dim ContactRecords As ADODB.recordset
 
    'check if user login already
 
    If Session Is Nothing Then
       Set Session = SmartSimple.PromptLogin()
    End If
    If Session Is Nothing Then
        MsgBox SmartSimple.getLastErr
        Exit Sub
 End If

    Set ContactRecords = SmartSimple.GetRecordSet(Session, "contacts", 0, "firstname,lastname,email", "email like '" & "*sampleaddress.com" & "'")
        
    MsgBox ContactRecords.RecordCount & " Records in Recordset"
    ContactRecords.MoveLast
    MsgBox ContactRecords("lastname") & " is the last name in the list"
    ContactRecords.MoveFirst
    MsgBox ContactRecords("lastname") & " is the first name in the list"
End Sub

Private Sub Logout_Click()
    If Session Is Nothing Then
                MsgBox "You are not logged into SmartSimple"
            Else
                SmartSimple.LogoutOut (Session)
                MsgBox "Logged out"
    End If
End Sub

Private Sub NoPrompt_Click()
    Dim Alias As String, _
    ServerURL As String, _
    User As String, _
    Password As String
    
'All values set in code
    Alias = "alias"
    ServerURL = "alias.smartsimple.xxx"
    User = "userID/e-mail address"
    Password = "password"
    Set Session = SmartSimple.LoginUser(Alias, ServerURL, User, Password)
End Sub
Private Sub PasswordOnly_Click()
    Dim Password As String
        Password = InputBox("Enter Your Password")
        Set Session = SmartSimple.LoginUserWithLastSetting(Password)
End Sub
Private Sub Prompt_Click()
    Dim Parameters As SmartComClient.Parameters
        If Session Is Nothing Then
            Set Session = SmartSimple.PromptLogin()
        End If
        If Session Is Nothing Then
            MsgBox SmartSimple.getLastErr
        Exit Sub
    End If
End Sub

Private Sub SessionProperties_Click()
    If Session Is Nothing Then
        MsgBox "Session not Valid - Please Log in"
    Else
        MsgBox "Session Alias: " & Session.Alias & vbCrLf _
        & "Server URL: " & Session.ServerURL & vbCrLf _
        & "User Full Name: " & Session.ss_FullName & vbCrLf _
        & "User Name: " & Session.User & vbCrLf _
        & "UserID: " & Session.ss_UserID & vbCrLf _
        & "Password: " & Session.Password & vbCrLf _
        & "Organisation: " & Session.ss_CompanyName & vbCrLf _
        & "OrganisationID: " & Session.ss_Companyid & vbCrLf _
        & "Root Organisation: " & Session.ss_RootCompanyid, vbOKOnly, "Session Settings"
    End If

End Sub

Private Sub SSLEnable_Click()

    SmartSimple.setSSL (True)

End Sub

Private Sub TwoSessions_Click()
Set Session = SmartSimple.LoginUser("alias1", "SmartSimple.xxx", "e-mail address1", "password1")
Set Session2 = SmartSimple.LoginUser("alias2", "SmartSimple.xxx", "e-mail address2", "password2")


End Sub

Private Sub UpdateUserDetails_Click()
    Dim para As New SmartComClient.Parameters

    If Session Is Nothing Then
        Set Session = SmartSimple.PromptLogin()
    End If
    
    If Session Is Nothing Then
        MsgBox SmartSimple.getLastErr
        Exit Sub
    End If

 'Update Table
        para.addField "userid", Userid & ""
        para.addField "firstname", "New First Name"
        para.addField "lastname", "New Last Name"
        para.addField "phone", "555-1212"
        If SmartSimple.updateTable(Session, "contacts", para) = -1 Then
        MsgBox "Record Not Updated"
        Else
        MsgBox "Record Updated"
        End If
End Sub

See Also