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
carmilyn.c martincarmilyn.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');    
            }
        }
    }
 
 
Best Answer chosen by carmilyn.c martin
Prateek Singh SengarPrateek Singh Sengar
Just add a null check before the if statement
String userid= userinfo.getUserId();
 
User userRec = [select Global_ID__c, Profile.Name from User where Id=:userid limit 1];

if(userRec.Global_ID__c != null)
{
if(userRec.Global_ID__c.contains('esi1') && ! userRec.Profile.Name.contains('System_Admin'))
{
 //YOUR TRIGGER LOGIC
}
}

 

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi,
In your trigger you can add something like
 
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'))
{
 //YOUR TRIGGER LOGIC
}

Hope this helps
carmilyn.c martincarmilyn.c martin
Hello I added that and here is what it looks like now: But I am getting an error: Error: Compile Error: Variable does not exist: userRec at line 5 column 7



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');    
                    }
            }
    } 
}
Prateek Singh SengarPrateek Singh Sengar
Sorry there was a typo in my code, please use the below
 
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'))
{
 //YOUR TRIGGER LOGIC
}


 
carmilyn.c martincarmilyn.c martin
Hi,

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
Prateek Singh SengarPrateek Singh Sengar
OOPS..
Try this
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') && ! userRec.Profile.Name.contains('System_Admin'))
{
 //YOUR TRIGGER LOGIC
}

 
Prateek Singh SengarPrateek Singh Sengar
By the way.. please ensure that the profile name is correct. If you are using standard system admin profile it should be 'System Administrator'
carmilyn.c martincarmilyn.c martin
Hi again,
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!



 
Prateek Singh SengarPrateek Singh Sengar
Just add a null check before the if statement
String userid= userinfo.getUserId();
 
User userRec = [select Global_ID__c, Profile.Name from User where Id=:userid limit 1];

if(userRec.Global_ID__c != null)
{
if(userRec.Global_ID__c.contains('esi1') && ! userRec.Profile.Name.contains('System_Admin'))
{
 //YOUR TRIGGER LOGIC
}
}

 
This was selected as the best answer
Prateek Singh SengarPrateek Singh Sengar
A better syntax would be 

if(userRec.Global_ID__c != null && userRec.Global_ID__c != '')
carmilyn.c martincarmilyn.c martin
You are amazing! Thank you very much!!!!It worked!!! :):):)