function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pawan AglPawan Agl 

How to add attachment in case entity

Hi Dev

i am using sforce version 3.0 and getting problems to add an attachment file with case entity thru API only. so if u have any samples please let me know.

i badly need solution for that.

regards

pawan

 

DevAngelDevAngel

Hi John B,
For your coding pleasure:



private void btnAttachment_Click(object sender, System.EventArgs e)
{
   sforce.SforceService binding =
new sforce.SforceService();
   sforce.LoginResult lr = binding.login("user@domain.com", "password");
   binding.SessionHeaderValue =
new sforce.SessionHeader();
   binding.SessionHeaderValue.sessionId = lr.sessionId;
   binding.Url = lr.serverUrl;

   //ofd is a OpenFile dialog control
   ofd.ShowDialog();

   string fname = ofd.FileName;
   System.IO.FileInfo fi =
new System.IO.FileInfo(fname);
   sforce.Attachment attach =
new sforce.Attachment();
   attach.Name = fi.Name;
   System.IO.FileStream fs = fi.OpenRead();
   byte[] buf = new byte[fi.Length];
   fs.Read(buf, 0, (
int)fi.Length);
   attach.Body = buf;
   attach.ParentId = "contactid";

   sforce.SaveResult[] sr = binding.create(
new sforce.sObject[] {attach});

   for (int i=0;i{
       if (sr[i].success)
       {
          System.Diagnostics.Trace.WriteLine("Attachment added.");
       }
       else
       {
          System.Diagnostics.Trace.WriteLine("Error adding attachment: " + sr[i].errors[0].message);
       }
   }
}


Pawan AglPawan Agl

Hi Deve

Thank you very much for that