Page 1 of 1

Custom column values getting erased

PostPosted: Mon May 28, 2018 10:21 am
by neil.interactit
Hey guys,

I have a mystery gremlin blanking values in a custom column, that I can't track down.

To replicate, add a new C# plugin, paste in the code attached, add form reference JiwaFinancials.Jiwa.JiwaCRBatchTXUI.CreditorPurchases, restart. Add a new creditor purchase, add a line item, save, add a CRN value, save. You can now close/reopen the form and see the CRN value persisted.

Now add a second line item. Note the CRN value is erased (without any call to CRBatchTransObject_CreateReadEnd).

I'm stumped. Can you advise how to rectify this?

(Note the really dodgy persistence in the attached sample code ... I extracted a minimal sample from more extensive code and stripped out the database persistence)

Cheers,
Neil

Re: Custom column values getting erased

PostPosted: Mon May 28, 2018 10:31 am
by Scott.Pearce
I'll begin by suggesting you use a "Creditor Purchase Lines" custom field instead. This sidesteps the issue and means I don't have to debug it ;) Let me know if the custom field will suffice.

Re: Custom column values getting erased

PostPosted: Mon May 28, 2018 10:39 am
by neil.interactit
Could you send a demo code snippet ... I saw the CRBatchTransObject.LineCustomFields property, poked around the forum, but couldn't spot how to implement.

Re: Custom column values getting erased

PostPosted: Mon May 28, 2018 11:29 am
by Scott.Pearce
If you simply define one in the "Custom Fields" tab of the plugin, and then restart Jiwa, you will see the field magically appear on the Creditor Purchases lines grid. All the reading and saving of the custom field value is done for you.

I've attached a plugin that shows how to retrieve a line custom field value using code. In the example, a line custom field called "My CRN" is created on the Creditor Purchases lines grid. When a line is edited a message box is displayed showing the value for "My CRN" for that line.

Handler added:

Code: Select all
        public void Setup(JiwaFinancials.Jiwa.JiwaApplication.IJiwaForm jiwaForm, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin plugin)
        {
            if (!(jiwaForm is CreditorCreditAdjustment || jiwaForm is CreditorDebitAdjustment || jiwaForm is CreditorPayment || jiwaForm is CreditorPurchases)) return;
            var form = (CRBatchTXPurchases)jiwaForm;
            AddColumnCrn(form);
         
         form.CRBatchTransObject.TransLines.DispersalChanged += CRBatchTransObject_TransLines_DispersalChanged;
        }


Code that retrieves and displays the line custom field value:

Code: Select all
      public void CRBatchTransObject_TransLines_DispersalChanged(JiwaFinancials.Jiwa.JiwaCRBatchTX.CRBatchDispersal item, System.ComponentModel.PropertyChangedEventArgs e)
      {      
         //Find the custom field value
         JiwaFinancials.Jiwa.JiwaApplication.CustomFields.CustomFieldValue customFieldValue = item.CustomFieldValues.get_ItemFromSettingName("My CRN","Custom Field Demo");
         
         //Display a message if a custom field value exists
         if (customFieldValue != null && customFieldValue.Contents != null)
            System.Windows.Forms.MessageBox.Show(string.Format("Value of {0}: {1}",customFieldValue.CustomField.PluginCustomField.Name, customFieldValue.Contents));
         
      }


Notes:
1. C# will not intelli-sense the get_ItemFromSettingName method (because it is a property with 2 parameters which c# does not like). The alternative to making a call to get_ItemFromSettingName would be to simply iterate through the collection and matching on CustomFieldValue.CustomField.PluginCustomField.Name thus:

Code: Select all
                Dim myCustomFieldValue  as JiwaFinancials.Jiwa.JiwaApplication.CustomFields.CustomFieldValue = nothing;
                For Each CustomFieldValue As CustomFieldValue In Me
                    If CustomFieldValue.CustomField.PluginCustomField.Name = SettingName AndAlso CustomFieldValue.CustomField.PluginCustomField.CustomFieldCollection.Plugin.Name = PluginName Then
                        myCustomFieldValue  = CustomFieldValue
                        exit for
                    End If
                Next


2. The creditor purchases batch object is a bit of an ugly duckling - a dispersal collection sits under each line. What you see on the grid isn't really a line, but a dispersal. This is why the line custom field collection hangs off the dispersal class.

3. To *modify* the line custom field value in code, simply push a value into customFieldValue.Contents. It will be saved away to the database when the Creditor Purchase object is saved.

4. Note that there is a difference between a Custom Field collection and a Custom Field *Value* collection.

Re: Custom column values getting erased

PostPosted: Mon May 28, 2018 6:09 pm
by neil.interactit
Thanks Scott.

The custom column ... too easy [head thump!]

Any chance of a code snippet showing accessing line custom values from the form object? I need to enable/disable activate based on business logic around values being required for certain creditors. So I need to access the custom values from form.CRBatchTransObject.ReadEnd ...

Code: Select all
            form.CRBatchTransObject.ReadEnd += (sender, e) => EnableActivate(form);
        }

        private static void EnableActivate(CRBatchTXPurchases form)
        {
            if (form.CRBatchTransObject.BatchStatus == CreditorBatchTrans.BatchStatusTypes.Activated) return;
            // access and check custom line items here
        }


And BTW, I can't download your attached zip ... I get
General Error
SQL ERROR [ mysql4 ]
Data too long for column 'ip' at row 1 [1406]
INSERT INTO download_log(attach_id, user_id, datetime, ip) VALUES(672, 620, NOW(), '2a01:44c8:5320:3405:c65e:51bb:d01e:1fbc')
(I guess my PC is supplying IPV6)

Cheers,
Neil.

Re: Custom column values getting erased

PostPosted: Mon May 28, 2018 6:12 pm
by Mike.Sheen
neil.interactit wrote:And BTW, I can't download your attached zip ... I get
General Error
SQL ERROR [ mysql4 ]
Data too long for column 'ip' at row 1 [1406]
INSERT INTO download_log(attach_id, user_id, datetime, ip) VALUES(672, 620, NOW(), '2a01:44c8:5320:3405:c65e:51bb:d01e:1fbc')
(I guess my PC is supplying IPV6)


This should be fixed - can you try again?

Re: Custom column values getting erased

PostPosted: Tue May 29, 2018 9:06 am
by neil.interactit
Downloads OK now, thanks Mike.

Re: Custom column values getting erased

PostPosted: Wed May 30, 2018 9:25 am
by neil.interactit
Bump. I can't quite see how to iterate through the CustomFieldValues from the CRBatchTXPurchases form object. Any advice appreciated.

Re: Custom column values getting erased  Topic is solved

PostPosted: Thu May 31, 2018 9:51 am
by Scott.Pearce
neil.interactit wrote:I can't quite see how to iterate through the CustomFieldValues from the CRBatchTXPurchases form object


The form object has a business logic object. The custom field values are in the business logic object.

Code: Select all
        private static void EnableActivate(CRBatchTXPurchases form)
        {
            if (form.CRBatchTransObject.BatchStatus == CreditorBatchTrans.BatchStatusTypes.Activated) return;
            // access and check custom line items here
         JiwaFinancials.Jiwa.JiwaCRBatchTX.CreditorBatchTrans creditorPurchasesBatch = (JiwaFinancials.Jiwa.JiwaCRBatchTX.CreditorBatchTrans)form.CRBatchTransObject;
         foreach(JiwaFinancials.Jiwa.JiwaCRBatchTX.CRBatchTranLine existingLine in creditorPurchasesBatch.TransLines)
         {
            foreach (JiwaFinancials.Jiwa.JiwaCRBatchTX.CRBatchDispersal existingDispersal in existingLine.Dispersals)
            {
               foreach(JiwaFinancials.Jiwa.JiwaApplication.CustomFields.CustomFieldValue existingCustomFieldValue in existingDispersal.CustomFieldValues)
               {
                  if(existingCustomFieldValue.CustomField.PluginCustomField.Name == "My CRN" && existingCustomFieldValue.CustomField.PluginCustomField.CustomFieldCollection.Plugin.Name == "Custom Field Demo")
                  {
                     //We have found the particular custom field we are interested in interrogating
                     if (existingCustomFieldValue != null && existingCustomFieldValue.Contents != null)
                        System.Windows.Forms.MessageBox.Show(string.Format("Line {0}, dispersal {1} - value of {2}: {3}",existingLine.ItemNo, existingDispersal.ItemNo, existingCustomFieldValue.CustomField.PluginCustomField.Name, existingCustomFieldValue.Contents));
                  }
               }
            }
         }
        }   


I've attached an updated plugin with the code above.

Re: Custom column values getting erased

PostPosted: Thu May 31, 2018 11:52 am
by neil.interactit
Sweet. Works a treat! Many thanks Scott.