I have vbscript code to add an attchment to an opportunity. I'm using the same fields and functions in .net but it's not attaching.
attachId = Application.BasicFunctions.GetIDFor("ATTACHMENT")
'copy the file to the attachment folder
Dim filesys, Fs, attachDesc
Set filesys = CreateObject("Scripting.FileSystemObject")
If filesys.FileExists(Form.tbSaveTo.Text & Form.tbFileName.Text & ".pdf") Then
Set Fs = filesys.GetFile(Form.tbSaveTo.Text & Form.tbFileName.Text & ".pdf")
On Error Resume Next
Fs.Copy(Application.BasicFunctions.GetAttachmentPath & "!" & attachId & Form.tbFileName.Text & ".pdf")
If Err.Number <> 0 Then
MsgBox "Error Saving The Attachment" & vbCRLF & "Description: " & Err.Description & vbCRLF & "Source: " & Err.Source, vbOKOnly + vbCritical, "ERROR"
Err.Clear
Exit Sub
End If
'insert new record in the attachment table
size = Fs.Size
Dim objAttchRs, strSql
Set objAttchRs = slxDb.GetNewRecordSet
strSql = "Select * From Attachment Where 1=2"
objAttchRs.Open strSql, slxDb.Connection
objAttchRs.AddNew
attachDesc = "Proposal #" & Form.tbOppNumber.Text
If Trim(Form.tbVersion.Text) <> "" Then
attachDesc = attachDesc & "-" & Form.tbVersion.Text
End If
attachDesc = attachDesc & CStr(Date)
attachDesc = Replace(attachDesc, "/", "-")
objAttchRs.Fields("ATTACHID").Value = attachId
objAttchRs.Fields("ATTACHDATE").Value = Date
objAttchRs.Fields("OPPORTUNITYID").Value = Form.tbOppId.Text
objAttchRs.Fields("DESCRIPTION").Value = attachDesc
objAttchRs.Fields("DATATYPE").Value = "R"
objAttchRs.Fields("FILESIZE").Value = size
objAttchRs.Fields("FILENAME").Value = "!" & attachId & Form.tbFileName.Text & ".pdf"
objAttchRs.Fields("USERID").Value = Application.BasicFunctions.CurrentUserID
objAttchRs.Update
Set objAttchRs = Nothing
Application.BasicFunctions.UpdateFileAttachment attachId
Application.BasicFunctions.LogSendFileAttachment Application.MainViews.ActiveView.CurrentID, "!" & attachId & Form.tbFileName.Text & ".pdf"
Else
MsgBox "Unable to find the specified file to create the attachment."
end if
Here is my C# .net code
string filePathAndName = tbPath.Text;
if (File.Exists(filePathAndName))
{
FileInfo fi = new FileInfo(filePathAndName);
Hoj_SLX_DAL.Attachment.Attachment attachment = new Hoj_SLX_DAL.Attachment.Attachment();
attachment.IsDirty = true;
attachment.ToInsert = true;
attachment.Id = SlxApplication.BasicFunctions.GetIDFor("ATTACHMENT");
attachment.AccountId = string.Empty;
attachment.ActivityId = string.Empty;
attachment.AttachDate = DateTime.Now;
attachment.ContactId = string.Empty;
attachment.ContractId = string.Empty;
attachment.CreateSource = "Test ";
attachment.DataType = "R";
attachment.DefectId = string.Empty;
attachment.Description = string.Empty;
attachment.FileSize = Convert.ToInt32(fi.Length);
attachment.FileName = "!" + attachment.Id + "vpTest.pdf";
attachment.HistoryId = string.Empty;
attachment.UserId = SlxApplication.BasicFunctions.CurrentUserID();
attachment.OpportunityId = OpportunityId;
attachment.Save(SlxApplication.ConnectionString);
SlxApplication.BasicFunctions.UpdateFileAttachment(attachment.Id);
SlxApplication.BasicFunctions.LogSendFileAttachment(SlxApplication.MainViews.ActiveView.CurrentID, attachment.FileName);
}
else
{
MessageBox.Show("Cannot Find The File " + filePathAndName, "SalesLogix", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Not sure what i'm missing.