Community
Showing results for 
Search instead for 
Do you mean 
Reply

Best way to save data to contact?

Copper Contributor
Posts: 12
Country: USA

Best way to save data to contact?

[ Edited ]

Is there a best practice on saving a piece of data to a contact object as part of my addin?

 

The user will be dragging text into a contact TabPage, and I would like to associate the text with the contact object and save the text.  I would prefer not changing the database schema (adding tables or columns).

 

I have looked through some of the custom table and custom subentity stuff.  It would be fine if I were developing for my own company.  But in distributing an addin, I want something less invasive.  Is there a string field people generally use for this purpose?  Something like TBL_CONTACT.GO_AHEAD_AND_ABUSE_ME?

 

Thanks for any feedback, this is my first addin.

Employee
Posts: 1,163
Country: USA

Re: Best way to save data to contact?

By far the easiest way to save changes to a contact would be to call it's Update() method. I'm not totally clear on what you mean by "dragging text" as none of the act field types accepts drag/drop, but you can capture a field, set it's value and then save the contact.

Matthew Wood
Act! SDK Support
Community Moderator
Copper Contributor
Posts: 12
Country: USA

Re: Best way to save data to contact?

The string data is dragged onto a custom TabPage that includes a drag-drop enabled control

 

I think that I am best off saving this string to a custom field.  That seems to be the way to go?

Employee
Posts: 1,163
Country: USA

Re: Best way to save data to contact?

Yea, I'd create a field to save the data in, you don't need to create a table for this to reside (you could just create a new column on the contact table) and you could save the data to an existing field, but if your planning on distributing it then you'd have no way of knowing whether that fields is already in use or not, so again, creating a field is probably the safest route.

Matthew Wood
Act! SDK Support
Community Moderator
Copper Contributor
Posts: 12
Country: USA

Re: Best way to save data to contact?

How do I delete a field?

 

I added it with myField =

NewFieldDescriptor("myField", Act.Framework.RecordType.Contact, FieldDataType.Memo)

ActFwk.Fields.Save(myField)

But I don't know how to get rid of it. Took a stab with:

 

Private Sub button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button.Click

	' find the field
	Dim cFields() As Act.Framework.Contacts.ContactFieldDescriptor = ActFwk.Contacts.GetContactFieldDescriptors
	For Each field As Act.Framework.Contacts.ContactFieldDescriptor In cFields
		If field.DisplayName = "myField" Then

			ActFwk.Database.LockDatabase(Act.Framework.DatabaseLockReason.SchemaChanges)
			ActFwk.Fields.Delete(field)
			ActFwk.Database.UnlockDatabase()
			MsgBox("Field deleted")

		End If
	Next

End Sub

 

But I am getting an error message:

 

Error: Value of type 'Act.Framework.Contacts.ContactFieldDescriptor' cannot be converted to 'Act.Framework.Database.FieldDescriptor'.

 

 

 

Employee
Posts: 1,163
Country: USA

Re: Best way to save data to contact?

I'm pretty sure you just need to get the same field as a FieldDescriptor object rather than a ContactFieldDescriptor.

Matthew Wood
Act! SDK Support
Community Moderator
Copper Contributor
Posts: 12
Country: USA

Re: Best way to save data to contact?

OK, here are the subs (in VB) that I came up with.  They respond to buttons and text fields in an underlying control.

 

They:

 

1) Check to see if the custom field has been created

2) Create a new custom contact field

3) Delete a custom contact field

4) Add text to the current contact in that custom contact field

5) Show the contents of that custom contact field in the current contact

 

Private Sub CheckForField_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckForField.Click

	Dim isFieldAvailable As Boolean = False

	' is field available?
	Dim cFields() As Act.Framework.Contacts.ContactFieldDescriptor = ActFwk.Contacts.GetContactFieldDescriptors
	For Each field As Act.Framework.Contacts.ContactFieldDescriptor In cFields
		If field.DisplayName = "MyFieldName" Then
			MsgBox("Field available: " + field.Name + " field")
			isFieldAvailable = True
		End If
	Next

	If isFieldAvailable = False Then
		MsgBox("Field not found")
	End If

End Sub

Private Sub AddField_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddField.Click

	Dim callTrunkField As FieldDescriptor

	myField = New FieldDescriptor("MyFieldName", Act.Framework.RecordType.Contact, FieldDataType.Memo)
	myField.AllowEmpty = True
	myField.Alias = "MyFieldName"

	ActFwk.Database.LockDatabase(Act.Framework.DatabaseLockReason.SchemaChanges)
	ActFwk.Fields.Save(myField)
	ActFwk.Database.UnlockDatabase()

	MsgBox("MyFieldName custom field created")

End Sub

Private Sub DelField_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DelField.Click

	Dim oFDM As FieldDescriptorManager = ActFwk.Fields
	Dim oFDColl As FieldDescriptorCollection = oFDM.GetFields(recordType:=Act.Framework.RecordType.Contact)
	For Each field As FieldDescriptor In oFDColl

		If field.Alias = "MyFieldName" Then

			ActFwk.Database.LockDatabase(Act.Framework.DatabaseLockReason.SchemaChanges)
			ActFwk.Fields.Delete(field)
			ActFwk.Database.UnlockDatabase()
			MsgBox("MyFieldName custom field deleted")

		End If

	Next

End Sub

Private Sub addTextToField_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addTextToField.Click

	Dim cContact As Contact = HostApplication.ApplicationState.CurrentContact

	Dim ctField As ContactFieldDescriptor = ActFwk.Contacts.GetContactFieldDescriptor(GetRealNameForAlias("MyFieldName"), Act.Framework.MutableEntities.FieldNameType.Real)

	ctField.SetValue(cContact, TextBox1.Text)

	cContact.Update()

End Sub

Private Sub viewText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles viewText.Click

	Dim cContact As Contact = HostApplication.ApplicationState.CurrentContact

	MsgBox(cContact.Fields(GetRealNameForAlias("MyFieldName"), Act.Framework.MutableEntities.FieldNameType.Real))

End Sub

Private Function GetRealNameForAlias(ByVal dispName As String) As String

	Dim cFields() As Act.Framework.Contacts.ContactFieldDescriptor = ActFwk.Contacts.GetContactFieldDescriptors
	For Each field As Act.Framework.Contacts.ContactFieldDescriptor In cFields
		If field.DisplayName = dispName Then
			Return field.Name
		End If
	Next
	Return Nothing

End Function