12-06-2011 09:28 AM
Hi,
I would like to be able to attach an Excel file to the Documents tab of a contact. I have the plugin interface working, but can't figure out the ACT object model to accomplish the subject task.
Thank you for your assistance,
John
12-06-2011 11:21 AM - edited 12-06-2011 02:03 PM
Hello collabex,
Below is a bit of code that creates a history item and attaches it to an opportunity. Attachments on the document tabs are history items of type Library Document, I think that's the biggest bit of confusion in regards to attachments.
OpportunityList ol = ActApp.ApplicationState.CurrentOpportunities; Opportunity o = ol[0]; History h = ActApp.ActFramework.Histories.CreateHistory(); string filePath = @"D:\Images\Sage_Logo.gif"; Act.Framework.SupplementalFiles.Attachment att = ActApp.ActFramework.SupplementalFileManager.CreateAttachment(AttachmentMate.History, filePath, "This File", false); HistoryType ht = ActApp.ActFramework.Histories.GetHistoryType("Library Document"); h.OpportunityList = ol; h.CreateUserID = ActApp.ActFramework.Contacts.GetMyRecord().ID; h.HistoryType = ht; h.Regarding = "Test"; h.Details = ""; h.StartTime = System.DateTime.Now; h.EndTime = System.DateTime.Now.AddDays(2); h.ManageUserID = ActApp.ActFramework.Contacts.GetMyRecord().ID; h.Update();
12-06-2011 01:44 PM
Hi Matthew,
Thank you for your quick response.
Can you briefly explain what each of the lines of code is doing? You create an ht object, but then never reference it - why? I see the CList object is created referencing the CurrentContact, but again it is never referenced again - why?
Thanks,
John
12-06-2011 02:02 PM - edited 12-06-2011 02:04 PM
I didn't really look through it much before posting it and for that I apologize. My code often has remanents left over from testing similar issues (or very disimilar in this case). I read the name of my method, ctrl+c, ctrl+v...
I'm sorry if this has caused you any confusion, I've updated my post above with something that does create a document for an opportunity, modifying it to work with contacts is pretty straightforward, but if you have any issues with it then please feel free to post them here.
12-14-2011 06:07 PM
OK,
I got the code working to add a document shortcut to the current contact, which is below:
// Private Variables private ActApplication _Application; private ActFramework _ActFramework; private ActApplicationState _ActApplicationState; private void CreateDocumentShortcutForCurrentContact(string filePath, string displayName) { try { Attachment MyAttachment = _ActFramework.SupplementalFileManager.CreateShortcutAttachment(AttachmentMate.History, filePath, displayName, false); HistoryType MyLibraryDocument = _ActFramework.Histories.GetHistoryType("Library Document"); Contact MyRecord = _ActFramework.Contacts.GetMyRecord(); History MyHistory = _ActFramework.Histories.CreateHistory(_CurrentContact, MyRecord.ID, MyLibraryDocument, false, DateTime.Now, DateTime.Now, "", "", MyAttachment); } catch (Exception Ex) { ExceptionHandler(Ex); } }
Any comments on my code are welcome.
Now I'm trying to figure out how to see if a shortcut to a file already exists to avoid duplicates. How do I get this informatiion out of the current contact?
Thank you for the help,
John
12-15-2011 06:02 AM
Hi John,
There are a couple of ways to go abouts doing this, but both involve using the overloaded HistoryManager.GetHistories().
1. Get a HistoryList for the Contact and itterate through each history using an if statement on the HistoryType for each History object
2. Create a InFilter object to get only the Library history items for that Contact.
Approach 2 would seem the most logical way forward but doing IFilterCriteria is really tedious in the SDK!
ACT! SDK rule of thumb - always look at the entity manager class to see if there is a general method for what you are trying to do.
HTH
P.S. if you use the .GetHistories(HistorySortCriteria[], Contact) overload you can set the sortcriteria object to null so you just pass in null and the myContact object.
12-15-2011 09:20 AM
Hi,
I've written a little method to find out whether or not a shortcut already exists on a contact or not, which is below:
private bool DocumentShortcutExistsForContact(string filePath, Contact contact) { bool Value = false; try { HistoryList MyHistoryList = _ActFramework.Histories.GetDocuments(contact.ID); int HistoryCount = MyHistoryList.Count; if (HistoryCount > 0) { HistoryType MyLibraryDocument = _ActFramework.Histories.GetHistoryType("Library Document"); for (int i = 0; i < HistoryCount; i++) { History MyHistory = MyHistoryList[i]; if (MyHistory.HistoryType == MyLibraryDocument) { if (MyHistoryList[i].AttachmentFileName == filePath) { Value = true; break; } } } } } catch (Exception Ex) { ExceptionHandler(Ex); } return Value; }
When it hits this line:
History MyHistory = MyHistoryList[i];
I get the exception below:
System.ArgumentException was caught
Message=Parameter USERID failed set its value to type String.
Source=Act.Data
StackTrace:
at Act.Data.Parameter.set_Value(Object value)
at Act.Framework.Histories.HistoryManagerDB.get_DataFetchCommand()
at Act.Framework.ComponentModel.BaseManagerDB.GetDataRange(Guid[] keys)
at Act.Shared.Collections.DataList`1.FetchDataRange(Guid[] newKeys)
at Act.Shared.Collections.DataList`1.GetData(Guid key, Int32 index, Boolean isEnumeration)
at Act.Shared.Collections.DataList`1.System.Collections.Generic.IList<T>.get_Item(Int32 index)
at Act.Shared.Collections.DataList`1.System.Collections.IList.get_Item(Int32 index)
at Act.Framework.Histories.HistoryList.get_Item(Int32 index)
at CopingSystems.SF2ACT.CurrentContactPlugin.DocumentShortcutExistsForContact(String filePath, Contact contact) in C:\CopingS\Development\SF2ACT\CurrentContactPlugin.cs:line 359
InnerException:
Any suggestions on why this is happening?
Thanks,
John
12-15-2011 10:27 AM
I think there is a bug in that method. If you substitute this line:
HistoryList MyHistoryList = _ActFramework.Histories.GetHistories(null,contact);
for this line:
HistoryList MyHistoryList = _ActFramework.Histories.GetDocuments(contact.ID);
your code should work.
Stan
12-15-2011 10:32 AM
Actually you're going to have to use the AttachmentDisplayName instead of the AttachmentFilename or you will have to resolve the lnk file that is in the AttachmentFilename.
Stan
12-15-2011 11:16 AM
I'm not sure there's a bug here, I tested this in 14.1 SP1 with this:
private void GetDocuments() { Contact c = ActApp.ApplicationState.CurrentContact; HistoryList myHList = ActApp.ActFramework.Histories.GetDocuments(c.ID); HistoryList myAltHList = ActApp.ActFramework.Histories.GetHistories(null, c); String results = "GetDocuments: " + myHList.Count.ToString() + Environment.NewLine + "GetHistories: " + myAltHList.Count.ToString(); MessageBox.Show(results); }
Both returned the correct number of history items, but it certainly can't hurt trying Stan's method admittedly this could have been an issue with a prior release that I'm just not aware of. What version are you running this against?