copy other module custom fields to SO custom fields  Topic is solved

Discussions relating to Jiwa 7 plugin development, and the Jiwa 7 API.

copy other module custom fields to SO custom fields

Postby DannyC » Wed Feb 04, 2015 2:38 pm

Hi,

I often need to copy custom fields which may exist in inventory, or debtors into sales order custom fields.

I can set the sales order custom fields OK to data from the entity header e.g debtors email address, but I was hoping there might be a one-liner to read a specific debtor custom field value without having to enumerate the debtor custom fields value collection and storing the contents into a variable.

This is what I have so far:
Code: Select all
   Private Sub SalesOrderCreateNewCompleted(sender As Object, e As System.EventArgs)
      Dim Debtor As JiwaDebtors.Debtor = JiwaApplication.Manager.Instance.BusinessLogicFactory.CreateBusinessLogic(Of JiwaDebtors.Debtor)(Nothing)
      Dim SalesOrder As JiwaSales.SalesOrder.SalesOrder = DirectCast(sender, JiwaSales.SalesOrder.SalesOrder)
      Debtor.Read(SalesOrder.Debtor.DebtorID)
      msgbox(Debtor.AccountNo)
      For Each customFieldValue As JiwaApplication.CustomFields.CustomFieldValue In salesOrder.CustomFieldValues
          If customFieldValue.CustomField.PluginCustomField.Name = "EMail" Then
              customFieldValue.Contents = Debtor.EmailAddress
              Exit For
          End If
         If customFieldValue.CustomField.PluginCustomField.Name = "DBCustField_1"
            customFieldValue.Contents = Debtor.CustomFieldValues'something else here   
         End If   
      Next
   End Sub


Thanks
User avatar
DannyC
Senpai
Senpai
 
Posts: 718
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 31

Re: copy other module custom fields to SO custom fields  Topic is solved

Postby Mike.Sheen » Wed Feb 04, 2015 7:09 pm

Hi Danny,

In Bug 10647 of 7.00.89.00 we introduced some methods to do this.

In your snippet of code, you can replace

Code: Select all
For Each customFieldValue As JiwaApplication.CustomFields.CustomFieldValue In salesOrder.CustomFieldValues
          If customFieldValue.CustomField.PluginCustomField.Name = "EMail" Then
              customFieldValue.Contents = Debtor.EmailAddress
              Exit For
          End If
         If customFieldValue.CustomField.PluginCustomField.Name = "DBCustField_1"
            customFieldValue.Contents = Debtor.CustomFieldValues'something else here   
         End If   
      Next


With This:

Code: Select all
salesOrder.CustomFieldValues.ItemFromSettingName("EMail").Contents = Debtor.EmailAddress
salesOrder.CustomFieldValues.ItemFromSettingName("DBCustField_1").Contents = Debtor.CustomFieldValues.ItemFromSettingName("DBCustField_1").Contents


I'm assuming you have a sales order custom field named "DBCustField_1" AND a debtor custom field named "DBCustField_1". I also note you were exiting the For Each loop when you had encountered the "Email" custom field - I'm assuming that was in error - doing so would mean your "DBCustField_1" value would not be set if the "Email" field was encountered first.

Also note that it is possible to have multiple plugins with the same custom field name - so in the code I supplied above you won't know for sure which one was used - to ensure you do get the right value in case of overlapping custom field names, there is an overloaded method you can use which specifies both the plugin name and the custom field name:

Code: Select all
salesOrder.CustomFieldValues.ItemFromSettingName("EMail", "My Plugin Name").Contents = Debtor.EmailAddress
salesOrder.CustomFieldValues.ItemFromSettingName("DBCustField_1", "My Plugin Name").Contents = Debtor.CustomFieldValues.ItemFromSettingName("DBCustField_1", "My Plugin Name").Contents


It's quite possible for such overlaps to exist - so I would err on the side of caution and use the latter example.

Mike
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2583
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 807

Re: copy other module custom fields to SO custom fields

Postby DannyC » Thu Feb 12, 2015 4:47 pm

I am getting an error using both versions, with & without the plugin name.

When creating a new sales order, I want those debtor field values to land in the sales order custom fields.
Code: Select all
    Public Sub Setup(ByVal JiwaForm As JiwaApplication.IJiwaForm, ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaFormPlugin.Setup
      If TypeOf JiwaForm Is JiwaSalesUI.SalesOrder.SalesOrderEntryForm Then
         Dim SalesOrderForm As JiwaSalesUI.SalesOrder.SalesOrderEntryForm = DirectCast(JiwaForm, JiwaAPplication.IJiwaForm)
         AddHandler SalesOrderForm.SalesOrder.CreateEnd, AddressOf SalesOrderCreateNewCompleted            
      End If
    End Sub
   
   Private Sub SalesOrderCreateNewCompleted(sender As Object, e As System.EventArgs)
      Dim Debtor As JiwaDebtors.Debtor = JiwaApplication.Manager.Instance.BusinessLogicFactory.CreateBusinessLogic(Of JiwaDebtors.Debtor)(Nothing)
      Dim SalesOrder As JiwaSales.SalesOrder.SalesOrder = DirectCast(sender, JiwaSales.SalesOrder.SalesOrder)
      Debtor.Read(SalesOrder.Debtor.DebtorID)
      SalesOrder.CustomFieldValues.ItemFromSettingName("Email").Contents = Debtor.EmailAddress
      messagebox.show(Debtor.CustomFieldValues.ItemFromSettingName("Overdue").Contents)
      messagebox.show(Debtor.CustomFieldValues.ItemFromSettingName("ShowInvoice").Contents)
      'SalesOrder.CustomFieldValues.ItemFromSettingName("DBOverdue").Contents = Debtor.CustomFieldValues.ItemFromSettingName("Overdue").Contents   
      'SalesOrder.CustomFieldValues.ItemFromSettingName("SendInvoice").Contents = Debtor.CustomFieldValues.ItemFromSettingName("SendInvoice").Contents   
   End Sub


When I create the new sales order, I get an error Object reference not set to an instance of an object. It is like it can't read my debtor custom fields. The last 2 lines are remmed on purpose in case the error was caused by assigning the values, but I still get the error.

Cheers
Danny
User avatar
DannyC
Senpai
Senpai
 
Posts: 718
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 31

Re: copy other module custom fields to SO custom fields

Postby Mike.Sheen » Sun Feb 22, 2015 10:27 am

DannyC wrote:
When I create the new sales order, I get an error Object reference not set to an instance of an object. It is like it can't read my debtor custom fields. The last 2 lines are remmed on purpose in case the error was caused by assigning the values, but I still get the error.

Cheers
Danny


Hi Danny,

I don't see the cause - the only reason I can think of is you have the custom field name incorrect - remember they are case sensitive. Try putting a System.Diagnostics.Debugger.Break() line in and then tracing with visual studio to identify the error by way of inspection.

Mike
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2583
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 807

Re: copy other module custom fields to SO custom fields

Postby DannyC » Wed Feb 25, 2015 1:36 pm

I have doubled checked the spelling and case. No issues there.
I have simplified another similar plugin just to display the custom field contents but am still getting errors.
Code: Select all
Public Class FormPlugin
    Inherits System.MarshalByRefObject
    Implements JiwaApplication.IJiwaFormPlugin

    Public Overrides Function InitializeLifetimeService() As Object
        ' returning null here will prevent the lease manager
        ' from deleting the Object.
        Return Nothing
    End Function

    Public Sub SetupBeforeHandlers(ByVal JiwaForm As JiwaApplication.IJiwaForm, ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaFormPlugin.SetupBeforeHandlers
    End Sub

    Public Sub Setup(ByVal JiwaForm As JiwaApplication.IJiwaForm, ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaFormPlugin.Setup
      Dim InventoryForm As JiwaInventoryUI.InventoryMaintenanceForm = DirectCast(JiwaForm, JiwaInventoryUI.InventoryMaintenanceForm)
      AddHandler InventoryForm.Inventory.CustomFieldValues.Changed, AddressOf ChangedCustomField   
   End Sub
   
   Sub ChangedCustomField(Sender As Object, e As System.EventArgs)      
      Dim Inventory As JiwaInventory.Inventory = DirectCast(sender, JiwaInventory.Inventory)
      Msgbox(Inventory.CustomFieldValues.ItemFromSettingName("MyCustomField").Contents)
      End Sub   
End Class


I can't seem to work out why this isn't working. Can you try creating a plugin in your Jiwa with my two examples (one for sales orders and the other in this post for inventory) and see if you get those errors too? I don't have access to VS.
User avatar
DannyC
Senpai
Senpai
 
Posts: 718
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 31

Re: copy other module custom fields to SO custom fields

Postby Mike.Sheen » Wed Feb 25, 2015 4:09 pm

DannyC wrote:I have doubled checked the spelling and case. No issues there.

I can't seem to work out why this isn't working. Can you try creating a plugin in your Jiwa with my two examples (one for sales orders and the other in this post for inventory) and see if you get those errors too? I don't have access to VS.


Try the attached and see if that works for you or not (it's for the sales order create end - sets the DBCustField_1 and Email custom setting on the sales order to be that of the debtor custom field contents). That should work for you.
Plugin Test.xml
Sample Plugin
(34.75 KiB) Downloaded 1249 times


Your new snippet of code is probably failing for another reason altogether - you're trying to cast the sender to an inventory business logic object, when in that case sender is completely something else.

Mike
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2583
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 807

Re: copy other module custom fields to SO custom fields

Postby Scott.Pearce » Thu Feb 26, 2015 9:44 am

DannyC wrote:I don't have access to VS.


You should be using Visual Studio. I can't imagine trying to debug plugins without it.
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 765
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 230

Re: copy other module custom fields to SO custom fields

Postby DannyC » Thu Mar 05, 2015 6:21 pm

Mike,
you're trying to cast the sender to an inventory business logic object, when in that case sender is completely something else.

Yes but I don't know how to fix it.

Here's the code snippet from above again. I have tried to recode but I still get errors.
Code: Select all
    Public Class FormPlugin
        Inherits System.MarshalByRefObject
        Implements JiwaApplication.IJiwaFormPlugin

        Public Overrides Function InitializeLifetimeService() As Object
            ' returning null here will prevent the lease manager
            ' from deleting the Object.
            Return Nothing
        End Function

        Public Sub SetupBeforeHandlers(ByVal JiwaForm As JiwaApplication.IJiwaForm, ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaFormPlugin.SetupBeforeHandlers
        End Sub

        Public Sub Setup(ByVal JiwaForm As JiwaApplication.IJiwaForm, ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaFormPlugin.Setup
          Dim InventoryForm As JiwaInventoryUI.InventoryMaintenanceForm = DirectCast(JiwaForm, JiwaInventoryUI.InventoryMaintenanceForm)
          AddHandler InventoryForm.Inventory.CustomFieldValues.Changed, AddressOf ChangedCustomField   
       End Sub
       
       Sub ChangedCustomField(Sender As Object, e As System.EventArgs)     
          Dim Inventory As JiwaInventory.Inventory = DirectCast(sender, JiwaInventory.Inventory)
          Msgbox(Inventory.CustomFieldValues.ItemFromSettingName("MyCustomField").Contents)
          End Sub   
    End Class
User avatar
DannyC
Senpai
Senpai
 
Posts: 718
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 31

Re: copy other module custom fields to SO custom fields

Postby Mike.Sheen » Thu Mar 05, 2015 6:35 pm

DannyC wrote:Yes but I don't know how to fix it.


Hi Danny,

The sender in the case of the CustomFieldValues.Changed event is a type of JiwaApplication.CustomFields.CustomFieldValue - so cast the sender to that - i.e. something like this:

Code: Select all
       
Sub ChangedCustomField(Sender As Object, e As System.EventArgs)
    Dim customFieldValue As JiwaApplication.CustomFields.CustomFieldValue = DirectCast(sender, JiwaApplication.CustomFields.CustomFieldValue)
    Msgbox(String.Format("Custom Field '{0}' has a value of '{1}'", customFieldValue.CustomField.PluginCustomField.Name, customFieldValue.Contents)
End Sub   


Mike
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2583
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 807

Re: copy other module custom fields to SO custom fields

Postby DannyC » Fri Mar 06, 2015 1:15 pm

That's great.

I can now get the Custom field value for the custom field I have just changed.
How can I write to another custom field, same inventory item?
in Pseudo
When "Custom Field 1" has changed
modify "Custom Field 2"
I have
Code: Select all
   Sub ChangedCustomField(Sender As Object, e As System.EventArgs)      
      Dim customFieldValue As JiwaApplication.CustomFields.CustomFieldValue = DirectCast(sender, JiwaApplication.CustomFields.CustomFieldValue)
      Dim inventory As JiwaInventory.Inventory = JiwaApplication.Manager.Instance.BusinessLogicFactory.CreateBusinessLogic(Of JiwaInventory.Inventory)(Nothing)
      If customFieldValue.CustomField.PluginCustomField.Name = "CustomField1" Then
         If CustomFieldValue.Contents = 0 Then
            inventory.CustomFieldValues.ItemFromSettingName("CustomField2").Contents = ""
         End If
         If CustomFieldValue.Contents = 1 Then
            inventory.CustomFieldValues.ItemFromSettingName("CustomField2").Contents = "Some text"
         End If
End If
End Sub

I don't get an error, but nothing happens. I am pretty sure it is because I need to Read the inventory object & pass in the InventoryID, but how do I get the inventoryID from the custom field values? My CustomField1 is a combobox with about half a dozen options. I just want to populate another Custom Field with some free text based on the changed combo box value.
As an aside I would normally use a SELECT CASE ...END CASE but that didn't work either. I think I might be able to suss that out though.
For now, I just need to write into another custom field.
User avatar
DannyC
Senpai
Senpai
 
Posts: 718
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 31

Next

Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 4 guests