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
Jon FoyJon Foy 

Need Help Writing an Apex Class for the following Trigger

trigger UpdateTimeSlip on TimeSlip__c (before insert, before update){
    Map<Id,Id> caseResourceMap = new Map<Id,Id>();
    String userId = userInfo.getUserId();
    String userName = userInfo.getName();
    Set<Id> caseIds = new Set<Id>();
   
    for(TimeSlip__c ts : trigger.new){
        caseIds.add(ts.Case__c);
    }
    for(Case_Resource__c caseResource : [select id,Case__c from Case_Resource__c where Resource_Name__c =: userId and Case__c IN: caseIds ]){
        caseResourceMap.put(caseResource.Case__c,caseResource.Id);  
    }
      
    List<Contact> contactList = [select id,Name from Contact where Name =: userName];
    System.debug('contactList '+contactList );

    for(TimeSlip__c ts : trigger.new){
        if(ts.Resource_Name__c == null){
            if(contactList[0].Id != null){
                ts.Resource_Name__c = contactList[0].Id;
            }
        }
        if(ts.Case_Resource_ID__c == null){
            if(caseResourceMap.containsKey(ts.Case__c) && caseResourceMap.get(ts.Case__c) !=null){
                ts.Case_Resource_ID__c = caseResourceMap.get(ts.Case__c);
            }
        }
    }
}
Vatsal KothariVatsal Kothari
Hello Jon,

you can try this code:

Apex Trigger:

trigger UpdateTimeSlip on TimeSlip__c (before insert, before update){
    if(Trigger.isInsert || Trigger.isUpdate){
		TimeSlip.updateResource(trigger.new);
	}
}


Apex Class:

public class TimeSlip{
	public static void updateResource(List<TimeSlip__c> tSlipList){
		Map<Id,Id> caseResourceMap = new Map<Id,Id>();
		String userId = userInfo.getUserId();
		String userName = userInfo.getName();
		Set<Id> caseIds = new Set<Id>();
	   
		for(TimeSlip__c ts : tSlipList){
			caseIds.add(ts.Case__c);
		}
		for(Case_Resource__c caseResource : [select id,Case__c from Case_Resource__c where Resource_Name__c =: userId and Case__c IN: caseIds ]){
			caseResourceMap.put(caseResource.Case__c,caseResource.Id);  
		}
		  
		List<Contact> contactList = [select id,Name from Contact where Name =: userName];
		System.debug('contactList '+contactList );

		for(TimeSlip__c ts : tSlipList){
			if(ts.Resource_Name__c == null){
				if(contactList[0].Id != null){
					ts.Resource_Name__c = contactList[0].Id;
				}
			}
			if(ts.Case_Resource_ID__c == null){
				if(caseResourceMap.containsKey(ts.Case__c) && caseResourceMap.get(ts.Case__c) !=null){
					ts.Case_Resource_ID__c = caseResourceMap.get(ts.Case__c);
				}
			}
		}
	}
}

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
Jon FoyJon Foy
Error: Compile Error: unexpected token: 'public class TimeSlip' at line 2 column 0
Vatsal KothariVatsal Kothari
trigger UpdateTimeSlip on TimeSlip__c (before insert, before update){
    if(Trigger.isInsert || Trigger.isUpdate){
		TimeSlip.updateResource(trigger.new);
	}
}
Save above doe to trigger of timeslip, which you have already created.

Now go to setup --> app setup --> develop --> apex class --> new
and paste below code:

public class TimeSlip{
	public static void updateResource(List<TimeSlip__c> tSlipList){
		Map<Id,Id> caseResourceMap = new Map<Id,Id>();
		String userId = userInfo.getUserId();
		String userName = userInfo.getName();
		Set<Id> caseIds = new Set<Id>();
	   
		for(TimeSlip__c ts : tSlipList){
			caseIds.add(ts.Case__c);
		}
		for(Case_Resource__c caseResource : [select id,Case__c from Case_Resource__c where Resource_Name__c =: userId and Case__c IN: caseIds ]){
			caseResourceMap.put(caseResource.Case__c,caseResource.Id);  
		}
		  
		List<Contact> contactList = [select id,Name from Contact where Name =: userName];
		System.debug('contactList '+contactList );

		for(TimeSlip__c ts : tSlipList){
			if(ts.Resource_Name__c == null){
				if(contactList[0].Id != null){
					ts.Resource_Name__c = contactList[0].Id;
				}
			}
			if(ts.Case_Resource_ID__c == null){
				if(caseResourceMap.containsKey(ts.Case__c) && caseResourceMap.get(ts.Case__c) !=null){
					ts.Case_Resource_ID__c = caseResourceMap.get(ts.Case__c);
				}
			}
		}
	}
}

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
Jon FoyJon Foy
yes i did this, same error...Error: Compile Error: unexpected token: 'public class TimeSlip' at line 1 column 0

shouldn't it start with @istest?
Vatsal KothariVatsal Kothari
No its not test class.
Can you please share the screenshot of that error?
Jon FoyJon Foy
I need a test class for this, so i can move the trigger into prod.
Vatsal KothariVatsal Kothari
Test class:

@isTest 
public class TimeSlipTestClass {
    static testMethod void timeSlipTestMethod() {
        
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser123@testorg.com', 
                          EmailEncodingKey='UTF-8', FirstName = 'Test123', LastName='Test123', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='standarduser123@testorg.com');
        insert u;
        System.runAs(u) {
            Case c = new Case();
            c.status = 'new';
            c.Origin = 'web';        
            insert c;
    
            Contact con = new Contact();
            con.FirstName = 'Test123';
            con.LastName = 'Test123';        
            insert con;
            
            Case_Resource__c caseRes = new Case_Resource__c();
            caseRes.Case__c = c.Id;
            caseRes.Case_Resource_Name__c = u.Id;
            insert caseRes;
            
            TimeSlip__c ts = new TImeSlip__c();
            ts.Case__c = c.Id;
            insert ts;
        }		        
	}
}

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
Jon FoyJon Foy
Code Coverage Failure
The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
UpdateTimeSlip
Vatsal KothariVatsal Kothari
Save the above class and click on run test.
Jon FoyJon Foy
Apex Test Result Detail

Time Started 8/29/2014 11:05 AM
Class TimeSlipTestClass
Method Name timeSlipTestMethod
Pass/Fail Fail
Error Message System.DmlException: Insert failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, E-mail: invalid email address: standarduser123testorg.com: [Email]
Stack Trace Class.TimeSlipTestClass.timeSlipTestMethod: line 10, column 1
Vatsal KothariVatsal Kothari
Have you removed '@' from email Id?

User u = new User(Alias = 'standt', Email='standarduser123@testorg.com', 
                          EmailEncodingKey='UTF-8', FirstName = 'Test123', LastName='Test123', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='standarduser123@testorg.com');



Jon FoyJon Foy
that solved that issue, now i'm getting

TimeSlipTestClass timeSlipTestMethod System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Resource Name: id value of incorrect type: 005A0000005ZCNIIA4: [Resource_Name__c]
Stack Trace: Class.TimeSlipTestClass.timeSlipTestMethod: line 25, column 1

FYI, I had to change the field name from Case_Resource_Name__c to Resource_Name__c
Vatsal KothariVatsal Kothari
Hey Jon,

So for this you just have to replace 24th line with below code:

caseRes.Resource_Name__c = u.Id;

This will solve your problem..:)

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal