Programming, technology, and CRM – from a Belgian programmer exiled to Missouri
  • rss
  • Home
  • Soft Gallery
    • autosvnbackup.sh
    • VBScript Snippets
  • Contact Me
  • Welcome

Saving an Attachment to Saleslogix

Nicolas Galler | June 4, 2008

This 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();
        }
    }
}
Comments
No Comments »
Categories
Programming, Saleslogix
Tags
Attachment, Saleslogix
Comments rss Comments rss
Trackback Trackback

Categories

  • Experiments (4)
  • Interesting (1)
  • MSCRM (1)
  • Programming (60)
  • Rant (3)
  • Saleslogix (34)
  • Tricks (8)
  • Uncategorized (30)

Post History

  • 2010
    • January (3)
    • March (3)
    • April (2)
    • August (2)
  • 2009
    • March (2)
    • April (1)
    • May (3)
    • June (3)
    • July (1)
    • September (3)
    • October (2)
    • December (5)
  • 2008
    • January (9)
    • February (4)
    • March (9)
    • April (1)
    • May (5)
    • June (8)
    • July (1)
    • August (2)
    • September (1)
    • November (1)
    • December (3)
  • 2007
    • January (3)
    • February (7)
    • March (1)
    • April (3)
    • May (6)
    • June (2)
    • July (1)
    • August (2)
    • September (5)
    • October (3)
    • November (5)
    • December (4)
  • 2006
    • January (2)
    • September (1)
    • November (3)
    • December (4)
  • 2005
    • April (1)

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox