Last week I posted a VB.NET code snippet showing how to retrieve data using the REST v2 API and put it into a datagrid. Today here I show how to update information back into CSI. This sample is adapted from the Infor CSI sample in "Infor Mongoose Integrating IDOs with External Applications" page 140 example 2
''' <summary>
''' This example shows how to update 2 fields back to CSI. The user clicks a row and then click the Update Test - prompts to update desc and UM for selected row button. The user will see
''' 2 input boxes where they can update the 2 fields. After answering the 2nd input box, the update will occur. Any errors will be displayed in a JSON string
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub btnUpdateTwoFieldsInRecord_Click(sender As Object, e As EventArgs) Handles btnUpdateTwoFieldsInRecord.Click
'// Get the selected row index and retrieve the values from the cells in the row
Dim row As Int32 = DataGridView1.CurrentCell.RowIndex
Dim Desc As String = DataGridView1.Rows(row).Cells(1).Value
Dim UM As String = DataGridView1.Rows(row).Cells(2).Value
Dim ItemID As String = DataGridView1.Rows(row).Cells("ItemID").Value
'// Get input from user for the description and UOM to update
Dim updDesc As String = InputBox("Update the description from this row", "Update Description", Desc)
Dim updUM As String = InputBox("Update the UOM field from this row", "Update UM", UM)
'// Make sure both description and UOM are not blank
If updDesc.Trim.Length > 0 And updUM.Trim.Length > 0 Then
Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
Application.DoEvents()
Dim json As String = String.Empty
Using client = New HttpClient()
Dim ido As String = IDOName.SelectedItem
Dim requestUrl As String = "">csi10a.erpsl.inforcloudsuite.com/.../" & ido & "?refresh=true"
'// provide token in the Authorization header
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", m_Token)
'// Set the properties to update back to CSI
Dim strDesc As UpdateProperty = New UpdateProperty With {
.Name = "Description",
.Value = updDesc,
.IsNull = False,
.Modified = True
}
Dim UOM As UpdateProperty = New UpdateProperty With {
.Name = "UM",
.Value = updUM,
.IsNull = False,
.Modified = True
}
'// Create the IDOITEM object to contain the properties (fields) we are updating
Dim idoItem As IDOUpdateItem = New IDOUpdateItem With {
.Action = UpdateAction.Update,
.Properties = {strDesc, UOM},
.ItemId = ItemID
}
'// Build the request object that contains the IDOITEM object to update
Dim request As UpdateCollectionRequest = New UpdateCollectionRequest With {.Changes = {idoItem}}
'// Serialize the request object into JSON
Dim contentStr As String = JsonConvert.SerializeObject(request)
'// Call the API with the JSON string
Dim response As HttpResponseMessage = client.PostAsync(requestUrl.ToString(), New StringContent(contentStr, Encoding.UTF8, "application/json")).Result
'// Evaluate the response from the API Call
Using content As HttpContent = response.Content
Dim result As Task(Of String) = content.ReadAsStringAsync()
json = result.Result
End Using
Me.Cursor = System.Windows.Forms.Cursors.Default
If InStr(1, json.ToLower, "success") > 0 Then
MsgBox("Updated Successfully")
Else
MsgBox(json) '// display errors during update
End If
End Using
Else
MsgBox("Please specify both the new description and UOM to update")
End If
End Sub
Supporting classes
#Region "Classes used to update IDO"
Public Class UpdateCollectionRequest
Public Property Changes As IDOUpdateItem()
Public Property RefreshAfterSave As Boolean
Public Property CustomInsert As String
Public Property CustomUpdate As String
Public Property CustomDelete As String
End Class
Public Class IDOUpdateItem
Public Property Action As UpdateAction
Public Property ItemId As String
Public Property ItemNo As Integer
Public Property Properties As UpdateProperty()
Public Property UpdateLocking As UpdateLocking
End Class
Public Class UpdateProperty
Public Property Name As String
Public Property Value As String
Public Property OriginalValue As String
Public Property Modified As Boolean
Public Property IsNull As Boolean
End Class
Public Enum UpdateAction
Insert = 1
Update = 2
Delete = 4
End Enum
Public Enum UpdateLocking
Row = 0
[Property] = 1
End Enum
#End Region