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
aditya rawataditya rawat 

Creating a Event e instance for unit testing transaction security policy classes

I am trying to find a better way to create TransactionSecurity.Event e instances.

The below is based on transaction security trailhead but it requires an empty map and entering values which are not needed at all times.
TxnSecurity.Event e = new TxnSecurity.Event(
						      orgID, /* organizationId */
						      u.Id, /* userId */
						      'Account', /* entityName */
						      'DataExport', /* action */
						      'Account', /* resourceName */
						      '', /* entityId */
		, /* timeStamp */
						      eventData ); /* data - Map with info about this event. why ? src: */

I tried creating an Event e using below line but it failed with mesage.
TxnSecurity.Event e2 = new TxnSecurity.Event();

Result: [OPERATION FAILED]: classes/Test_CL_TxService.cls: Constructor not defined: [TxnSecurity.Event].<Constructor>() (Line: 16, Column: 29)

Is there any better way to create Event e variable for unit testing purposes ?
Jakub FlisJakub Flis

Hi Rawat,

Unfortunately this won't work as with other objects or classe - TxnSecurity.Event e2=new TxnSecurity.Event()
I'm not sure why but it simply doesn't you need to use the method you have above e.g.

 Map<String, String> eventData = new Map<String, String>();
eventData.put('NumberOfRecords', '19');

//Creating Data Export event
          TxnSecurity.Event e = new TxnSecurity.Event(
         	orgId /* organizationId */,
            u.Id /* userId */,
            'Lead' /* EntityName */,
             'DataExport' /* Action */,
             'Lead' /*resourceType */,
              null /* entityId */,
              Datetime.newInstance(2017, 5, 23) /* timeStamp */,
              eventData /* data */ );

P.S. Event Data Map is quite important here as you'll be referring to it in your Security Policy.
For Example if you add data ('NumberOfRecors','19');

In the policy it is checked by this statement :

Integer numberOfRecords = Integer.valueOf('NumberOfRecords'));

I hope it helps.