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
Freddie P. SmithFreddie P. Smith 

Trigger for dates

Hello,

I would like to create possibly a trigger for dates that selects the real number of days according to availability.

For example if I have an Account that has available days only Monday and Tuesday every week, but selects for example Off Days between 16.05.2016 (Monday) till 22.05.2016 (Sunday), I don't want it to roll-up the days and count 7 days, but use the Availabilty and show me only the real 2 day count for that period.

Is there a way to do this?

Thank you!
Best Answer chosen by Freddie P. Smith
Atul GuptaAtul Gupta
if they are text fields, then use this
 
trigger TriggerAccount on Account (before insert, before update){
    
	Integer avInt = 0;
	
    for(Account acc : Trigger.new){
		
		if(!String.isBlank(acc.Monday__c)){
			avInt = avInt+1;
		}
		if(!String.isBlank(acc.Tuesday__c)){
			avInt = avInt+1;
		}
		if(!String.isBlank(acc.Wednesday__c)){
			avInt = avInt+1;
		}
		if(!String.isBlank(acc.Thursday__c)){
			avInt = avInt+1;
		}
		if(!String.isBlank(acc.Friday__c)){
			avInt = avInt+1;
		}
		if(!String.isBlank(acc.Saturday__c)){
			avInt = avInt+1;
		}
		if(!String.isBlank(acc.Sunday__c)){
			avInt = avInt+1;
		}
		
		acc.Availability_Days__c = avInt;
	}
}

 

All Answers

Atul GuptaAtul Gupta
Hi Freddie, are all these fields i.e. Monday, Tuesday, etc Date fields?
Atul GuptaAtul Gupta
Try this, assuming these fields are of Data type
 
trigger TriggerAccount on Account (before insert, before update){
	
	Integer avInt = 0;
	
	for(Account acc : Trigger.new){
		
		if(acc.Monday__c <> null){
			avInt = avInt+1;
		}
		if(acc.Tuesday__c <> null){
			avInt = avInt+1;
		}
		if(acc.Wednesday__c <> null){
			avInt = avInt+1;
		}
		if(acc.Thursday__c <> null){
			avInt = avInt+1;
		}
		if(acc.Friday__c <> null){
			avInt = avInt+1;
		}
		if(acc.Saturday__c <> null){
			avInt = avInt+1;
		}
		if(acc.Sunday__c <> null){
			avInt = avInt+1;
		}
		acc.Availability_Days__c = avInt;
	}
}

If all these fields are blank, then Availability_Days__c field will be 0.
Freddie P. SmithFreddie P. Smith
Hey Atul,

Actually they are text fields, because they have the available hours when fields are not blank. For example: Friday: 08:00-12:30
 
Atul GuptaAtul Gupta
if they are text fields, then use this
 
trigger TriggerAccount on Account (before insert, before update){
    
	Integer avInt = 0;
	
    for(Account acc : Trigger.new){
		
		if(!String.isBlank(acc.Monday__c)){
			avInt = avInt+1;
		}
		if(!String.isBlank(acc.Tuesday__c)){
			avInt = avInt+1;
		}
		if(!String.isBlank(acc.Wednesday__c)){
			avInt = avInt+1;
		}
		if(!String.isBlank(acc.Thursday__c)){
			avInt = avInt+1;
		}
		if(!String.isBlank(acc.Friday__c)){
			avInt = avInt+1;
		}
		if(!String.isBlank(acc.Saturday__c)){
			avInt = avInt+1;
		}
		if(!String.isBlank(acc.Sunday__c)){
			avInt = avInt+1;
		}
		
		acc.Availability_Days__c = avInt;
	}
}

 
This was selected as the best answer
Freddie P. SmithFreddie P. Smith
Thank you Atul! I will test it!

All the best for you.
Atul GuptaAtul Gupta
Sure Freddie, let me know if you face any issues.