Saving an Attachment to Saleslogix
Nicolas Galler | June 4, 2008This is an example of how much simpler, cleaner and more powerful the code in the new client is. Here is a method that will take an arbitrary entity (well, arbitrary to a point - it has to be one of the "TALCO" entities) and write an attachment under it.
/// <summary> /// Create a new attachment for the designated file. /// </summary> /// <param name="entityType">(Case sensitive) entity type that the attachment is going to be related to (eg Account, Opportunity)</param> /// <param name="entityId">PK</param> /// <param name="path">Absolute path to file to be attached. /// It will be copied to the attachment path.</param> public object SaveAttachment(string entityType, object entityId, string path, string description) { Type entityTypeActual = Type.GetType("Sage.Entity.Interfaces.I" + entityType + ",Sage.Entity.Interfaces"); if (entityTypeActual == null) throw new InvalidOperationException("Invalid entity type " + entityType); object parentEntity = EntityFactory.GetById(entityTypeActual, entityId); if(parentEntity == null) throw new InvalidOperationException("Invalid entity id or type: " + entityType + "/" + entityId); IAttachment attachment = EntityFactory.Create<IAttachment>(); switch (entityType) { case "Account": attachment.AccountId = (String)((IAccount)parentEntity).Id; break; case "Opportunity": attachment.AccountId = (String)((IOpportunity)parentEntity).Account.Id; attachment.OpportunityId = (String)((IOpportunity)parentEntity).Id; break; case "Contact": attachment.AccountId = (String)((IContact)parentEntity).Account.Id; attachment.ContactId = (String)((IContact)parentEntity).Id; break; case "Ticket": attachment.AccountId = (String)((ITicket)parentEntity).Account.Id; attachment.ContactId = (String)((ITicket)parentEntity).Contact.Id; attachment.TicketId = (String)((ITicket)parentEntity).Id; break; case "Lead": attachment.LeadId = (String)((ILead)parentEntity).Id; break; default: throw new InvalidOperationException("Unsupported entity type " + entityType); } attachment.AttachDate = DateTime.Now; attachment.Description = description ?? System.IO.Path.GetFileName(path); // save the attachment so that the Id property is populated attachment.Save(); // copy the path to the attachment folder and save the attachment record // this will also populate the user attachment.UpdateFileAttachment(path); return attachment.Id; }
And this is a unit test for it:
/// <summary> ///A test for SaveAttachment ///</summary> [TestMethod()] public void SaveAttachmentTest1() { AttachmentWriter target = new AttachmentWriter(); using (ISession sess = new SessionScopeWrapper()) { IAccount acc = sess.CreateQuery("from Sage.SalesLogix.Entities.Account order by newid()").List<IAccount>()[0]; string entityId = (string)acc.Id; string entityType = "Account"; string path = "dynamicmethods.xml"; string description = "dynamicmethods"; object attachId = target.SaveAttachment(entityType, entityId, path, description); IAttachment attach = EntityFactory.GetById<IAttachment>(attachId); Assert.IsNotNull(attach); try { String attachPath = Path.Combine(Sage.SalesLogix.Attachment.Rules.GetAttachmentPath(), attach.FileName); Assert.IsTrue(File.Exists(attachPath), "File was not copied: " + attach.FileName); File.Delete(attachPath); } finally { attach.Delete(); } } }









