locked
WorkLog, Current Date return Day of Week from related Entity RRS feed

  • Question

  • I was wondering if someone could help me with the following.  I have two entities WorkLog with a many to 1 relationship with Week.

    The worklog entity contains a date field equal to the date and time of when the log is created.

    The week entity contains three fields, Number, Week Start and Week End.

    I am wanting to add a week field to the worklog entity and have it automatically set based on the worklog.date being between a value in the week entity which looks like the following.

    Is this best handled by a preprocess query?  I'm new to programming and lightswitch, any help is appreciated!

    Friday, November 20, 2015 4:26 PM

Answers

  • Hello

    How are the two tables currently related? I can see that you have the "Week" entity within your table but what are the fields that are being linked?

    Personally I would put the code into your _Inserting method for when you are creating a WorkLog record. Within that I would do a select on the Week table and then insert the Week number into your new field.....a bit like this.

    partial void WorkLog_Inserting(WorkLog entity)
    {
    	var myInt = (from Weeks item in this.WeeksSet where entity.Date >= item.WeekStart && entity.Date <= item.WeekEnd select item.Number);
    
    	entity.YOUR_NEW_FIELD = myInt;
    }

    This would then just add the value to your new field every time you created a record.


    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    • Proposed as answer by Otis Ranger Tuesday, November 24, 2015 10:13 AM
    • Marked as answer by Angie Xu Tuesday, December 8, 2015 3:32 PM
    Monday, November 23, 2015 3:09 PM

All replies

  • Hi Eric J Cross,

    >>I am wanting to add a week field to the worklog entity and have it automatically set based on the worklog.date being between a value in the week entity which looks like the following.

    Do you mean you want to set the value of WorkLog.Time fieldbased on the range in Week entity automatically?

    Please set the WorkLog.Time as a compute field in properties window (check "Is Computed" option).

    Then select the field and click the Write Code -> WorkLog.Time_Compute to write your compute code for the value of WorkLog.Time.

    If misunderstanding, please feel free to let me know.

    Best Regards,
    Weiwei

    Monday, November 23, 2015 9:21 AM
    Moderator
  • Hello

    How are the two tables currently related? I can see that you have the "Week" entity within your table but what are the fields that are being linked?

    Personally I would put the code into your _Inserting method for when you are creating a WorkLog record. Within that I would do a select on the Week table and then insert the Week number into your new field.....a bit like this.

    partial void WorkLog_Inserting(WorkLog entity)
    {
    	var myInt = (from Weeks item in this.WeeksSet where entity.Date >= item.WeekStart && entity.Date <= item.WeekEnd select item.Number);
    
    	entity.YOUR_NEW_FIELD = myInt;
    }

    This would then just add the value to your new field every time you created a record.


    If you found this post helpful, please mark it as helpful. If by some chance I answered the question, please mark the question as answered. That way you will help more people like me :)

    • Proposed as answer by Otis Ranger Tuesday, November 24, 2015 10:13 AM
    • Marked as answer by Angie Xu Tuesday, December 8, 2015 3:32 PM
    Monday, November 23, 2015 3:09 PM
  • I think this leads me in the right direction, thank you Otis.
    Friday, December 4, 2015 3:33 PM
  • Hello Weiwei,

    I want to determine what the week number is based on by comparing the time of now in the worklog entity compared to the start and end day in the week table and return the number.

    Friday, December 4, 2015 3:35 PM