function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
King KooKing Koo 

transaction control with REST API - does it exist?

Hi there

It may be a dumb question but is there any transaction control capability with REST API?

I have SAP sending data over to my Salesforce instance over some middleware, calling REST API.  I don't have any web services exposed or anything.  The middleware basically just simply pumps data from SAP into Salesforce calling REST API (/composite/batch resource).

I'm wondering what if the batch is sending me 20 records (say one account followed by 5 contacts, 5 opportunities and 9 orders).  If any of the record fails, is there a way for the REST API to rollback this set of 20 records?

Is there also a place in Salesforce that I can view the REST API log?

Thanks
King
Daniel BallingerDaniel Ballinger
You could try the Composite Resource in the REST API. See Create Multiple Records (https://developer.salesforce.com/docs/atlas.en-us.200.0.api_rest.meta/api_rest/dome_composite_sobject_tree_flat.htm). From that same page:
 
The response body will contain the IDs of the created records if the request is successful. Otherwise, the response contains only the reference ID of the record that caused the error and the error information.
Another alternative is to create your own Apex method to do the DML operations and expose it via the REST API. With the standard Apex transaction control it will roll back for you. Or you can use the explicity Database methods to handle errors.

You can view the REST API log using the Event Monitoring API (https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/using_resources_event_log_files.htm). REST API calls will ofter show up in the Debug log as well if there is an active Trace Flag.
King KooKing Koo
Hi  Daniel

Thanks for your reply.

How can I combine these two concept though?  Let's say using the example I have above, I have 20 recrds, for the one accout, but I have over 2000 accounts.  (That's why I was using /composite/batch.  I don't think it would make sense to use /composite/tree/Account 2000 times, once for each account?

As for event monitoring, is that the one through Setup -> Monitor -> Logs -> Event Monitoring Setup?  That one doesn't have any frontend interface so I have to use REST API to generate log right?

I was trying to use debug log but I don't understand how.  Let's say if I'm just trying to post to account with (from workbench for example with REST)
 
{
  "name": "quicktest"
}

what exactly do I have to do to see whether the insert is successful or not through the debug log?  

I set the trace flag to FINEST for everything.  I noticed if I have some validation rules or workflows then I can see some log.  But what if the object has none of that, would I be able to see from the debug log that an attempt was made to insert a record with the above field values, and the attempt was successful, or not; and in the case of latter, that I be provided an error message?

Thanks
King
Daniel BallingerDaniel Ballinger
You should be able to look in the debug log for DML_BEGIN and DML_END events. These will tell you how many records were successfully inserted.

If there is a failure/problem you will be sent back a corresponding error.
King KooKing Koo
Hi Daniel, thanks.

I wasn't able to see any DML_BEGIN and DML_END - I did set Database to Finest.  But the only things that it went through were the validation rules, workflow rules and my duplicate rules.  I was hoping to find something like DML_BEGIN too.

Any advice?

Thanks

 
36.0 APEX_CODE,FINEST;APEX_PROFILING,FINEST;CALLOUT,FINEST;DB,FINEST;SYSTEM,FINE;VALIDATION,INFO;VISUALFORCE,FINER;WORKFLOW,FINER
06:55:40.0 (335248)|EXECUTION_STARTED
06:55:40.0 (361891)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Account:new
06:55:40.0 (375283)|VALIDATION_RULE|03d3B000000CeGE|Inactive_Date_Required_If_Inactive
06:55:40.0 (534623)|VALIDATION_FORMULA|ISPICKVAL( Status__c , 'Inactive')   &&   ISBLANK( Inactive_Date__c )|Status__c=Active , Inactive_Date__c=null
06:55:40.0 (539550)|VALIDATION_PASS
06:55:40.0 (560183)|CODE_UNIT_FINISHED|Validation:Account:new
06:55:40.0 (572314)|EXECUTION_FINISHED
06:55:40.1 (1654900)|EXECUTION_STARTED
06:55:40.1 (1660284)|CODE_UNIT_STARTED|[EXTERNAL]|DuplicateDetector
06:55:40.1 (1691598)|DUPLICATE_DETECTION_BEGIN
06:55:40.1 (2632161)|DUPLICATE_DETECTION_RULE_INVOCATION|DuplicateRuleId:0Bm3B0000004CEw|DuplicateRuleName:Prevent Duplicate Account|DmlType:INSERT
06:55:40.1 (177516575)|DUPLICATE_RULE_FILTER|[Account : SAP Account ID equals ]
06:55:40.1 (177542741)|DUPLICATE_RULE_FILTER_VALUE|
06:55:40.1 (177548851)|DUPLICATE_RULE_FILTER_RESULT|true
06:55:40.1 (211988224)|DUPLICATE_DETECTION_MATCH_INVOCATION_DETAILS|EntityType:Account|ActionTaken:Allow_[Alert,Report]|DuplicateRecordIds:
06:55:40.1 (211999537)|DUPLICATE_DETECTION_MATCH_INVOCATION_SUMMARY|EntityType:Account|NumRecordsToBeSaved:1|NumRecordsToBeSavedWithDuplicates:0|NumDuplicateRecordsFound:0
06:55:40.1 (212121632)|DUPLICATE_DETECTION_END
06:55:40.1 (212174331)|CODE_UNIT_FINISHED|DuplicateDetector
06:55:40.1 (212186724)|EXECUTION_FINISHED
06:55:40.300 (300737968)|EXECUTION_STARTED
06:55:40.300 (300754842)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Account
06:55:40.300 (306634483)|WF_RULE_EVAL_BEGIN|Workflow
06:55:40.300 (306656523)|WF_CRITERIA_BEGIN|[Account: ccccccc 0013B0000069Prr]|Account Customer Type Edited|01Q3B0000000KHW|ON_ALL_CHANGES|0
06:55:40.300 (307086263)|WF_FORMULA|Formula:ENCODED:[treatNullAsNull]ISBLANK( {!ID:00N3B000000P534} ) &&  (  ISNEW() ||  ISCHANGED(  {!ID:00N3B000000PAOH}) )|Values:Customer_Type__c=null, SAP_ID__c=null
06:55:40.300 (307098370)|WF_CRITERIA_END|true
06:55:40.300 (325477680)|WF_SPOOL_ACTION_BEGIN|Workflow
06:55:40.300 (346535018)|WF_ACTION| Field Update: 1;
06:55:40.300 (346540444)|WF_RULE_EVAL_END
06:55:40.300 (346599779)|WF_ACTIONS_END| Field Update: 1;
06:55:40.300 (346606244)|CODE_UNIT_FINISHED|Workflow:Account
06:55:40.300 (346612920)|EXECUTION_FINISHED
06:55:40.370 (370904065)|EXECUTION_STARTED
06:55:40.370 (370919397)|CODE_UNIT_STARTED|[EXTERNAL]|DuplicateDetector
06:55:40.370 (370930733)|DUPLICATE_DETECTION_BEGIN
06:55:40.370 (370996934)|DUPLICATE_DETECTION_RULE_INVOCATION|DuplicateRuleId:0Bm3B0000004CEw|DuplicateRuleName:Prevent Duplicate Account|DmlType:INSERT
06:55:40.370 (403512330)|DUPLICATE_RULE_FILTER|[Account : SAP Account ID equals ]
06:55:40.370 (403536138)|DUPLICATE_RULE_FILTER_VALUE|
06:55:40.370 (403582508)|DUPLICATE_RULE_FILTER_RESULT|true
06:55:40.370 (403751456)|DUPLICATE_DETECTION_END
06:55:40.370 (403800579)|CODE_UNIT_FINISHED|DuplicateDetector
06:55:40.370 (403809817)|EXECUTION_FINISHED
06:55:40.405 (405623329)|CUMULATIVE_PROFILING_BEGIN
06:55:40.405 (405623329)|CUMULATIVE_PROFILING|No profiling information for SOQL operations
06:55:40.405 (405623329)|CUMULATIVE_PROFILING|No profiling information for SOSL operations
06:55:40.405 (405623329)|CUMULATIVE_PROFILING|No profiling information for DML operations
06:55:40.405 (405623329)|CUMULATIVE_PROFILING|No profiling information for method invocations
06:55:40.405 (405623329)|CUMULATIVE_PROFILING_END

 
King KooKing Koo
Hi Daniel

I dug a little deeper, it looks like unless I issue those DML statements like insert, update, upsert, delete, there won't be any DML_BEGIN or DML_END in the log.

These inserts are done via REST API, so no DML statements, looks like.

The integration developer is hoping to find some way for Salesforce to spit out some logs every time when he sends records over REST API.  I'm still not sure if I can use the debug log or not.....

Thanks
King