You need to sign in to do that
Don't have an account?
Jon Foy
Trigger Code Isn't Quite Working - Need Help
I have the following Code. It updates a custom object (TimeSlip), anytime it is saved. 98% of the time a user will save their own timecards, and it works perfectly. However, in some rare cases, another user will need to update a timeslip. In these cases, the "Case_Resource__ID" on the TimeSlip is being updated to the Case Resource ID where the Resrouce Name, matches the current user...it should match the Resource name listed in the Resource Name field of the TImeslip. I can't determine why it's not.
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(caseResourceMap.containsKey(ts.Case__c) && caseResourceMap.get(ts.Case__c) !=null){ts.Case_Resource_ID__c = caseResourceMap.get(ts.Case__c);
}
}
}
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(caseResourceMap.containsKey(ts.Case__c) && caseResourceMap.get(ts.Case__c) !=null){ts.Case_Resource_ID__c = caseResourceMap.get(ts.Case__c);
}
}
}
Resource_Name__c =: userId
Just make sure you are not checking Name with the Id It should be either
Case_Resource_ID__c =: userId or Resource_Name__c =: userName