Pages

Wednesday, August 31, 2011

CandyWrapper: Attaching Files Programmatically

A recent question on the SugarCRM Forums served as a reminder of a specific programming need whose steps are not immediately obvious. Before we go any further, let us discuss the scenario so we have a clear understanding of our intended goal.


In our example, we will assume we need to attach files to a Lead record using a custom application that we have built. We will focus on the part of the code that would be responsible for attaching the file, using C# and the CandyWrapper .NET wrapper for the SugarCRM SOAP API.


Alright, now we need to review the process involved in completing this task. Here is a step-by-step summary of what our code needs to do:


1. Create a Note
2. Associate the Note entry with the desired Lead
3. Attach the file to the Note entry


Pretty straight forward. Let us take a look at the code snippet that will take care of it for us.


For this snippet, bear in mind that oCW represents an instance of the CandyWrapper object. Using CandyWrapper is not a must, but the general logic provided in the above summary is important.


string[] saFields = new string[] { "id", "name" };
string[] saValues = new string[] { string.Empty, "Test Document"};
string sFilePath = "C:\\Docs";
string sFileName = "Test1.doc";  
string sNoteID = oCW.doSetEntry(sSession, "Notes", saFields, saValues);

oCW.doRelateNoteToModule(sSession, sNoteID, "Leads", sLeadID);
oCW.doSetNoteAttachment(sSession, sFilePath, sFileName, sNoteID);

So what exactly does the above code do?


In a nutshell, we are defining certain required parameters, beginning with the fields in the Notes record that are to be updated upon creation of the record. For our example, we are creating a new entry with the value of "Test Document" in the name field.


Next we define the path containing the file to be attached and lastly, we provide the name of the file that is to be attached, C:\Docs and Test1.doc respectively.


The lines that follow simply add the Note record, associate the newly created note to the desired Lead entry (identified via its ID stored in the sLeadID variable) and lastly, attach the file to the note entry -- in that order.


It is worth noting that the SugarCRM SOAP API will automatically upload the file and it is not necessary for us to write code to handle that part.


That is it!

1 comment:

  1. HI Angel,
    I was looking to your project CandyWrapper and I don't know if you can help me to have data from contact form on Umbraco to be post as lead on SugarCrm ! i also post the problem on https://community.sugarcrm.com/sugarcrm/topics/contact_ascx_cs_umbraco_to_sugarcrm_using_soup

    thanks in advance.

    ReplyDelete

Your comments, feedback and suggestions are welcome, but please refrain from using offensive language and/or berating others. Thank you in advance.