Source a value from custom record field to a field in employee record - workflow

I need to source a value from a custom record field (type: decimal) to a field in employee record(type: decimal). when the custom record is created. Is there any way I can implement this in workflow or should I go for the script?
If I choose the script which script I need to write?
Actually, I am new to script.so can anyone show me some sample?

Sounds very much like business logic, and thus shouldn't be done in a template... but, maybe you have no choice, so you can write a function like this:
<#--
Returns if two sequences (Java List-s or Java arrays usually) are equal.
Items at the same index must have common comparable type, and no item
can be null, or else you may get an error!
-->
<#function sequencesEqual s1, s2>
<#if s1?size != s2?size>
<#return false>
</#if>
<#list s1 as i1>
<#if i1 != s2[i1?index]>
<#return false>
</#if>
</#list>
<#return true>
</#function>
Then you can do things like <#if sequencesEqual(foo, bar)>...</#if>.

Related

Using FreeMarker macro in XDocReport

I am trying to use a simple freemarker macro in an XDocReport, but I couldn't find a way to add and call it.
<#macro address company>
<#if company.name??>${company.name}</#if>
<#if company.address??>${company.address}</#if>
</#macro>
Should be:
<#address company=c />
where c is your company object
You need to add MergeField (which has limit, you can divided in 2 fields) with text:
[#macro address company]
[#if company.name??]${company.name}[/#if]
[#if company.address??]${company.address}[/#if]
[/#macro]
And separated MergeField to call it (quotes,square brackets are important):
"[#address company=c1]"

Conditional Attributes in Sightly Templates (AEM/CQ)

In the Sightly templating language, for Adobe AEM6 (CQ), how do I add an attribute to an element only if a condition is true, without duplicating lots of code/logic?
e.g.
<ul data-sly-list="${items}" ${if condition1} class="selected"${/if}>
<li${if condition2} class="selected"${/if}>
Lots of other code here
</li>
</ul>
When setting HTML attributes dynamically (with an expression), Sightly guesses your intention to simplify the writing:
If the value is an empty string or if it is the false boolean, then the attribute gets remove altogether.
For instance <p class="${''}">Hi</p> and <p class="${false}">Hi</p> render just <p>Hi</p>.
If the value is the true boolean, then the attribute is written as a boolean HTML attribute (i.e. without attribute value, like for e.g. the checked, selected, or disabled form attributes).
For instance <input type="checkbox" checked="${true}"> renders <input type="checkbox" checked>.
You can then use two Sightly operators to achieve what you want (both work as in JavaScript): the ternary conditional operator, or the logical AND (&&) operator.
Ternary conditional operator
<ul data-sly-list="${items}" class="${condition1 ? 'selected' : ''}">
<li class="${condition2 ? 'selected' : ''}">
Lots of other markup here
</li>
</ul>
Logical AND operator
For that, you additionally have to understand that like in JavaScript, ${value1 && value2} returns value1 if it is falsy (e.g. false, or an empty string), otherwise it returns value2:
<ul data-sly-list="${items}" class="${condition1 && 'selected'}">
<li class="${condition2 && 'selected'}">
Lots of other markup here
</li>
</ul>
As said, in both examples the class attribute will be removed altogether if the corresponding condition is false.
What Gabriel has said is entirely correct. I did want to add a "gotcha" to look out for though. For the record, I was running into the exact same problem where, in a Sightly template, I wanted to toggle the presence of an input element's "checked" attribute based on a boolean value. In my case this boolean value was coming from the backing Use class.
After about 3-4 hours and being on the verge of pulling my hair out I finally realized that the boolean value I was relying on for toggling the "checked" attribute was ultimately being set in the activate method of a Sling service I wrote to back the work I'm doing. While everything else was logically correct, because I was setting this boolean in activate() and then retrieving it via a getter as needed, it would only ever have its value updated on bundle activation meaning my view would essentially lose state after the first refresh of the page.
I know it's silly but I wanted to call it out since it's relevant here and it may help someone not have to lose some hair like I did...

Extbase add calculated field in query/repository

I have a normal extbase extension.
In the controller I get all my records:
$stuff = $this->jobRepository->findAll();
$this->view->assign('stuff', $stuff);
and in the template I display them:
<f:for each="{stuffs}" as="stuff">
{stuff.title} <br />
{stuff.category}
</f:for>
Now I need a new field stuff.isnew with the value 1 if the record is the newest by category.
An SQL-Statement for that would look like:
SELECT j2.isnew, j.* FROM `tx_stuff_domain_model_stuff` as j
left join
(SELECT max(crdate) as crdate, category, 1 as isnew FROM `tx_stuff_domain_model_stuff` group by category) as j2
on j.crdate=j2.crdate and j.category=j2.category
(If I have to write my own Query I will have to check deleted, hidden, starttime, endtime to check if the record is active right now)
My question now is, what is the cleanest/best way to add that to my extension?
My solution would be to implement the method findAll in the job repository. It would first use the method findAll from the parent class to get all jobs. The usual constraints will already be applied. Then it would walk the resulting jobs and mark the newest one.
It should also be possible to create a custom query to reach you goal, but that would probably be a bite more work.
Actually you don't need to use additional query at all... while it's always first record which is new, you can just use an iterator of for ViewHelper:
<f:for each="{stuffs}" as="stuff" iteration="iterator">
<f:if condition="{iterator.isFirst}">THIS ONE IS BRAND NEW STUFF! </f:if>
{stuff.title} <br />
{stuff.category}
</f:for>

ASP.NET MVC 2: What Model property datatype will autobind an html select (DDL) with multiple selections?

The customer has a Model property that requires a comma separated list of selected options. We present their select list (DDL) as a multi-choice drop down.
What would the property datatype look like that would autobind multi-selections in the client side HTML select (DDL)?
The select posts data like this:
myOptions=Volvo&myOptions=Mercedes&myOptions=Audi
And we want to automagically bind it back to some property:
IList<string> CarChoices {get;set;}
So the POST action method parameter would be (Carform myForm)
which would have myForm.CarChoices which includes a List of the three selected cars?
I might be misunderstanding what you're trying to accomplish but I think this post from Phil Haack describes how to do what you're attempting to do in a clean way: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Sometimes it is just easier to get your hands dirty and work with the HTML. I suggest doing something like this:
<select multiple>
<% foreach(var item in Model){ %>
<option value="<%= item.ID %>"><%= item.Description %></option>
<% } %>
</select>
obviously your model is your collection. You can also use the ViewData["Whatever"] object to pass data as well, your choice.

jQuery ajaxSubmit(): ho to send the form referencing on the fields id, instead of the fields name?

im pretty new to jQuery, and i dont know how to do that, and if it can be done without editing manually the plugin.
Assume to have a simply form like that:
<form action="page.php" method="post">
Name: <input type="text" name="Your name" id="contact-name" value="" />
Email: <input type="text" name="Your email" id="contact-email" value="" />
</form>
When you submit it, both in 'standard' way or with ajaxSubmit(), the values of the request take the label of the field name, so in the page.php i'll have:
$_POST['Your name'];
$_POST['Your email'];
Instead i'll like to label the submitted values with the id of the field:
$_POST['contact-name'];
$_POST['contact-email'];
Is there a way to do that with jquery and the ajaxsubmit() plugin?
And, maybe, there is a way to do it even with the normal usage of a form?
p.s: yes, i know, i could set the name and id attributes of the field both as 'contact-name', but how does two attributes that contain the same value be usefull?
According to the HTML spec, the browser should submit the name attribute, which does not need to be unique across elements.
Some server-side languages, such as Rails and PHP, take multiple elements with certain identical names and serialize them into data structures. For instance:
<input type="text" name="address[]" />
<input type="text" name="address[]" />
If the user types in 1 Infinite Loop in the first box and Suite 45 in the second box, PHP and Rails will show ["1 Infinite Loop", "Suite 45"] as the contents of the address parameter.
This is all related to the name attribute. On the other hand, the id attribute is designed to uniquely represent an element on the page. It can be referenced using CSS using #myId and in raw JavaScript using document.getElementById. Because it is unique, looking it up in JavaScript is very fast. In practice, you would use jQuery or another library, which would hide these details from you.
It is reasonably common for people to use the same attribute value for id and name, but the only one you need to care about for form submission is name. The jQuery Form Plugin emulates browser behavior extremely closely, so the same would apply to ajaxSubmit.
It's the way forms work in HTML.
Besides, Id's won't work for checkboxes and radio buttons, because you'll probably have several controls with the same name (but a different value), while an HTML element's id attribute has to be unique in your document.
If you really wanted, you could create a preprocessor javascript function that sets every form element's name to the id value, but that wouldn't be very smart IMHO.
var name = $("#contact-name").val();
var email = $("#contact-email").val();
$.post("page.php", { contact-name: name, contact-email: email } );
This will let you post the form with custom attributes.