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
beenerbeener 

Is this a realy bad idea?

Hi All,

I have a custom object named timesheet__c, which has a date field week_ending__c (this date can only be a saturday)

I am required to show clickable list of dates,
where clicking on a date would lead to a view of all objects with week_ending__c = <date selected>
I don't know Visual force or Apex. so I was thinking  have a more simple solution, please tell me if this is a really bad Idea.

I would create another custom object: weekend_dates__c. and feed it with all of the Saturdays in the coming year or two.

When a user chooses week_ending__c from a date control, another field (a lookup to weekend_dates__c) would look up to the appropriate Saturday.

This would allow me to easily design a tab listing weekend_dates__c, and in which all related timesheet__c will be seen under related lists.

The question is, how hard, if at all possible, to create a lookup field that automatically looks up a record. can this be done with a default formula?

I'm thankful to any ideas you may come up with.

Ben
werewolfwerewolf
That does sound rather needlessly complicated.  What is it exactly you actually want to do – sort the timesheets by saturdays?
beenerbeener
I am only trying to do the following:

Have a list of dates, where by each date there would be a count of the number of sheets that are marked by that date.

Clicking on a date, would lead one to an inline editable, list view of the object.

Simple enough, I found I don't know how to do that.

Ben
werewolfwerewolf
What if you make a formula field, or a field filled by an Apex trigger (which would be much easier), that calculates the Saturday after the filing date of the timesheet?  That way you'd have the saturday automatically stamped on each timesheet, which would relieve your users of having to associate it, plus you could report on it by grouping or sorting by the saturday date.
werewolfwerewolf
The inline editable list view wouldn't be that easy though btw -- right now you'd have to make a Visualforce component out of it.  Although if you use the scheme I noted above with the "next saturday" field on the timesheet object, you could just make a list view sorted by saturdays and inline edit stuff in place.  It could get long over time though.
SiriusBlackOpSiriusBlackOp

Yikes!  LOL!

Without using Apex this could get a little crazy, but it's doable.  You need a pretty complex validation rule to do this. This site has a pretty decent calculation, and the only problems I see with it are validation data that the date control should handle for you anyway (ie. you can enter things like 0 for the day and month and it doesn't complain.  lol).  http://www.urquhart.org/clanurquhartge/birthday_calculator.htm

You will have to sort of use this:

Code:
<script LANGUAGE="JavaScript">

function MakeArray(n) {
this.length = n;
for (var i = 1; i <=n; i++) {
this[i] = 0;
   }
}
days = new MakeArray(7);
days[0] = "Saturday"
days[1] = "Sunday"
days[2] = "Monday"
days[3] = "Tuesday"
days[4] = "Wednesday"
days[5] = "Thursday"
days[6] = "Friday"
months = new MakeArray(12);
months[1] = "January" 
months[2] = "February" 
months[3] = "March" 
months[4] = "April" 
months[5] = "May" 
months[6] = "June" 
months[7] = "July" 
months[8] = "August"
months[9] = "September" 
months[10] = "October"
months[11] = "November"
months[12] = "December"
function compute(form) {
var val1 = parseInt(form.day.value, 10)
if ((val1 < 0) || (val1 > 31)) {
alert("Day is out of computable range.")
}
var val2 = parseInt(form.month.value, 10)
if ((val2 < 0) || (val2 > 12)) {
alert("Month is out of computable range.")
}  
var val2x = parseInt(form.month.value, 10)
var val3 = parseInt(form.year.value, 10)
if (val2 == 1) {
val2x = 13;
val3 = val3-1
}
if (val2 == 2) {
val2x = 14;
val3 = val3-1
}
var val4 = parseInt(((val2x+1)*3)/5, 10)
var val5 = parseInt(val3/4, 10)
var val6 = parseInt(val3/100, 10)
var val7 = parseInt(val3/400, 10)
var val8 = val1+(val2x*2)+val4+val3+val5-val6+val7+2
var val9 = parseInt(val8/7, 10)
var val0 = val8-(val9*7)
form.result1.value = months[val2]+" "+form.day.value +", "+form.year.value
form.result2.value = days[val0]
}   
// End -->
</script>

... but we are only concerned with small peices of it.  First, Saturday should always mean that val0 = 0.  So we don't really need the arrays.  Also, we shouldn't need the validation for calc range as the date field will do that for us.  Lastly, we don't need to set the result textboxes.  So... we are left with the bits we need.
 

Code:
//Get info from provided date
var val1 = parseInt(form.day.value, 10) var val2 = parseInt(form.month.value, 10) var val2x = parseInt(form.month.value, 10) var val3 = parseInt(form.year.value, 10)
//make adjustments for leap year
if (val2 == 1) {
val2x = 13;
val3 = val3-1
}
if (val2 == 2) {
val2x = 14;
val3 = val3-1
}
//calulate val0 to see if it = 0, if Yes then it is a Saturday.
var val4 = parseInt(((val2x+1)*3)/5, 10)
var val5 = parseInt(val3/4, 10)
var val6 = parseInt(val3/100, 10)
var val7 = parseInt(val3/400, 10)
var val8 = val1+(val2x*2)+val4+val3+val5-val6+val7+2
var val9 = parseInt(val8/7, 10)
var val0 = val8-(val9*7)

So, all you have to do now is struture all of that up above so that is it in one line and convert it to the validation rule code. parseInt = Round, form.day.value will now use Day, etc.

Do you need my help to take this farther, or do you think you can take it from here?

werewolfwerewolf
Converting this JavaScript to validation rule syntax won't be easy; also, a validation rule probably is not what is called for here. 

It's more like a formula field or a date field filled by Apex (personally I'd go with the latter).  In Apex, it's as simple as using the toStartOfWeek function and adding 6 days to get to the next Saturday.  Just a couple of lines of code.  Not that difficult.
SiriusBlackOpSiriusBlackOp

Well, first he said this: "I don't know Visual force or Apex. so I was thinking  have a more simple solution...", but you are right that sorting this out in APEX is WAY easier.

Second, shame on me for stopping me reading of what he wanted after I got this far.

"Hi All,

I have a custom object named timesheet__c, which has a date field week_ending__c (this date can only be a saturday)

I am required to show clickable list of dates,
where ....
I don't know Visual force or Apex. so I was thinking  have a more simple solution, please tell me if this is a really bad Idea."


Which made me think he just wanted a date input field that only accepted saturdays. --  Anyway, I went back up and "read" it all and now I'm really confused.  LOL!  It sounds like he wants a tab that acts like a summary report grouped by week_ending__c... but then he wants a list view that is inline editable??? I didn't think a list veiw had any any way to be inline editable.  ----  OK... apparently you can edit some things by checking "Enable Enhanced Lists"... interesting.  Is that new to v9?

werewolfwerewolf
Yes, you can do inline editing using enhanced list views, but only of list views, not of related lists.  He kind of wants an object for each saturday that lists timesheets associated with the prior week, and to be able to inline edit them in place, which isn't really possible now.  I think what I proposed is the next best thing.

Beener, you might think about calling your Salesforce.com support guy and asking about the Force.com Extension program whereby Salesforce.com developers can write simple Apex and Visualforce pages for you at a low cost.
beenerbeener
Alright, thanks to both of you.
I understand from the discussion that what I want isn't available in any simple manner.

SiriusBlackOp, is that script supposed to help me find the upcomng saturday? if it is I have  a one line formula that does that. if not, what does it do?

I did look into VF, as werewolf said, I can only look at enhanced, existing lists, that is, I cannot make an enhanced list by way of a query, such as [select ... where date1__c= this_weekend_date].

This is a real shame, but I guess I'll have to work within what I have.


Thanks again to both of you.
SiriusBlackOpSiriusBlackOp
It would validate that any day selected in a date field was a Saturday (past, present, future and including leap years).  You would have to convert the code to a format that is suited for a validation control.
ben_hizakben_hizak
Here's my validation that a date is saturday

CASE( MOD( date__c - DATE(1900, 1, 7), 7),
6, 1,
0 ) = 0


I copied and pasted it from my Validation rule.

It's much shorter then the Java script. do you see any fault with it?
SiriusBlackOpSiriusBlackOp

Tested it and it seems to work great!  Covered both leap and non-leap years.

If anyone needs to validate a day, that is the way to go.  :smileyhappy:

Thanks for the post.