# Transaction Scope Error in Custom Code

**URL:** <https://www.epiusers.help/t/transaction-scope-error-in-custom-code/95772>\
**Category:** Kinetic ERP\
**Tags:** baq, customization\
**Created:** [October 26, 2022, 3:26pm UTC](https://www.epiusers.help/t/transaction-scope-error-in-custom-code/95772 "2022-10-26T15:26:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgreenEA](https://www.epiusers.help/user_avatar/www.epiusers.help/dgreenea/32/22635_2.png) [@dgreenEA](https://www.epiusers.help/u/dgreenEA)\
**Post date:** [October 26, 2022, 3:26pm UTC](https://www.epiusers.help/t/transaction-scope-error-in-custom-code/95772/1 "2022-10-26T15:26:09Z")

</div>

I am working on some custom code and getting the following error:

![image](https://www.epiusers.help/uploads/default/original/3X/b/5/b5ad2813cdb5ee6827b9e3dcbb0fe921bf92efa5.png)

We have this exact code working elsewhere in our system. But, I cannot create new bpms using this same code nor can I edit the existing ones as they will start posting this same error. I also see that System.Transactions is listed in the references section by default shown in the screen shot below:

![image](https://www.epiusers.help/uploads/default/original/3X/0/6/06063b1784160678c2f2502029a62d317dc2579f.png)

Why would I be receiving this error now?

Steps to Recreate Error:

1. Load at least one row into the UD01 table
2. Create a In-Transaction Data Directive for UD01
3. Create the following in designer
4. 

![image](https://www.epiusers.help/uploads/default/original/3X/5/b/5bb50b77804eea9fdca7e39f3c920cf8597570ca.png)

1. Create the following condition
2. 

![image](https://www.epiusers.help/uploads/default/original/3X/f/4/f418fe3d3f497acf63ca39143b0a062f6b5e68cd.png)

1. Paste the following into the custom code block
2. using (var txScope = IceContext.CreateDefaultTransactionScope())  
{  
foreach(var UD01 in (from row in Db.UD01 select row))  
{  
Db.UD01.Delete(UD01);  
}  
Db.Validate();  
txScope.Complete();

}  
7. Check the syntax in the custom block and you should get the same error.  
If you save and activate this bpm it’ll error the first time it runs then refuse to operate after.

The use case for this is to allow end users to delete items out of a UD table as needed.

---

<div class="post-metadata">

**Author:** ![Joseph\_Martin](https://www.epiusers.help/user_avatar/www.epiusers.help/joseph_martin/32/16359_2.png) [@Joseph\_Martin](https://www.epiusers.help/u/Joseph_Martin)\
**Post date:** [October 26, 2022, 3:48pm UTC](https://www.epiusers.help/t/transaction-scope-error-in-custom-code/95772/2 "2022-10-26T15:48:39Z")

</div>

See this thread.

> [@Kinetic 2022.1.1 Transaction Scope](https://www.epiusers.help/t/kinetic-2022-1-1-transaction-scope/89831):
>
> Hi Team, I’ve a BPM which includes IceDataContext.CreateDefaultTransactionScope(). It was working in Kinetic 2021 but after upgrade to Kinetic 2022.1.1, it started throwing elow error: The type 'TransactionScope' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Transactions.Local' System.Transactions is already available in reference by default. [image] Regards, Zoher Ali

Also, you could just use the BO to do the delete, in a BPM it would be something like this.

```auto
using ( var service = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.UD05SvcContract>( this.Db ))
{
	service.DeleteByID(Key1Value,Key2Value,Key3Value,Key4Value,Key5Value);
}

```

---

<div class="post-metadata">

**Author:** ![dgreenEA](https://www.epiusers.help/user_avatar/www.epiusers.help/dgreenea/32/22635_2.png) [@dgreenEA](https://www.epiusers.help/u/dgreenEA)\
**Post date:** [October 28, 2022, 2:12pm UTC](https://www.epiusers.help/t/transaction-scope-error-in-custom-code/95772/3 "2022-10-28T14:12:36Z")

</div>

When trying to run the code you supplied in either a data directive or method directive I get errors for the keys:

![image](https://www.epiusers.help/uploads/default/original/3X/e/c/ecff8bef651f0a0814794ddf2c93178460637074.png)

I also tried using dsUD05Row.Key2 as context with the same result. Any idea what I can do to fix this?

As for the thread you linked me we are in the gov cloud so we do not have access to get things installed on our instance. I have reached out to cloud services to see if they can implement this fix.

Thanks for the recommendations!

---

<div class="post-metadata">

**Author:** ![NateS](https://www.epiusers.help/user_avatar/www.epiusers.help/nates/32/18519_2.png) [@NateS](https://www.epiusers.help/u/NateS)\
**Post date:** [October 28, 2022, 2:20pm UTC](https://www.epiusers.help/t/transaction-scope-error-in-custom-code/95772/4 "2022-10-28T14:20:54Z")

</div>

I run a simple delete custom code on my UDs all the time. I have been reusing this bit of code forever:

```cs
// step through each row of results
foreach (var xRow in (from ttResults_Row in queryResultDataset.Results where ttResults_Row.Calculated_MyCheck == true select ttResults_Row))
{
     using (Ice.Contracts.UD09SvcContract mySvc = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.UD09SvcContract>(Db))
       {
       mySvc.DeleteByID(xRow.UD09_Key1, xRow.UD09_Key2, xRow.UD09_Key3, xRow.UD09_Key4, xRow.UD09_Key5);
       }
}

```

---

<div class="post-metadata">

**Author:** ![Joseph\_Martin](https://www.epiusers.help/user_avatar/www.epiusers.help/joseph_martin/32/16359_2.png) [@Joseph\_Martin](https://www.epiusers.help/u/Joseph_Martin)\
**Post date:** [October 28, 2022, 2:21pm UTC](https://www.epiusers.help/t/transaction-scope-error-in-custom-code/95772/5 "2022-10-28T14:21:13Z")

</div>

The “KeyXValue” references are placeholders. You have to change those to whatever references you are using in your BPM.  
In your BPM specifically it would look something like this.

```auto
using ( var service = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.UD01SvcContract>( this.Db ))
	{
		foreach( var UD01 in ( from row in Db.UD01 select row))
		{
			service.DeleteByID(UD01.Key1,UD01.Key2,UD01.Key3,UD01.Key4,UD01.Key5);
		}
	}

```
