Set default value to field inside module using macro.

Divyanshu Gupta asked on December 16, 2015 07:05

Hi, All
I want to set default value for one field using macro inside cms_user table of module.
I see there is a button to insert a macro into the field. Wondering if it is possible, why it is raising exception.

1- Create field inside cms_user table (Module -> Membership -> Classes -> Cms.user (systemTable))
fieldname : testInt
Type : Integer
Default Value using macro : {% CurrentUser.UserID %}

2- While creating new user it is raising the following exception :

Conversion failed when converting the nvarchar value '{% CurrentUser.UserID |(user)dgupta|(hash)81584e11fb7b5fd61a819aea394f1b89d63fe6ca7d50e957211b1c3703962b50%}' to data type int.

Thanks in advance for any assistance.

Recent Answers


Petar Kozjak answered on December 16, 2015 09:22

Not sure this is possible with macro... :D

But you can override on after save event.

0 votesVote for this answer Mark as a Correct answer

Jan Hermann answered on December 16, 2015 12:36

Hello,

Regrettably, that's too early (and the CurrentUser is not a correct collection). When the macro gets resolved no UserID is created yet. Please take advantage of global events instead:

https://docs.kentico.com/display/K9/Reference+-+Global+system+events#Reference-Globalsystemevents-ObjectEvents

In your case you can use ObjectEvents.Insert.After or UserInfo.TYPEINFO.Events.Insert.After:

https://docs.kentico.com/display/K9/Handling+object+events

Best regards,
Jan Hermann

0 votesVote for this answer Mark as a Correct answer

Divyanshu Gupta answered on December 16, 2015 13:10 (last edited on December 16, 2015 13:46)

Hi Jan Hermann,
My First Query is that "can we use macro with default value inside module" ??
If Yes then
If i pass the macro in a very simple manner like this :
Default Value using macro : {% 12 %}
Still i am getting the conversion exception.

Conversion failed when converting the nvarchar value '{% 12 %}' to data type int.

Please suggest me your valuable idea to resolve this issue as soon as possible.

Best regards,
Divyanshu Gupta

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on December 16, 2015 14:48 (last edited on December 16, 2015 14:49)

Hi,

Macro's not always return the correct type.

Try adding:

|(toint)<default value>

although it seems the macro isn't resolved at all.

Greets,

David

1 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on December 16, 2015 16:34

@Divyanshu regarding your conversion error, are you using the actual arrow to the left of the field to enter your macro? Secondly, use your macro like so to remove the signature from it: {% 12 @%}

And as Jan has stated the CurrentUser object is not available at that time so it won't resolve as you're expecting. What you might do instead is create a global handler to handle the creation of a new user and set some values you can specify as settings in your module.

0 votesVote for this answer Mark as a Correct answer

Divyanshu Gupta answered on December 17, 2015 06:22

Hi all,
I have already tried conversions with signature..

{% 12|(toint)0 @%}

As per your knowledge i am using the actual arrow to the left of the field to enter macro.
Wondering, why it is raising the same conversion exception:

Conversion failed when converting the nvarchar value '{% 12|(toint)0 @%}' to data type int.

Can someone help me please. Thank you

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.