+ Start a Discussion
TheLearnerTheLearner 

Showing error msg when the holiday is entered

Hi Experts,

There is checkbox filed called Non working day, when the user click this checkbox any date user can enter in those fields including holiday , if this Nonworking day is unchecked user should not enter the holiday date and weekends, so my problem is that i want show error message when user enter the holiday day, could anyone help me plesae.

01 trigger CheckonNonWorkingday on SW_Notice__c (before insert,before update)

02 {

03     List<Holiday_List__c> holidayList = new List<Holiday_List__c>([Select Holiday_Date__c from Holiday_List__c]);

04     Set<Date> holidayDayeSet = new Set<Date>();

05      

06     for(Holiday_List__c holiday : holidayList){

07         holidayDayeSet.add(holiday.Holiday_Date__c);

08     }

09      for(SW_Notice__c swnotice : trigger.new)

10      {

11  

12             if(!swnotice.Non_Working_Day__c)

13             {

14  

15                 if(holidayDayeSet.contains(swnotice.Estimated_Start_Date__c) || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sun')

16                 {

17  

18                     swnotice.addError('Estimated start date must be a Weekday');

19  

20                 }

21  

22                 else if(holidayDayeSet.contains(swnotice.Estimated_End_Date__c) || ((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sun')

23                 {

24  

25                     swnotice.addError('Estimated End date must be a Weekday');

26  

27                 }

28  

29                else if(holidayDayeSet.contains(Date.newInstance(swnotice.Actual_Start_Date__c.year(),swnotice.Actual_Start_Date__c.month(),swnotice.Actual_Start_Date__c.day())) || swnotice.Actual_Start_Date__c.format('E') == 'Sat' || swnotice.Actual_Start_Date__c.format('E')=='Sun)
30                 {

31  

32                     swnotice.addError('Actual start date must be a Weekday');

33  

34                 }

35  

36                 else if(holidayDayeSet.contains(swnotice.Target_Stop__c) || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sun')

37                 {

38  

39                     swnotice.addError('Target Stop date must be a Weekday');

40  

41                 }

42  

43                 else if(holidayDayeSet.contains(swnotice.Works_Stop__c) || ((DateTime)swnotice.Works_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Works_Stop__c).format('E') == 'Sun')

44                 {

45  

46                     swnotice.addError('Work Stop date must be a Weekday');

47  

48                 }

49  

50             }

51  

52         }

53  

54      

55  

56 }
Best Answer chosen by TheLearner
Abhishek BansalAbhishek Bansal
Hi,

I have modified the above code according to your requirement.
Please find the updated code below :
 
trigger CheckonNonWorkingday on SW_Notice__c (before insert,before update) {
    List<Holiday_List__c> holidayList = new List<Holiday_List__c>([Select Holiday_Date__c from Holiday_List__c]);
	Set<Date> holidayDayeSet = new Set<Date>();
	
	for(Holiday_List__c holiday : holidayList){
		holidayDayeSet.add(holiday.Holiday_Date__c);
	}
	for(SW_Notice__c swnotice : trigger.new) {

		if(!swnotice.Non_Working_Day__c) {
				
			if(holidayDayeSet.contains(Estimated_Start_Date__c)){
				swnotice.addError('Estimated start date should not be a Holiday');
			}
			else if(holidayDayeSet.contains(Estimated_End_Date__c)){
				swnotice.addError('Estimated End date should not be a Holiday');
			}
			else if(holidayDayeSet.contains(Works_Stop__c)){
				swnotice.addError('Work Stop date should not be a Holiday');
			}
			else if(holidayDayeSet.contains(Target_Stop__c){
				swnotice.addError('Target Stop date should not be a Holiday');
			}
			else if(holidayDayeSet.contains(Date.newInstance(swnotice.Actual_Start_Date__c.year(),swnotice.Actual_Start_Date__c.month(),swnotice.Actual_Start_Date__c.day()))){
				swnotice.addError('Work Stop date should not be a Holiday');
			}
            else if(((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sun'){
                swnotice.addError('Estimated start date must be a Weekday');
            }
            else if(((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sun'){
                swnotice.addError('Estimated End date must be a Weekday');
            }
            else if(swnotice.Actual_Start_Date__c.format('E') == 'Sat' || swnotice.Actual_Start_Date__c.format('E') == 'Sun'){
                swnotice.addError('Actual start date must be a Weekday');
            }
            else if(((DateTime)swnotice.Target_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sun'){
                swnotice.addError('Target Stop date must be a Weekday');
            }
            else if(((DateTime)swnotice.Works_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Works_Stop__c).format('E') == 'Sun') {
                swnotice.addError('Work Stop date must be a Weekday');
            }
        }
    }
}

Let me know if you any issue with it.

Thanks,
Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi,

I have modified the above code according to your requirement.
Please find the updated code below :
 
trigger CheckonNonWorkingday on SW_Notice__c (before insert,before update) {
    List<Holiday_List__c> holidayList = new List<Holiday_List__c>([Select Holiday_Date__c from Holiday_List__c]);
	Set<Date> holidayDayeSet = new Set<Date>();
	
	for(Holiday_List__c holiday : holidayList){
		holidayDayeSet.add(holiday.Holiday_Date__c);
	}
	for(SW_Notice__c swnotice : trigger.new) {

		if(!swnotice.Non_Working_Day__c) {
				
			if(holidayDayeSet.contains(Estimated_Start_Date__c)){
				swnotice.addError('Estimated start date should not be a Holiday');
			}
			else if(holidayDayeSet.contains(Estimated_End_Date__c)){
				swnotice.addError('Estimated End date should not be a Holiday');
			}
			else if(holidayDayeSet.contains(Works_Stop__c)){
				swnotice.addError('Work Stop date should not be a Holiday');
			}
			else if(holidayDayeSet.contains(Target_Stop__c){
				swnotice.addError('Target Stop date should not be a Holiday');
			}
			else if(holidayDayeSet.contains(Date.newInstance(swnotice.Actual_Start_Date__c.year(),swnotice.Actual_Start_Date__c.month(),swnotice.Actual_Start_Date__c.day()))){
				swnotice.addError('Work Stop date should not be a Holiday');
			}
            else if(((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sun'){
                swnotice.addError('Estimated start date must be a Weekday');
            }
            else if(((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sun'){
                swnotice.addError('Estimated End date must be a Weekday');
            }
            else if(swnotice.Actual_Start_Date__c.format('E') == 'Sat' || swnotice.Actual_Start_Date__c.format('E') == 'Sun'){
                swnotice.addError('Actual start date must be a Weekday');
            }
            else if(((DateTime)swnotice.Target_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sun'){
                swnotice.addError('Target Stop date must be a Weekday');
            }
            else if(((DateTime)swnotice.Works_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Works_Stop__c).format('E') == 'Sun') {
                swnotice.addError('Work Stop date must be a Weekday');
            }
        }
    }
}

Let me know if you any issue with it.

Thanks,
Abhishek
This was selected as the best answer
TheLearnerTheLearner
HIi Abhishek,

Thanks its working fine.

I need one more help could you help me in this requriment please.

If Twm Job(TWM_Job__c Master of TWM Task) field Job Code(Job_Code__c)= Poss and TWM Task(TWM_Tasks__c) record type is 'PMP property survey'`and Install Type(Install_Type__c)='screw In', then create TWM Task record with record type 'Install'and the picklist field TypeofInstall(Type_of_Install__c)='screwin' and the record should be populated like with these fields(Name,Date(Date__c),Planned Screw In End(Planned_Screw_In_End__c this field need to come from the object called TWMP_Batch__c there is no relation with any object),Address1 (Address_1__c formula),Address2(Address_2__c formula ),Town(Town__c formula),PostCode(Postocode__c formula),RecordTypeName(Record_Type_Name__c formula),Job(Job__c Master detail (relationshipTWM_Tasks)),No ofinstalled meters(No_of_Installed_Meters__c picklist ),TypeofInstall(Type_of_Install__c),Last Task(Latest_Task__c checkbox needs to be true)
Abhishek BansalAbhishek Bansal
Hi ,

I have already seen your question on forum but the requirements are not clear to me.
Please clearly explain what you want to achieve.

Abhishek
TheLearnerTheLearner
Hi Abhishek,

Thanks for the quick reply.

This is requriement if anything is there please tell me.



My requirment is that:
1. There is object called TWM JOb(TWM_Job__c) in that there is field called  JOB CODE(Job_Code__c), when this JOB CODE='FOSS' and its child TWM TASK object       (TWM_Tasks__c details of TWM JOB) if this TWM Task record type is 'PMP Propery survey' and a field called Install Type(Install_Type__c)='screw In'(which is there in the TWM Task avialble for the record type 'PMP Propery Survey'.

2. Then Create TWM Task record with reocrd type Install and TypeofInstall(Type_of_Install__c)='screwin' and this record should populate the fields called
NAME,Date(Date__c),Planned Screw In End(Planned_Screw_In_End__c this field need to come from the object called TWMP_Batch__c there is no relation with any object),Address1 (Address_1__c formula),Address2(Address_2__c formula ),Town(Town__c formula),PostCode(Postocode__c formula),RecordTypeName(Record_Type_Name__c formula),Job(Job__c Master detail (relationshipTWM_Tasks)),No ofinstalled meters(No_of_Installed_Meters__c picklist ),TypeofInstall(Type_of_Install__c),Last Task(Latest_Task__c checkbox needs to be true)




 
TheLearnerTheLearner
Hi Abhishek,

Im getting this error while saving the code:

There are validations as well:

1. Actual_Start_Date:IF( AND(ISPICKVAL(Pre_Construction_Survey_Been_Carried_Out__c, 'No') ,NOT(ISBLANK( Actual_Start_Date__c ) )),true,false)

2. Estimate_Start_Date: IF( ISBLANK( NRSWA_Number__c ) , Estimated_Start_Date__c < IF(ISNULL(CreatedDate), TODAY() , (DATEVALUE(CreatedDate)))
, false)

3.Estimate_End_Date: Estimated_End_Date__c < Estimated_Start_Date__c

4. Est_Start_Date_Greater_Than_Est_End_Date:Estimated_Start_Date__c > Estimated_End_Date__c


Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CheckonNonWorkingday caused an unexpected exception, contact your administrator: CheckonNonWorkingday: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CheckonNonWorkingday: line 84, column 1;
coudl you please help me its urgent
This is the trigger:


trigger CheckonNonWorkingday on SW_Notice__c (before insert,before update) 
{

        List<shutdown_day__c> holidayList = new List<shutdown_day__c>([Select Date__c from shutdown_day__c]);

        Set<Date> holidayDayeSet = new Set<Date>();

         

        for(shutdown_day__c holiday : holidayList)
        {

            holidayDayeSet.add(holiday.Date__c);

        }

        for(SW_Notice__c swnotice : trigger.new) 
        {

     

            if(!swnotice.Non_Working_Day__c) 
            {

                     

                if(holidayDayeSet.contains(swnotice.Estimated_Start_Date__c))
                {

                    swnotice.addError('Estimated start date should not be a Holiday');

                }

                else if(holidayDayeSet.contains(swnotice.Estimated_End_Date__c))
                {

                    swnotice.addError('Estimated End date should not be a Holiday');

                }

                else if(holidayDayeSet.contains(swnotice.Works_Stop__c))
                {

                    swnotice.addError('Work Stop date should not be a Holiday');

                }

                else if(holidayDayeSet.contains(swnotice.Target_Stop__c))
                {

                    swnotice.addError('Target Stop date should not be a Holiday');

                }

                else if(holidayDayeSet.contains(Date.newInstance(swnotice.Actual_Start_Date__c.year(),swnotice.Actual_Start_Date__c.month(),swnotice.Actual_Start_Date__c.day())))
                {

                    swnotice.addError('Actual start  date should not be a Holiday');

                }

                else if(((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Estimated start date must be a Weekday');

                }

                else if(((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Estimated End date must be a Weekday');

                }

                else if(swnotice.Actual_Start_Date__c.format('E') == 'Sat' || swnotice.Actual_Start_Date__c.format('E') == 'Sun')
                {

                    swnotice.addError('Actual start date must be a Weekday');

                }

                else if(((DateTime)swnotice.Target_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sun')
                {

                    swnotice.addError('Target Stop date must be a Weekday');

                }

                else if(((DateTime)swnotice.Works_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Works_Stop__c).format('E') == 'Sun') 
                {

                    swnotice.addError('Work Stop date must be a Weekday');

                }

            }

        }

    }
TheLearnerTheLearner
Hi,

Target_Stop__c and Estimated_End_Date__c these two fieds are date fields using in forumala, if i put these two in page layout its not throwing error, if i remove these two fieds its throwing error , i need to display these two fieelds with formula fields which is mentioned below

Target_Stop_display__c(Target_Stop__c)(formula)
Estimated_End_Date_Display__c(Estimated_End_Date__c)(formula)
Abhishek BansalAbhishek Bansal
Hi,

Right now i am in the office and according to the rules and regulations of Office, we are not allowed to work on any outside work.
I will look into your issue at evening when i reach home.

Thanks,
Abhishek
TheLearnerTheLearner
Hi Abhishek,

i need to do some amendments in my trigger, when ever Defect_on_Hold__c checkbox  is checked, i need to change this Activity_Status__c field value from    "INTrR" to "HOLD". im not getting where and how exactly i need to amendments,Could anyone help please.


trigger tRIIO_Calc_Activity_Status_And_Fields on tRIIO_Activity__c (before update, before insert) {
    //To calculate Reinstatement Status
    if(!Test.isRunningTest())
    if(Trigger.isUpdate){
        Boolean isActivityWithoutOM  = false;
        Boolean isAnyOMWithoutFM = false;
        Boolean doesAnyFMHaveInterimDate = false;
        String Status = ''; 
        Integer countInterimDate = 0;
        for(tRIIO_Reinstatement_FM__c fm:[SELECT Id, Interim_Date__c FROM tRIIO_Reinstatement_FM__c WHERE Activity__c =: trigger.new[0].id]){
            if(fm.Interim_Date__c != null)
                countInterimDate++;
        }
        List<tRIIO_Reinstatement_OM__c> OM = [select id, name,FM__c from tRIIO_Reinstatement_OM__c where Activity__c =: trigger.new[0].id ];
        Boolean isNotWithOutOM = false;
        Boolean isAnyOMWOFM = false;
        Boolean doesNiFMHaveInterimDate = false;
        String NGA_Work_Status_Text = '';
        if (OM.size()==0) {
            isActivityWithoutOM = true;
        } else {   
            //Loop for all OMs and if any OM is without FM then set the flag
            for(tRIIO_Reinstatement_OM__c a : OM) {
                if (a.FM__c!=true) {
                    isAnyOMWithoutFM = true;
                }
           }
           if (countInterimDate > 0) {
               doesAnyFMHaveInterimDate = true;
           } 
        }
        //if there are no Original Measures for Notice set Status to 'Schld'
        if (isActivityWithoutOM == true) {
            Status = 'Schld';
        } else {
            //if any original measures is without a final measures set status to 'InPrg'
            if (IsAnyOMWithoutFM == true) {
                Status = 'InPrg';
            } else {
                //if any final measures have an interim date set status to 'InPrg' else set to 'IntReinst'
                if (DoesAnyFMHaveInterimDate == true) {
                    Status = 'IntrC';
                } else {
                    Status = 'PermC';
                }
            }   
         }
        trigger.new[0].Status__c = Status;
    }
    //To calculate Activity Status
    if(Trigger.isInsert || Trigger.isUpdate){
        String ActivityStatus = '';
        tRIIO_Activity__c [] ActivityNew;
        ActivityNew = trigger.new;
        for( tRIIO_Activity__c Nots : ActivityNew){
            if(Nots.Activity_Status__c != 'IntrR(S)'){
                Nots.Clk_Int_Perm__c = false;
            }
        }
        Profile p = [SELECT Id,Name FROM profile WHERE id=:Userinfo.getProfileId()];
        String currentProfile = p.Name;
        String readOnlyProfiles = 'NG Read-Only';//Add other read-only profiles here seperated by ,
        if (readOnlyProfiles.indexOf(currentProfile) > -1){
            ActivityNew[0].addError('You do not have sufficient privilages to add or modify records.');
        }
        for(tRIIO_Activity__c activity : ActivityNew){
            if ((activity.Activity_Status__c != 'IntrR(S)')&&(activity.Activity_Status__c != 'Cancelled') ) {
                if (activity.Abandoned__c == true){
                    if(activity.Hit_Int_Perm__c == true){
                        ActivityStatus = 'Awaiting Notice';
                    } else{
                        ActivityStatus = 'Abandoned';
                    }
              }
              else{
                  //If Works Close Date AND Registration Date exists AND Reinstatement Status = "Schld" then "Nil Reinstatement"
                  if ((activity.Works_Close__c != null) && (activity.Registration__c != null) && (activity.Status__c == 'Schld'))
                      ActivityStatus = 'Nil Reinstatement';
                  //If Works Close Date AND Registration Date exists AND Reinstatement Status = "PermC" then "PermR"
                  else if ((activity.Works_Close__c != null) && (activity.Registration__c != null) && (activity.Status__c == 'PermC'))
                      ActivityStatus = 'PermR';
                  //If Works Close Date exists AND Registration Date does not exist AND Reinstatement Status = "PermC" then "PermC"     
                  else if ((activity.Works_Close__c != null) && (activity.Registration__c == null) && (activity.Status__c == 'PermC'))
                      ActivityStatus = 'PermC';
                  //If Works Close Date AND Registration Date exists AND Reinstatement Status = "IntrC" then "IntrR" 
                  else if ((activity.Works_Close__c != null) && (activity.Registration__c != null) && (activity.Status__c == 'IntrC'))
                      ActivityStatus = 'IntrR';
                  //If Works Close Date exists AND Registration Date does not exist AND Reinstatement Status = "IntrC" then "IntrC"  
                  else if ((activity.Works_Close__c != null) && (activity.Registration__c == null) && (activity.Status__c == 'IntrC'))
                      ActivityStatus = 'IntrC';
                  //If Actual Start Date exists then "InPrg
                  else if ((activity.Actual_Start__c != null))
                      ActivityStatus = 'InPrg';
                  //If Works Start Date exists then "Notice Raised" 
                  else if ((activity.Works_End_Date__c != null))
                      ActivityStatus = 'Notice Raised';
                  else
                      ActivityStatus = 'Awaiting Notice';                    
               }
               activity.Activity_Status__c = ActivityStatus;
               //If this is a new record then default reinstatement status to 'Schld'
               if(Trigger.isInsert)
                   activity.Status__c = 'Schld';
            }
            //when Reinstatement status changes to a status that is not 'PermC'or 'IntrC'then deattach Activity from closure report
            //so that it can can be added to next batch
            if ((activity.Status__c != 'PermC')&&(activity.Status__c != 'IntrC')){
                activity.Closure_Report__c = null;
            }
            if(Trigger.isInsert){
                Map<Id,Double> mapCounts = tRIIO_QuotationAttachment.ValidateNotice(ActivityNew);
                for(tRIIO_Activity__c activity_Quotation: Trigger.new){                
                    if(activity_Quotation.Works_Order__c != null){
                        if(mapCounts.get(activity_Quotation.Works_Order__c) != null && mapCounts.get(activity_Quotation.Works_Order__c) != 0.0){
                            activity_Quotation.addError('Notice already generated for this Quotation'); 
                        }
                    }
                }
            }    
        }
    }