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
impalacrazy69impalacrazy69 

Test Class

I have a trigger that was updated to handle bulk case closures and it works but when i deploy to production i get errors from other test classes what have nothing to do with my trigger. So i think i need to have a test created but am having trouble to create one. Here is the trigger i have created.

trigger pullSE on Case (before update, before insert) {
Set<id> userids=new Set<id>();
Map<id,string> siteMap=new Map<id,string>();
    for (case t: trigger.new) {

        try {
            if (t.SE__c == null) {
                userids.add(t.site__c);                
            }
        }
        catch (Exception e) {
        }
    }
	if(userids!=null){
		for(site__c s:[select id,user__c from site__c where id in:userids]){
			siteMap.put(s.id,s.user__c);
			}
		}
		for(case t:trigger.new){
			t.se__c=sitemap.get(t.site__c);
		}
	}

 

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Try the following test class,

 

 @isTest(SeeAllData == true)
 public class Test_pullSE_Trigger{
     public static testMethod void test1(){
     
         user u = [select id,name from user where id = UserInfo.getUserId() limit 1];
         Site__c s = new Site__c(name = 'test', user__c = u.id);
         insert s;
         
         Case c = new Case(Origin = 'Phone', Status = 'New', SE__c = null, site__c = s.id);
         insert c;
         
         c.SE__c = s.user__c;
         update c;
         
     }
 }

 

Hope so this helps you...!

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.

impalacrazy69impalacrazy69

Thanks Kamatchi for the help, however i am getting an error message that says ( Unexpected token: 'Userinfo.getUserID' )

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Sorry i missed a colon in the checking part.

 

Try the following test class,

 

 @isTest(SeeAllData == true)
 public class Test_pullSE_Trigger{
     public static testMethod void test1(){
     
         user u = [select id,name from user where id = : UserInfo.getUserId() limit 1];
         Site__c s = new Site__c(name = 'test', user__c = u.id);
         insert s;
         
         Case c = new Case(Origin = 'Phone', Status = 'New', SE__c = null, site__c = s.id);
         insert c;
         
         c.SE__c = s.user__c;
         update c;
         
     }
 }

 

Hope so this helps you...!

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.