Skip to Content
0

Service Layer Extensions - Transaction between companys

May 11, 2017 at 04:43 PM

137

avatar image
Former Member

Hi,

I trying to implement transactions beetween companys on Service Layer, i create a extension for service layer to test this funcionality.

This is the extension:

var ServiceLayerContext = require('ServiceLayerContext.js');
var BusinessPartner = require('EntityType/BusinessPartner.js');
var http = require('HttpModule.js');


function POST() {
    var slContext = new ServiceLayerContext();
    var ToDo = http.request.getJsonObj();
    var ret = {};


     switch (ToDo.Method) {
        case "Transactions":
            ret = AnalyseTransactions(slContext, ToDo);
            break;
        case "BusinessPartners":
            ret = BusinessPartnersPost(slContext, ToDo);
            break;
        default:
            ret.status = http.HttpStatus.HTTP_BAD_REQUEST;
            ret.message = "Not Implemented";
    }


    http.response.send(ret.status, ret.message);
}
 
function AnalyseTransactions(slContext, obj) {
    var ret = {};
    switch (obj.Operation) {
        case "InTransaction":
            ret.status = http.HttpStatus.HTTP_OK;
            ret.message = slContext.isInTransaction();
            break;
        case "StartTransaction":
            slContext.startTransaction();
            ret.status = http.HttpStatus.HTTP_OK;
            ret.message = "Started Transaction" + slContext.isInTransaction();
            break;
        case "CommitTransaction":
            slContext.commitTransaction();
            ret.status = http.HttpStatus.HTTP_OK;
            ret.message = "Committed Transaction" + slContext.isInTransaction();
            break;
        case "RollbackTransaction":
            slContext.rollbackTransaction();
            ret.status = http.HttpStatus.HTTP_OK;
            ret.message = "RolledBack Transaction" + slContext.isInTransaction();
            break;
        default:
            ret.status = http.HttpStatus.HTTP_BAD_REQUEST;
            ret.message = "Not Implemented";
    }
    return ret;
}


function BusinessPartnersPost(slContext, obj) {
    var ret = {};


    switch (obj.Operation) {
        case "Add":
              
			var res = slContext.BusinessPartners.add(obj.Data);
			if (!res.isOK()) {
				slContext.rollbackTransaction();
				throw http.ScriptException(http.HttpStatus.HTTP_BAD_REQUEST, "Erro:" + res.getErrMsg());
			}
	  
            ret.status = http.HttpStatus.HTTP_OK;
            ret.message = "OK";
            break;
        case "Rollback":
			//Test if rollback works
			
            slContext.startTransaction(); 
			var res = slContext.BusinessPartners.add(obj.Data);
			if (!res.isOK()) {
				slContext.rollbackTransaction();
				throw http.ScriptException(http.HttpStatus.HTTP_BAD_REQUEST, "Erro:" + res.getErrMsg());
			} 
			
            slContext.rollbackTransaction();
            ret.status = http.HttpStatus.HTTP_OK;
            ret.message = "OK";
            break;
        case "Commit":
			//test if commit works
			
            slContext.startTransaction();
			var res = slContext.BusinessPartners.add(obj.Data);
			if (!res.isOK()) {
				slContext.rollbackTransaction();
				throw http.ScriptException(http.HttpStatus.HTTP_BAD_REQUEST, "Erro:" + res.getErrMsg());
			} 
			
            slContext.commitTransaction();
            ret.status = http.HttpStatus.HTTP_OK;
            ret.message = "OK";
            break;
        default:
            ret.status = http.HttpStatus.HTTP_BAD_REQUEST;
            ret.message = "Not Implemented";
    }
    return ret;


}

with this sample i am can add a BP.

The test i am doing is:

1. Call Service Layer on Multiple companys to start transaction.

2. Add the BP on all companys

3. If sucess i commit the transactions, if error rollback all transactions.

But when i start transactions and call service layer again, the transaction is not active.

So my question is, this is possible using the Transaction API? in my tests this fail.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

0 Answers