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
Mathias_KMathias_K 

Need help - testclass

How can i pass a String in a trigger at the testrun?
 
trigger tr_EventToReport on Event (after insert, after update) {    
    
    if (Trigger.isInsert){
    for (Event events : Trigger.new) {
        if (events.Type == 'SD_KeyAccount_Visit'){

        // URL auslesen für Ereignisverlinkung
        String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm()+'/'+events.Id;

        createVisitReports cvr = new createVisitReports();
            cvr.createVisitReport('0127E0000000pTqQAI', events.AccountId, events.WhoId, 
            baseUrl);

  }
        else {
            System.debug('KEIN KEYACCOUNT BESUCH');
      }   
    }
  }    
}

my testclass : 
@isTest
private class test_Create_Event {
    
    @isTest public static void createEvent(){
        // Erstellen eines Termins für Triggeraktivierung
        
        Event ev = new Event(OwnerID = '0057E000002cOCVQA2', Type = 'KeyAccount Besuch', 
                             ActivityDateTime = Date.valueOf('2017-07-12 12:30:00') , EndDateTime = Date.valueOf('2017-07-12 13:30:00'), 
                             WhoID = '0037E00000TAqP8QAL');
        insert ev;
        
        // Perform test
        Test.startTest();
        Database.DeleteResult result = Database.delete(ev, false);
        Test.stopTest();
    }
}

how can i pass the string and the method?? 
thx 4 help
Best Answer chosen by Mathias_K
Amit Chaudhary 8Amit Chaudhary 8
Hi
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

You write a test class for this the same way that you would any other:

- Set up some data for the Trigger to access (in this case it looks like Even)
- Instantiate the Trigger- by insert or update
- Execute a method/methods
- Verify the behaviour with asserts.

Please try below code in your code
@isTest
private class test_Create_Event 
{
    
    @isTest public static void createEvent(){
	
		Contact cont = new Contact();
		cont.FirstName ='Test';
		cont.LastName ='Test';
		// insert all required field here
		insert cont;
	
        Event ev = new Event(Type = 'SD_KeyAccount_Visit', 
                             ActivityDateTime = system.now(), EndDateTime =system.now(), 
                             WhoID = cont.id );
        
        Test.startTest();
		
			insert ev;
			
        Test.stopTest();
		
    }
    @isTest public static void createEvent1(){
	
		Contact cont = new Contact();
		cont.FirstName ='Test';
		cont.LastName ='Test';
		// insert all required field here
		insert cont;
	
        Event ev = new Event(Type = 'KeyAccount Besuch', 
                             ActivityDateTime = system.now(), EndDateTime =system.now(), 
                             WhoID = cont.id );
        
        Test.startTest();
		
			insert ev;
			
        Test.stopTest();
		
    }
	
}

Let us know if this will help you

All Answers

GauravGargGauravGarg
Hi Mathias,

There are few rules to write trigger:
  1. Never write code in Trigger class, always call a handler class from trigger to handle the functionality. 
  2. Create methods in helper class that can be passed in Test class. 

As I can see above, there is not methods in trigger. Where do you need to send String variable. Please follow above apporach and pass the value from Test class to method. 

Hope this helps.

Thanks,

Gaurav

Amit Chaudhary 8Amit Chaudhary 8
Hi
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

You write a test class for this the same way that you would any other:

- Set up some data for the Trigger to access (in this case it looks like Even)
- Instantiate the Trigger- by insert or update
- Execute a method/methods
- Verify the behaviour with asserts.

Please try below code in your code
@isTest
private class test_Create_Event 
{
    
    @isTest public static void createEvent(){
	
		Contact cont = new Contact();
		cont.FirstName ='Test';
		cont.LastName ='Test';
		// insert all required field here
		insert cont;
	
        Event ev = new Event(Type = 'SD_KeyAccount_Visit', 
                             ActivityDateTime = system.now(), EndDateTime =system.now(), 
                             WhoID = cont.id );
        
        Test.startTest();
		
			insert ev;
			
        Test.stopTest();
		
    }
    @isTest public static void createEvent1(){
	
		Contact cont = new Contact();
		cont.FirstName ='Test';
		cont.LastName ='Test';
		// insert all required field here
		insert cont;
	
        Event ev = new Event(Type = 'KeyAccount Besuch', 
                             ActivityDateTime = system.now(), EndDateTime =system.now(), 
                             WhoID = cont.id );
        
        Test.startTest();
		
			insert ev;
			
        Test.stopTest();
		
    }
	
}

Let us know if this will help you
This was selected as the best answer