简体   繁体   English

管理.NET + Salesforce API的事务

[英]Managing Transaction for .NET + Salesforce API

I have .NET application that updates Database and Salesforce through API. 我有.NET应用程序,可通过API更新数据库和Salesforce。 I need to update multiple Salesforce Objects. 我需要更新多个Salesforce对象。

Code look like this 代码看起来像这样

public class Service ....

public void myTransaction(..){
DBConnection dbcon;
SFConnection sfcon;

// DB Begin Transaction

//Update Database tables 
//Update SF Objects 

// DB End Transaction

}

Please help me to get better idea on how to achieve transaction. 请帮助我更好地了解如何实现交易。

I know this is old, but for reference to those who surf on in: 我知道这是旧的,但参考那些冲浪的人:

Like LaceySnr hinted, transactions are not properly available (as of 2016) through the API. 就像LaceySnr提示的那样,无法通过API正确进行事务处理(截至2016年)。 There are, however, two ways to achieve transaction-like results via the API. 但是,有两种方法可以通过API实现类似事务的结果。

  1. by creating a custom REST endpoint in Apex, or 通过在Apex中创建自定义REST端点,或
  2. by using the AllOrNone header with the API. 通过使用带有API的AllOrNone标头。

REST Endpoint REST端点

To create a custom REST endpoint you would create an apex class defined with @RestResource. 要创建自定义REST端点,您将创建使用@RestResource定义的顶点类。 Within that class you'd define a function decorated with @HttpPost that takes a list of sObjects. 在该类中,您将定义一个用@HttpPost装饰的函数,该函数带有sObjects列表。 Within the function you would set a savepoint. 在函数中,您将设置一个保存点。

When this new endpoint is called, if there a catchable error you can rollback on your own. 调用此新端点时,如果存在可捕获错误,则可以自行回滚。 Otherwise, Salesforce, upon successfully completing the function, will auto-commit. 否则,Salesforce会在成功完成该功能后自动提交。 In other words, you don't have to explicitly commit the transaction. 换句话说,您不必显式提交事务。 See this documentation for creating REST endpoints . 请参阅此文档以创建REST端点 and developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_transaction_control.htm for transaction control (I can't post more than 2 links). 和developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_transaction_control.htm用于事务控制(我不能发布两个以上的链接)。

example apex: 示例顶点:

    @RestResource(urlMapping='/myendpoint/*')
    global with sharing bool class MyFirstRESTController {

       @HttpPost
       global static boolean doSomething(List<sObject> objectsToUpdate) {
           boolean error = false;
           Savepoint sp = Database.setSavepoint();

           //do something

           //oh no! there was an error
           if( error ) {
                Database.rollback(sp);
           }
           return error;
       }
    }

with that example, if you posted to /services/apexrest/myendpoint/doSomething/ with all of your sObjects it should call that function and give you some transactional control (eg convert the objects to JSON and post them). 在该示例中,如果您将所有sObjects发布到/ services / apexrest / myendpoint / doSomething /,则应调用该函数并提供一些事务控制(例如,将对象转换为JSON并发布)。 You'll need to use the "Authorization: Bearer sessionId " header on your call to the custom REST endpoint. 您需要在对自定义REST端点的调用上使用“ Authorization:Bearer sessionId ”标头。 The sessionId can be the same sessionId retrieved from a SOAP login (in case you use the wdsl to generate a service). sessionId可以与从SOAP登录名检索到的sessionId相同(以防您使用wdsl生成服务)。

AllOrNone Header AllOrNone Header

For the AllOrNone header see the documentation for the SOAP API . 有关AllOrNone标头,请参阅SOAP API文档 This header allows you to indicate that if any single record fails at updating, then they all fail. 此标头允许您指示如果任何单个记录在更新时失败,则它们都会失败。 Essentially, if one record fails, the entire transaction is rolled back. 实质上,如果一条记录失败,则回滚整个事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM