Reputation: 537
We need to add an attachment for new contact. We are using APEX class for adding new contact. We are able create new contact. we need to maintain the Order information for a contact. This is not possible with the available fields/custom fields. So we are going to try with attachment. a customer may have multiple orders. Can you please let me know how to add attachment for a contact using c#.
Please find the below code snippet:
Contact newContact = new Contact();
newContact.LastName = downloadInformation.Name;
newContact.Email = downloadInformation.Email;
try
{
SforceService salesForce = new SforceService();
MySFServiceService mySFServive = new MySFServiceService();
mySFServive.SessionHeaderValue = new SForce.MyService.SessionHeader();
LoginResult loginResult = salesForce.login("id", "password");
mySFServive.SessionHeaderValue.sessionId = loginResult.sessionId;
// UserRegistration is a method defined in our apex class.
// parametter 1: contact object parameter
// 2: account name
mySFServive.UserRegistration(newContact, "Test Account");
}
catch (Exception ex)
{
}
Upvotes: 2
Views: 2973
Reputation: 19040
Import the enterprise WSDL into your app (which it looks like you already have), then you'd create an instance of the attachment object,set its body to the order blob, and set the parentId to be the id of the contact. So you'd need to update your custom UserRegistration call to return the created contactId, then you could do.
salesforce.SessionHeaderValue = new SforceService.SessionHeader();
salesforce.SessionHeaderValue.sessionId = loginResult.sessionId;
salesforce.Url = loginResult.serverUrl;
...
String contactId = mySFervive.UserRegistration(....);
Attachment a = new Attachment();
a.Body = readFileIntoByteArray(someFile);
a.parentId = contactId;
a.Name = "Order #1";
SaveResult sr = salesforce.create(new SObject [] {a})[0];
if (sr.success){
// sr.id contains id of newly created attachment
} else {
//sr.errors[0] contains reason why attachment couldn't be created.
}
Upvotes: 3