0

I would like to create a case and attachment in one single transaction by using the Salesforce DotNetForce client.

This is how I would do this directly in Apex:

// start transaction

Case c = new Case(...);

insert c

ContentVersion cv = new ContentVersion(...);

cdl.FirstPublishLocationId = c.Id;

insert cv;

// end transaction

By setting the field FirstPublishLocationId to the case Id Salesforce will create automatically the missing Content Document and Content Document Link records, also if one insert fails, then the entire transaction is rolled back.

QUESTION

How can I create in a single transaction a case and attachment using DotNetForce client?

I see that the Dotnet client already support composite requests:

// Create Tree

var accounts =
   Enumerable.Range(1, 6).Select(i => new AttributedObject("Account", $"acc{i}")
   {
      ["Name"] = $"UnitTest Account {i}",
      ["Contacts"] = new RecordsObject(Enumerable.Range(1, 2).Select(j =>
          new AttributedObject("Contact", $"cont{i}[{j}]")
          { ["Name"] = $"UnitTest Contact {i}-{j}" }))
   });

var accountsResult = await client.Composite.CreateTreeAsync("Account", accounts);
DNF.ThrowIfError(accountsResult);
Assert.NotEmpty(accountsResult.Results);

The field FirstPublishLocationId is a polymorphic relation field, not sure if what I am trying to do is possible.

1
1

Finally, I got a solution for my question. The composite tree would not work because the Case object is not a parent of the Content Version object.

There is a way to roll back the entire transaction if on record throws an error message, sadly is not possible to do it with the DotNetForce client but with the Salesforce API directly, for example using composite requests

POST https://instance.salesforce.com/services/data/v.xxx/composite

Sample Request:
{
    "allOrNone" : true,
    "compositeRequest" : [
        {
            "method" : "POST",
            "url" : "/services/data/v.xxx/sobjects/Case",
            "referenceId" : "caseRef",
            "body" : { "Subject" : "Best Case" }
        },{
            "method" : "POST",
            "url" : "/services/data/v.xxx/sobjects/ContectVersion",
            "body" : {
                "VersionData" : "blob data",
                "FirstPublishLocationId" : "@{caseRef.id}"
            }
        },
    ]
}

Notice I am setting a reference called caseRef and use it to access the case id.

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.