I recently helped a client who was looking for a solution that would enable him to find specific attachments quickly based on their intended use. For example, an Account might have several attachments associated with it, but the user may want to know which of those various attachments is being used for which specific purposes. A "specific purpose" might be something like a contract for the account, or a support agreement.

In the code example below, it is assumed that a contract document needs to be tracked for every Account. So there is a custom field called ContractAttachmentId that stores the identifier of the Attachment containing the agreement for the Account being currently viewed. The code populates a drop down list with all the attachments associated with that Account. A user can then choose one of those attachments and save the record so he doesn’t have to scan through the entire list to find the contract.

This code should be set up on the Load event so as to populate the dropdown as soon as the screen loads. The same concept can be extended if other entities such as Opportunities, Contacts etc. have to be linked to specific attachments.

  1. string currentAttachId;
  2. Sage.Entity.Interfaces.IAccount acct = this.BindingSource.Current as Sage.Entity.Interfaces.IAccount;
  5. //Get a list of attachments for the current Account
  6. Sage.Platform.Repository.IRepository<Sage.Entity.Interfaces.IAttachment> repAttach = Sage.Platform.EntityFactory.GetRepository<Sage.Entity.Interfaces.IAttachment>();
  7. Sage.Platform.Repository.IQueryable qryAttach = (Sage.Platform.Repository.IQueryable)repAttach;
  8. Sage.Platform.Repository.IExpressionFactory efAttach = qryAttach.GetExpressionFactory();
  9. Sage.Platform.Repository.ICriteria crAttach = qryAttach.CreateCriteria();
  10. crAttach.Add(efAttach.Eq("AccountId", acct.Id.ToString()));
  11. System.Collections.Generic.IList<Sage.Entity.Interfaces.IAttachment> listAttach = crAttach.List<Sage.Entity.Interfaces.IAttachment>();
  14. if(acct.ContractAttachmentId!=null){
  15. currentAttachId=acct.ContractAttachmentId.ToString();
  16. }else
  18. {
  19. currentAttachId="";
  20. }
  22. AttachmentSelection.Items.Clear();
  23. ListItem blankItem = new ListItem("","No Selection");
  24. AttachmentSelection.Items.Add(blankItem);
  25. foreach(Sage.Entity.Interfaces.IAttachment attachItem in listAttach)
  26. {
  27. string attachDesc;
  28. ListItem l = new ListItem();
  30. //Truncate the attachment a little so the drop down doesnt look huge
  31. if(attachItem.Description.Length>30){
  32. attachDesc = attachItem.Description.Substring(0,30).ToString() + "...";
  33. }
  34. else{
  35. attachDesc = attachItem.Description;
  36. }
  37. l.Text=attachDesc.ToString();
  38. l.Value=attachItem.Id.ToString();
  39. l.Selected = l.Value.ToString().Equals(currentAttachId);
  40. AttachmentSelection.Items.Add(l);
  41. }
Posted in:

Start a Project With Us

Submit your email below to get in touch with our team.