1

Is there a way to create a file using the .NET toolkit ?

I can't find it when configuring my data services in visual studio 2015.

I found Attachment and Document but I heard Attachment will be deprecated soon and Document can't be directly linked to an opportunity.

Below is the code I use today :

public bool AttachProjectDocuments(string siren, string description, IEnumerable<DocumentWithDataDto> projectDocuments)
    {
        var account = Query<Account>("SELECT ID FROM Account WHERE SIREN__c = " + siren);
        if (account != null && account.Records.Any())
        {
            var opportunity = Query<Opportunity>("SELECT ID, Name, CreatedDate, eligibility_rating__c, loan_duration_request__c, loan_reason__c, Loan_amount_request__c FROM Opportunity WHERE AccountId = '" + account.Records[0].Id + "'");
            if (opportunity != null && opportunity.Records.Any())
            {
                var lastOpportunity = opportunity.Records.OrderByDescending(x => x.CreatedDate).First();

                Update("Opportunity", lastOpportunity.Id,
                       new Opportunity
                       {
                           Id = lastOpportunity.Id,
                           AccountId = account.Records[0].Id,
                           Loan_amount_request__c = lastOpportunity.Loan_amount_request__c,
                           loan_duration_request__c = lastOpportunity.loan_duration_request__c,
                           loan_reason__c = lastOpportunity.loan_reason__c,
                           eligibility_rating__c = lastOpportunity.eligibility_rating__c,
                           LeadSource = lastOpportunity.LeadSource,
                           Loan_request_details__c = description,
                           CloseDate = DateTime.UtcNow,
                           Request_submission__c = DateTime.UtcNow,
                           StageName = "Needs Analysis",
                           Name = lastOpportunity.Name,
                           Year_N_accounts__c = true,
                           Year_N_1_accounts__c = true,
                           Year_N_2_accounts__c = true
                       });
                foreach (var doc in projectDocuments)
                {
                    var attachment = new Attachment
                                     {
                                         ParentId = lastOpportunity.Id,
                                         BodyLength = doc.Data.Length,
                                         Name = doc.Name,
                                         ContentType = doc.MimeType,
                                         Body = doc.Data
                                     };
                    Create("Attachment", attachment);
                }
                return true;
            }
        }
        return false;
    }
1

So the rumor you heard would probably be Lightning Experience: Attachments are being replaced by Files and not returned in search:

This is expected behavior and due to Attachments being deprecated in Lightning Experience. Attachments are being superseded and replaced in favor of the more robust Files feature. For more details on Files see Files Overview.

As best as I can tell, Files are exposed via Chatter REST API resources. , The example Id given in docs for a file is 069D00000001FHF. The 069 keyprefix is for ContentDocument, so that means you could probably use the Content Objects data model to find the correct objects to work with.

Give that a try. There are generic instructions for working with ContentDocument and ContentVersion in Insert or Update Blob Data.

See also: Differences Between Files, Salesforce CRM Content, Salesforce Knowledge, Documents, and Attachments

  • Thanks for your help, it helped me to find a solution with the .net toolkit. I updated my question with the next problem. – PMerlet May 25 '16 at 16:56
  • @Cubi General best practice on StackExchange sites is to ask a new question for subsequent problems. At a guess, is the Mime Type for the PDF correct? Do the number of bytes in Salesforce match the file size before upload? – Daniel Ballinger May 25 '16 at 19:46
0

I finally got it working using ContentDocument, DocumentVersion and ContentDocumentLink.

Below is the method I ended up with to create a file and attach it to an opportunity :

private void CreateDocument(string opportunityId, DocumentWithDataDto document)
{
    var docName = string.Format("{0} - {1}", opportunityId, document.Name);

    var version = new ContentVersion
                  {
                      Title = docName,
                      VersionData = document.Data,
                      FileType = document.MimeType,
                      PathOnClient = "/" + docName
                  };
   var versionId = Create("ContentVersion", version);

   var contentQuery = Query<ContentDocument>("SELECT Id FROM ContentDocument WHERE Title = '" + docName + "'");
   if (versionId != null && contentQuery != null && contentQuery.Records.Any())
   {
       var docId = contentQuery.Records[0].Id;

       var link = new ContentDocumentLink
       {
           ContentDocumentId = docId,
           LinkedEntityId = opportunityId,
           ShareType = "I"
       };
       Create("ContentDocumentLink", link);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.