You need to sign in to do that
Don't have an account?
carmilyn.c martin
trigger to fire on selected user base on criteria
Hi,
I am creating a trigger where it will only fire if the user matches these criteria:(fields from user object)
1. Global_ID__c like 'esi1%'
2. Profile.Name != 'System_Admin'
Here is my trigger.
trigger Time_Off_Terrirtory_more_than_90_days_old on Time_Off_Territory_vod__c (before delete,before insert,before update){
if(Trigger.isDelete)
{
for(Time_Off_Territory_vod__c tot: trigger.old)
if((tot.Date_vod__c < (system.today()- 90 )) && tot.Admin__c != True )
{
tot.adderror('You cannot delete or cancel Paid Time Off record that is 90 or more days old.' );
}
else if((Date.today() > Date.NewInstance(Date.today().year(), 1, 10)) && (tot.Date_vod__c < Date.NewInstance(Date.today().year(), 1, 1)) && tot.Admin__c != True)
{
tot.adderror('You cannot delete or cancel Time Off Entry Record of Last Fiscal Year' );
}
}
if (Trigger.isInsert)
{
for(Time_Off_Territory_vod__c tot: trigger.new)
if(
(tot.Date_vod__c < Date.NewInstance(Date.today().year(), 1, 1))
&&
( system.today() > Date.NewInstance(Date.today().year(), 1, 10))
&&
(tot.Admin__c != True)
)
{
tot.adderror('Error4');
}
}
}
I am creating a trigger where it will only fire if the user matches these criteria:(fields from user object)
1. Global_ID__c like 'esi1%'
2. Profile.Name != 'System_Admin'
Here is my trigger.
trigger Time_Off_Terrirtory_more_than_90_days_old on Time_Off_Territory_vod__c (before delete,before insert,before update){
if(Trigger.isDelete)
{
for(Time_Off_Territory_vod__c tot: trigger.old)
if((tot.Date_vod__c < (system.today()- 90 )) && tot.Admin__c != True )
{
tot.adderror('You cannot delete or cancel Paid Time Off record that is 90 or more days old.' );
}
else if((Date.today() > Date.NewInstance(Date.today().year(), 1, 10)) && (tot.Date_vod__c < Date.NewInstance(Date.today().year(), 1, 1)) && tot.Admin__c != True)
{
tot.adderror('You cannot delete or cancel Time Off Entry Record of Last Fiscal Year' );
}
}
if (Trigger.isInsert)
{
for(Time_Off_Territory_vod__c tot: trigger.new)
if(
(tot.Date_vod__c < Date.NewInstance(Date.today().year(), 1, 1))
&&
( system.today() > Date.NewInstance(Date.today().year(), 1, 10))
&&
(tot.Admin__c != True)
)
{
tot.adderror('Error4');
}
}
}
All Answers
In your trigger you can add something like
Hope this helps
trigger Time_Off_Terrirtory_more_than_90_days_old on Time_Off_Territory_vod__c (before delete,before insert,before update){
String userid= userinfo.getUserId();
User (userRec = [select Global_ID__c, Profile.Name from User where Id=:userid limit 1]);
if(userRec.Global_ID__c.contains('esi1') && ! Profile.Name.contains('System_Admin'))
{
if(Trigger.isDelete)
{
for(Time_Off_Territory_vod__c tot: trigger.old)
if((tot.Date_vod__c < (system.today()- 90 )) && tot.Admin__c != True )
{
tot.adderror('You cannot delete or cancel Paid Time Off record that is 90 or more days old.' );
}
else if((Date.today() > Date.NewInstance(Date.today().year(), 1, 10)) && (tot.Date_vod__c < Date.NewInstance(Date.today().year(), 1, 1)))
{
tot.adderror('You cannot delete or cancel Time Off Entry Record of Last Fiscal Year' );
}
}
if (Trigger.isInsert)
{
for(Time_Off_Territory_vod__c tot: trigger.new)
if
(
(tot.Date_vod__c < Date.NewInstance(Date.today().year(), 1, 1))
&&
(system.today() > Date.NewInstance(Date.today().year(), 1, 10))
)
{
tot.adderror('Yihaaa');
}
}
}
}
I appreciate all your help so much..and I hate to be a pain but I am getting an error on this line:
if(userRec.Global_ID__c.contains('esi1') && ! Profile.Name.contains('System_Admin'))
Error: Error: Compile Error: Method does not exist or incorrect signature: [Schema.SObjectField].contains(String) at line 7 column 51
Try this
Last question:
What if some users have blank values on Global_ID__c fields, i noticed that if it is blank, I am getting an error Attempt to dereference Null.
I have to make sure that all Global_ID__c field are not null, but we cannot modify our user records...is there a way the trigger will also not fire if Global_ID__c field is null or blank?
Thank you very much!
if(userRec.Global_ID__c != null && userRec.Global_ID__c != '')