Replicate Salesforce Mod Function

  • 1
  • Question
  • Updated 6 months ago
  • Answered
  • (Edited)
Hello

I have a need to calculate the date of the previous Friday. Since the mod function is not available for Skuid formula fields, I am currently doing this with a Salesforce formula field: 

(TODAY()) - MOD(TODAY() - DATE(1900,1,12),7)

This will return the date of the previous Friday, so if calculated on 5/10/2018 it will return 5/4/2018. Since Salesforce formula fields are not calculated until the record is saved, when displayed on a new record page in skuid this field will be blank until the row on the model is saved. I am trying for a way to display this on a new record page while the new record is still being edited and before it is saved.

The mod function basically takes a number, divides it by another number, and returns the remainder. Is there any way to replicate this in a Skuid UI-only formula?

Thanks!
Photo of Joe Dobbels

Joe Dobbels

  • 546 Points 500 badge 2x thumb

Posted 6 months ago

  • 1
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
Joe,

You can create your own custom formulas using Skuid.  See this page-> https://docs.skuid.com/latest/en/skuid/api/skuid_formula.html

Here is a sample that worked for me in Skuid 11.  I think custom formula fields were enabled as of Skuid 10.  Just put this into an inline snippet.  If you want to use this on other pages, then create a static resource and reference that static resource on any page where you need it.

skuid.formula.Formula (
  'MODULUS',
  function (number, divisor) {
    return number % divisor;
  },{
    namespace: 'customMath',
    numArgs : 2,
    returnType : 'number'
  }
);
When you want to reference this in a formula field, you would use it as follows:
customMath__MODULUS({{Number1}}, {{Number2}})

Thanks,

Bill
Photo of Luzie Baumgart

Luzie Baumgart, Official Rep

  • 1,310 Points 1k badge 2x thumb
Hi Joe and Bill,
You can use the mod function in a Skuid formula like this:

{{input}}%10

The sample above returns the remainder after the input value has been divided by 10.
I'm afraid that this is not covered in the documentation. I will double check it and let the documentation team know that it needs to be added, if necessary.

Greetings,
Luzie
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
Luzie,

Thanks for pointing that out!

Will other operators like '++' work?

Best!

Bill
Photo of Luzie Baumgart

Luzie Baumgart, Official Rep

  • 1,310 Points 1k badge 2x thumb
Not as far as I know. Could you let me know more details why you would need that? Would the following also meet your needs?

{{input}}+1
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
Luzie,

Thanks for the reply.  I should have put on my question...'I was wondering if...'   The '[[input}} + 1' works...just wanted to understand the breadth of operators that would work.  I can test this out myself.

Thanks!

Bill
Photo of Luzie Baumgart

Luzie Baumgart, Official Rep

  • 1,310 Points 1k badge 2x thumb
Great! We are always interested in the ideas that come up in the community :-) let me know if run into a specific scenario where the {{input}}+1 does not solve the issue.
Photo of Joe Dobbels

Joe Dobbels

  • 546 Points 500 badge 2x thumb
Thanks guys, both of those solutions work for the mod part of it, but now I'm having some trouble with the date function. I always have trouble manipulating dates in Skuid.

What the function is doing is subtracting today's date from a Friday way in the past. Then divides that value by 7 and takes the remainder. Then subtracts that remainder from today's date to return the previous Friday. If it is currently Friday it should return Today's date.

I'm wondering if this would be easier to use a Skuid UI formula or a custom Javascript formula like Bill's solution to do the whole thing.
Photo of Luzie Baumgart

Luzie Baumgart, Official Rep

  • 1,310 Points 1k badge 2x thumb
Good to hear that the mod part works now :-) regarding calculations with date&time, I once posted a detailed guideline here:
https://community.skuid.com/skuid/topics/difference-between-2-datetime-fields

Is that helpful for you?
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
Joe,

Luzie write up is very good...hopefully that helps you.  I would say that you want to keep it as a Skuid UI formula field.  This will let Skuid handle any conversions needed for timezone and between Salesforce date/time to Javascript date/time.

Thanks,

Bill
Photo of Luzie Baumgart

Luzie Baumgart, Official Rep

  • 1,310 Points 1k badge 2x thumb
Thanks Bill!
Joe, please let me know if there are any questions left :-)