1

I am using the SOAP API (in C#) to create new Cases, which will have several Tasks attached. The intent is to be able to view these Tasks under the "Activities > Next Steps" area of the Case view on Salesforce Lightning. Currently we are doing this by uploading the Cases first then retrieving their Id and setting it in the Tasks (through the What field). However, I would like to reduce this to a single API call by uploading them both at once, as described here under "Creating Parent and Child Records in a Single Call Using Foreign Keys."

Unfortunately, the method described there seems to only apply to objects related through a custom relationship created using an External ID. I need to relate my Tasks to my Cases through the WhatId field of the Task sObject.

Is there any way to accomplish what I am attempting to do, or is the old two-call approach my only option?

  • Can you clarify what you mean by "upload them both at once"? – Ayoola Olapo Jun 1 '19 at 2:16
  • Of course, sorry if that wasn't clear. I am creating the objects (not updating) using the .create() call on the client generated from the enterprise WSDL, as in the link in my question. – webMan1 Jun 1 '19 at 3:01
0

The External ID in this scenario doesn't define the field that represents the relationship, but rather how to identify the parent record when the foreign key isn't known yet.

The example they give in Creating Parent and Child Records in a Single Call Using Foreign Keys runs through this scenario.

Note how both the parentAccount and the newOpportunity are both created in the same API call. And then the relationship from newOpportunity to parentAccount is via the newOpportunity.Account field. That relationship field only has the external ID field set.

You could do exactly the same think with the Task.WhatId field if you add an external ID field to the parent Cases.

public void createForeignKeySample()
{
   try
   {
      Opportunity newOpportunity = new Opportunity();
      newOpportunity.Name = "OpportunityWithAccountInsert";
      newOpportunity.StageName = "Prospecting";
      DateTime dt = (DateTime)binding.getServerTimestamp().timestamp;
      newOpportunity.CloseDate = dt.AddDays(7);
      newOpportunity.CloseDateSpecified = true;

      // Create the parent reference.
      // Used only for foreign key reference
      //  and doesn't contain any other fields.
      Account accountReference = new Account();
      accountReference.MyExtID__c = "SAP111111";
      newOpportunity.Account = accountReference;

      // Create the Account object to insert.
      // Same as above but has Name field.
      // Used for the create call.
      Account parentAccount = new Account();
      parentAccount.Name = "Hallie";
      parentAccount.MyExtID__c = "SAP111111";

      // Create the account and the opportunity.
      SaveResult[] results = binding.create(new sObject[] {
      parentAccount, newOpportunity });

      // Check results.
      for (int i = 0; i < results.Length; i++)
      {
         if (results[i].success)
         {
            Console.WriteLine("Successfully created ID: "
                  + results[i].id);
         }
         else
         {
            Console.WriteLine("Error: could not create sobject "
                  + "for array element " + i + ".");
            Console.WriteLine("   The error reported was: "
                  + results[i].errors[0].message + "\n");
         }
      }
   }
   catch (SoapException e)
   {
      Console.WriteLine("An unexpected error has occurred: " +
                              e.Message + "\n" + e.StackTrace);
   }
}

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.