crm 2011 Any way to consistently get string value of attribute without casting? - dynamics-crm-2011

I want to iterate thru the set of attributes for an entity and for each one, get the text value. I can do this by getting the attribute type and explicit casting but is there a way to do it regardless of type? Similar to this method...

I don't think you'll be able to simply get a text value without testing for and handling the specific field types that are possible on an entity, since some field types have more complex values where you'll need to decide what value you want to get from the field. I ran into this same situation and ended up building a function to do the heavy lifting for me. Basically I did a case statement on the attribute type and got the values I wanted based on the business needs, here's an example of the function I used:
Private Function GetAttributeValue(ByRef pEntity As Entity, ByRef pAttr As KeyValuePair(Of String, Object)) As String
Dim lstrValue As String = String.Empty
Select Case pAttr.Value.GetType
Case GetType(OptionSetValue)
lstrValue = String.Format("{0} - {1}", CType(pAttr.Value, OptionSetValue).Value.ToString(), pEntity.FormattedValues(pAttr.Key))
Case GetType(EntityReference)
lstrValue = CType(pAttr.Value, EntityReference).Id.ToString()
'... could also get the Name value from the EntityReference
Case GetType(Money)
lstrValue = CType(pAttr.Value, Money).Value.ToString()
Case GetType(Date)
lstrValue = CType(pAttr.Value, Date).ToString("MM/dd/yy hh:mm:ss")
Case GetType(Guid)
lstrValue = CType(pAttr.Value, Guid).ToString()
Case Else
'... assume a string and just return the attribute value, or do some other handling
End Select
Return lstrValue
End Function
Once I had this function, I could just iterate through the attributes on the entity I was dealing with and get some text value back that represented the value for each field regardless of the field type. One thing to note, in my example I'm getting the string value of OptionSet fields using .FormattedValues(), I believe you can only use that if your entities have the Id unique identifier field populated.

you can use the method GetFormattedAttributeValue of the Entity class.
Example:
Entity myAccount = service.Retrieve("account", accountId, new ColumnSet(true));
string createdOn = myAccount.GetFormattedAttributeValue("createdon");

Related

Execute the code stored as a String in VB.NET

I wonder about how I can store a part of code in a string field of an object and convert it at run-time in executable code.
Let's say I have my class:
Public Class Car
Public m_IDCar As String
Public m_Brand As String
Public m_Description As String
Public m_Condition As String ' => here I need to store an If, or an If condition as a String, that will be executed at run-time.
End Class
Then the code that happens when CommandButton1.Click():
Dim carList As List(Of Car)
Dim parameter as String
' here I create carList with data from a database, so for each car In the database I create its relative object, with its .m_IDCar, .m_Brand, .m_Description and .m_Condition, and add it to carList
parameter = TextBox1.Text ' => given in input by the user
For each car As Car in carList
If (car.m_Condition = True) then ' => here there must be something to do cause, as things are now, in car.m_Condition is stored a String, but I need to parse it in code that returns a boolean value and, if this value would be True, the code will enter in the If statement.
'do something
End If
Next
Example of car.m_Condition could be:
car.m_Condition = "(car.m_Name=""BMW"" AND car.m_Description.Contains(parameter)) OR car.m_Brand=""AUDI"""
I need some tips on how to implement this approach, if someone would help me.
TY!
EDIT:
Thanks David, I have seen that question and seems a lot similar. I've seen that the user asked how store the entire If in a string:
Dim code As String = "IIf(1 = 2, True, False)"
(the users that asked that question used IIf)
The best way for me would be to store the condition to evaluate in a string and, maybe, the value to with compare the result in another one. So, for example:
Dim condition As String = "(car.m_Name=""BMW"" AND car.m_Description.Contains(parameter)) OR car.m_Brand=""AUDI"""
Dim valueToCompareWith as String = "True"
and the following If
If (car.m_Name=""BMW"" AND car.m_Description.Contains(parameter)) OR car.m_Brand=""AUDI"" = True) Then
'do something
End If
will become:
If (condition = valueToCompareWith) Then '(conceptually, because in this form it's simply a String comparison that returns always False)
'do something
End If
EDIT2:
Thanks Plutonix, I've read about your hint on Getters and Setters, I've not specified it, but the context I'm working on is a lot more complex then this. I've made a very simple example just for focus the problem, but I'm working with a good amount of objects, with various fields to compare and for every comparison there are different conditions in the If, with different logics and different types of data to compare.

Exporting Business Account attributes with Acumatica API

Our Business Accounts in Acumatica have 13 custom Attributes for our main Business Account Class. I've been able to save values to the Attributes successfully, based on Acumatica's example "Adding Records to the Business Accounts and Opportunities Forms". But I have not been able to figure out how to retrieve the values with an Export.
First, I tried using a format similar to how the field was specified when saving them.
Public Function GetCustomerAttributes(ByVal customerID As String) As String()()
Dim customer As CR303000Content = m_context.CR303000GetSchema()
m_context.CR303000Clear()
Dim idFilter As Filter = New Filter()
idFilter.Field = customer.AccountSummary.BusinessAccount
idFilter.Condition = FilterCondition.Equals
idFilter.Value = customerID
' SIMILAR TO EXAMPLE FOR SAVING
Dim awdField As Field = New Field()
awdField.ObjectName = customer.Attributes.Attribute.ObjectName
awdField.FieldName = "AWD Number"
Dim searchfilters() As Filter = {idFilter}
Dim searchCommands() As Command = {awdField}
Dim searchResult As String()() = m_context.CR303000Export(searchCommands, searchfilters, 0, False, False)
Return searchResult
End Function
I thought this would return one result with the value for our attribute named "AWD Number". Instead, it returned 13 results, one for each attribute, and the value of each one was blank. I changed the FieldName to customer.Attributes.Attribute.FieldName and then it started returning the name of each attribute. So I thought if I added another field for the value, then I might get the name and value in separate results, like this:
Public Function GetCustomerAttributes(ByVal customerID As String) As String()()
Dim customer As CR303000Content = m_context.CR303000GetSchema()
m_context.CR303000Clear()
Dim idFilter As Filter = New Filter()
idFilter.Field = customer.AccountSummary.BusinessAccount
idFilter.Condition = FilterCondition.Equals
idFilter.Value = customerID
Dim awdField As Field = New Field()
awdField.ObjectName = customer.Attributes.Attribute.ObjectName
awdField.FieldName = customer.Attributes.Attribute.FieldName
Dim awdValue As Field = New Field()
awdValue.ObjectName = customer.Attributes.Attribute.ObjectName
awdValue.FieldName = customer.Attributes.Attribute.Value
Dim searchfilters() As Filter = {idFilter}
Dim searchCommands() As Command = {awdField, awdValue}
Dim searchResult As String()() = m_context.CR303000Export(searchCommands, searchfilters, 0, False, False)
Return searchResult
End Function
I did get a 2-item array back for each of the 13 results, but the value in the second field was still blank.
Does anyone know how I can get the values? I don't really care if I have to get them one at a time, but I'd prefer to get them all at once with their names or codes so that I don't have to rely on the indices always staying the same. Below are images of the debugger running on my second example and view in Acumatica. Thanks!
Your first attempt is correct, however you're not using the right object name and field name. The system will dynamically add fields to the primary object (view) of the screen, in this case the object name represented by customer.AccountSummary.BusinessAccount.ObjectName variable (I suggest you use the debugger to see what this value equals too - good learning exercise).
The attribute field name will use the same naming convention as used in How To Retrieve An Attribute Field In StockItems In Acumatica API?. The naming convention is _Attributes. The attribute ID is not the attribute name; I don't see your configuration but I doubt in your case the Attribute ID is "AWD Number". To summarize, the code will look like:
Dim awdField As Field = New Field()
awdField.ObjectName = customer.AccountSummary.BusinessAccount.ObjectName
awdField.FieldName = "AWDNumber_Attributes"
In your example, by putting the Attributes.Attribute.ObjectName object, the system will iterate through all values inside this table, and then return for every row the fields you want. I'm not exactly sure why you're not seeing all the attribute values in this case, but I think you should be fine with the example above.

Linq query to return boolean

I am inserting data into my entity table using .AddObject(). The object is of the entity table's type. The object is eventStudent, it has string eventStudent.ID, bool eventStudent.StudentPresent, bool eventStudent.ParentPresent.
The students are a list of strings containing student ids. Their presence at the event is in another object called attendees, consisting of String studentID, bool studentPresent and bool parentPresent. Only student id's that have true for StudentPresent and/or ParentPresent are in the attendees list.
As I load up my eventStudent object, I need to set StudentPresent and ParentPresent. This is what I came up with:
foreach (StudentMinimum student in students)
{
eventStudent.StudentPresent = (from a in attendees
where a.StudentID.Contains(student.StudentID)
&& a.StudentPresent
select a.StudentPresent);
}
I receive the error cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'bool'
How can I improve my query so eventStudent.StudentPresent is set to either True or False?
The compiler doesn't know what type will be returned from your query as you haven't explicitly casted it to a type. As a result, it gets a generic IEnumerable type (there could be many records returned right? Hence the IEnumerable. And those records could each be of any type, hence the generic type).
So if the data in your DB were bad and you got multiple records back, converting:
StudentPresent
true
false
false
to a bool is not going to happen. There are a few ways you could get around this. Personally, I'd do something like
var studentPresent = (from a in attendees
where a.StudentID.Contains(student.StudentID)
&& a.StudentPresent
select a.StudentPresent).FirstOrDefault();
eventStudent.StudentPresent = (bool)studentPresent;
Well, actually, I'd use a lambda query instead but that's just personal preference.

How to emulate the ScriptingContext's "ASPTypeLibrary.Application" Object

I have been tasked with modifying some legacy ActiveX DLLs written in Visual Basic 6. One of the things I need to do is to emulate the "ScriptingContext" object, (so that we can support other mechanisms for running the DLLs other than IIS without having to re-write large chunks of the code).
Something that has been causing me some grief is the "ASPTypeLibrary.Application" object which has two very different ways to access its stored values, eg:
.Application("KeyName")
or
.Application.Value("KeyName")
How can I create my own VB6 class which supports both of these access mechanisms? I can do one or the other but not both?
(a simple code example would be great thanks, I'm not a VB6 programmer)
I have found a way to do this, see the code snippet below taken from two classes, "clsContext" and "clsContextApp". The latter implements the ".Value" functionality and the former has the ".Application" property...
I have now discovered an even more difficult problem. The ScriptingContext's "ASPTypeLibrary.Request" object has three different ways to access its ".Request.QueryString" property:
.Request.QueryString("KeyName")
or
.Request.QueryString.Value("KeyName")
or
.Request.QueryString
The last method returns a string comprised of all the Key/Value pairs concatenated by "&" characters. I have no idea how to implement this?
' clsContext
Public ContextApp As clsContextApp
Public Property Get Application(Optional ByRef Key As Variant = Nothing) As Variant
If (Key Is Nothing) Then
Set Application = ContextApp
Else
If (Not ContextApp.p_Application.Exists(Key)) Then
Application = ""
Else
Application = ContextApp.p_Application.Item(Key)
End If
End If
End Property
Public Property Let Application(ByRef Key As Variant, ByVal Value As Variant)
If (VarType(Key) = vbString) Then
If (VarType(Value) = vbString) Then
If (Not ContextApp.p_Application.Exists(Key)) Then
ContextApp.p_Application.Add Key, Value
Else
ContextApp.p_Application.Item(Key) = Value
End If
End If
End If
End Property
' clContextApp
Public p_Application As Scripting.Dictionary
Public Property Get Value(Key As String) As String
If (Not p_Application.Exists(Key)) Then
Value = ""
Else
Value = p_Application.Item(Key)
End If
End Property
Public Property Let Value(Key As String, Value As String)
If (Not p_Application.Exists(Key)) Then
p_Application.Add Key, Value
Else
p_Application.Item(Key) = Value
End If
End Property
Well I've managed to answer the additional question regarding ScriptingContext's "ASPTypeLibrary.Request" object which has three different ways to access its ".Request.QueryString" property.
I've included a code snippet below that is based on the code from my previous answer for the "ASPTypeLibrary.Application" object. If I add a new Property to the "clsContextApp" class and make it the default property for that class, then it will be called when the ".Application" property is called without any qualification eg:
MyString = Context.Application
Setting a particular property as the default property in VB6 is a little obscure, but I followed the directions I found here.
' clsContextApp Default Property
Property Get Values(Optional ByVal Index As Integer = -1) As String ' This is the Default Value for clsContextApp
Attribute Values.VB_UserMemId = 0
Dim KeyName As String, Value As String
Values = ""
If (Index < 0) Then
For Index = 0 To p_Application.Count - 1
KeyName = p_Application.Keys(Index)
Value = p_Application.Item(KeyName)
If (Index > 1) Then
Values = Values + "&"
End If
Values = Values + KeyName + "=" + Value
Next Index
Else
If (Index < p_Application.Count) Then
KeyName = p_Application.Keys(Index)
Value = p_Application.Item(KeyName)
Values = KeyName + "=" + Value
End If
End If
End Property
Adding a reference to Microsoft Active Server Pages Object Library, and to COM+ Services Type Library, and then using the object browser reveals some basic things you seem to be missing.
GetObjectContext is a global method in COMSVCSLib with no arguments used to retrieve the current ObjectContext as its return value.
ObjectContext is a Class. It has a read-only default property, named Item that takes a String argument and is of type Variant.
Passing "Application" as an argument to Item returns the current instance of the Application Class.
ScriptingContext is a Class. It is obsolete.
Application is another Class. It has a default property named Value that takes a String argument and is of type Variant.
Value is a property of the Application Class and provides access to a read-write key/value pair store where keys are always Strings. Since it is of type Variant you can store objects as well as simple values and arrays of various types.
None of this looks difficult to replicate in VB6. The key/value store could be a Collection or Scripting.Dictionary.

Visual basic string issues

Every time i try to attribute any type of string to this i get Object reference not set to an instance of an object. I have tried every combination of possible way to handle the string, convert it to a string again and all the fuzz. It's very frustrating and i guess it's some kind of base principle of the structure/class usage and the string array or whatnot (which is also very dumb)
Private Class movie
Public name As String
Public actors As String
Public year As Integer
Public country As String
Public votes As Integer
End Class
Private movies() As movie
If File.Exists(OpenFileDialog1.FileName) Then
lblPath.Text = OpenFileDialog1.FileName
Dim iFile As New StreamReader(lblPath.Text)
While Not iFile.EndOfStream
current = iFile.ReadLine
movies(i).name = "sasasasa"
lbMovies.Items.Add(movies(i).name)
i = i + 1
End While
End If
these are the code parts where i use it
You are creating an empty array of movie objects, as was pointed out previously. Consequently movies(i) is Nothing. When you try to access a member (movies(i).name) the appropriate exception is generated. Note that your code does not even reach the assignment operator = but fails prior to that. In other words, this has nothing to do with strings altogether; you will get the same error if you write movies(i).votes = 42 instead. To fix your code you will first have to create a movie object, populate it and append it to your array.

Resources