How to add a Field collection to a BO from related BOs?


Author
Message
Edhy Rijo
E
StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
I have a BO which I would like to have access to 2 more related BOs.  The idea is to have a property in the Parent BO to the ChildBO and in this ChildBO to another Child like this:

CustomersBO.OrdersBO.OrderItemsBO.ItemPrice

How can I do that for SF Business Object?

Edhy Rijo

Replies
Edhy Rijo
E
StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Greg, Bill,

Third, on you question about what to do if the BO is empty and having to test it, why not just test it in the property and add the row there? You could initialize the data in that row with values that makes sense when there is no data in the BO.

Thanks, the code is pretty understandable.  About the third option, this where a function like SCATTER MEMVAR BLANK would do the trick on initializing the empty row, unless I am just missing the simple point on using the BO.NewRow() to have a blank row with no values, so any use of its property will not throw an error.  I will test that.

Also thanks for the points about not using the descriptors, you are right and these are not needed for this kind of properties.

All the time I simply get amazed with how functionality that seems pretty difficult or hard to do are accomplish in the .NET world and of course SF with a couple of line of code.Hehe

Edhy Rijo

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Yep, I think the adding the blank row would be very easy. First you call NewRow on the BO, which adds a new row based on the table's schema:



bo.NewRow()



Then you just set the properties:



bo.InsuranceCompanyName = "None"

bo.InsuranceCompanyAddress = String.Empty

etc.



Finally, I'd call AcceptChanges on the table, so it isn't dirty (but doesn't save anything to the database):



bo.CurrentDataTable.AcceptChanges()



Easy peasy BigGrin



I agree that it is amazing how little code is often needed. Initially I spent something like a 10:1 ratio between learning and coding. Often I'd spend a couple of hours to realize it was one line of code! w00t
Edhy Rijo
E
StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Humm,

This is getting better BigGrin when changing the code as follow:

Private _insuranceCompany As InsuranceCompanyBO '--This is at the top of the class

Public ReadOnly Property [InsuranceCompany]() As InsuranceCompanyBO

     Get

          If _insuranceCompany Is Nothing Then

               Dim _insuranceCompany As New InsuranceCompanyBO

               If Not String.IsNullOrEmpty(Me.FK_InsuranceCompany) Then

                    _insuranceCompany.FillByPrimaryKey(Me.FK_InsuranceCompany)

               End If

               '-- Check if the BO is empty...

               If _insuranceCompany.Count = 0 Then

                    '-- Initiailize a row with appropriate values

                    _insuranceCompany.NewRow()

               End If

          End If

          Return _insuranceCompany

     End Get

End Property

I now get an error when trying to use this property in a code like this:

Dim x As String = Me.PolicyBO1.InsuranceCompany.CompanyName

Look at the attached image for the error: NullReferenceException was unhandled by user code.  Object reference not set to an instance of an object.

What I understand about this error is that I would need to create an instance of this object in order to use this property, while when using the original method there was not need to do that, guess because previous method was always creating an instance of the class with this code:

Dim loLookupBO As New InsuranceCompanyBO

 

Edhy Rijo

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
When you call new row, I think is just adds a new row to the data table, with the values being set to whatever is the default for their type. For strings, that is Nothing (null), hence the error.



Don't forget the other part of the code, which is to set values of that new row:

_insuranceCompany.InsuranceCompanyName = String.Empty

...etc.




Do this right after you call NewRow()
Edhy Rijo
E
StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Greg,

So far, the error has nothing to do with setting values to the created new row.  When using BO.NewRow() SF will populate it with the default values which are good enough in my case.

In order to make it work I added a "New" command to initialize the Private BO variable like this:

Private _insuranceCompany As New InsuranceCompanyBO

Then I made some changes to the property code to make sure the BO is filled all the time, like this:

Public ReadOnly Property [InsuranceCompany]() As InsuranceCompanyBO

     Get

          '-- If the private BO has not been created, then create it here.

          If _insuranceCompany Is Nothing Then

               Dim _insuranceCompany As New InsuranceCompanyBO

          End If

          If Not String.IsNullOrEmpty(Me.FK_InsuranceCompany) Then

               _insuranceCompany.FillByPrimaryKey(Me.FK_InsuranceCompany)

          End If

          '-- Check if the BO is empty...

          '-- Initiailize a new row, default values could be added here if needed.

          If _insuranceCompany.Count = 0 Then

               _insuranceCompany.NewRow()

               '-- AcceptChanges on the table, so it isn't dirty (but doesn't save anything to the database)

               _insuranceCompany.CurrentDataTable.AcceptChanges()

          End If

          Return _insuranceCompany

     End Get

End Property

So far the code above is working fine. Smile

Edhy Rijo

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Threaded View
Threaded View
Edhy Rijo - 17 Years Ago
Greg McGuffey - 17 Years Ago
Paul Chase - 17 Years Ago
Bill Cunnien - 17 Years Ago
Bill Cunnien - 17 Years Ago
Edhy Rijo - 17 Years Ago
                     I am not using a parent-child setup. My custom property in...
Bill Cunnien - 17 Years Ago
Paul Chase - 17 Years Ago
                         Thanks a log guys, I will start playing with the info provided right...
Edhy Rijo - 17 Years Ago
                             Well, here the working version of my code :D [quote][codesnippet]...
Edhy Rijo - 17 Years Ago
                                 This stuff sure is fun!! :w00t: Glad it is working for you...I'll...
Bill Cunnien - 17 Years Ago
                                 [quote]I am sure, there should be something I could add to the...
Edhy Rijo - 17 Years Ago
                                     A couple of thoughts...

First, I don't think you need to...
Greg McGuffey - 17 Years Ago
                                         Sigh...It fubarred my code:crazy:. I didn't use html codes...In any...
Greg McGuffey - 17 Years Ago
                                             Hi Greg, Bill, [quote]Third, on you question about what to do if the...
Edhy Rijo - 17 Years Ago
                                                 Yep, I think the adding the blank row would be very easy. First you...
Greg McGuffey - 17 Years Ago
                                                 Humm, This is getting better :D when changing the code as follow:...
Edhy Rijo - 17 Years Ago
                                                     When you call new row, I think is just adds a new row to the data...
Greg McGuffey - 17 Years Ago
                                                         Greg, So far, the error has nothing to do with setting values to the...
Edhy Rijo - 17 Years Ago
Edhy Rijo - 17 Years Ago

Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search