# Write a function

**URL:** <https://forum.dimagi.com/t/write-a-function/3029>\
**Category:** Users\
**Created:** [October 18, 2016, 7:52am UTC](https://forum.dimagi.com/t/write-a-function/3029 "2016-10-18T07:52:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sunrise](https://dimagi.b-cdn.net/letter_avatar_proxy/v4/letter/s/9fc29f/32.png) [@Sunrise](https://forum.dimagi.com/u/Sunrise)\
**Post date:** [October 18, 2016, 7:52am UTC](https://forum.dimagi.com/t/write-a-function/3029/1 "2016-10-18T07:52:08Z")

</div>

hi all

??? where can i write a new function then use it in any element.

best

---

<div class="post-metadata">

**Author:** ![Robin\_Selwitz](https://dimagi.b-cdn.net/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@Robin\_Selwitz](https://forum.dimagi.com/u/Robin_Selwitz)\
**Post date:** [October 18, 2016, 3:32pm UTC](https://forum.dimagi.com/t/write-a-function/3029/2 "2016-10-18T15:32:56Z")

</div>

Hi Rohlat,

Can you elaborate on what specifically you're trying to do with this  
function?  
You may find the following article helpful: CommCare Functions  
[https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions](https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions)

Best,

Robin Selwitz

> **···**
>
> On Tue, Oct 18, 2016 at 3:52 AM, Sunrise wrote:
> 
> > hi all
> > 
> > ??? where can i write a new function then use it in any element.
> > 
> > best
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "commcare-users" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [commcare-users+unsubscribe@googlegroups.com](mailto:commcare-users+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Sunrise](https://dimagi.b-cdn.net/letter_avatar_proxy/v4/letter/s/9fc29f/32.png) [@Sunrise](https://forum.dimagi.com/u/Sunrise)\
**Post date:** [October 19, 2016, 3:31am UTC](https://forum.dimagi.com/t/write-a-function/3029/3 "2016-10-19T03:31:30Z")

</div>

Hi

I want generat Unique ID for each case so i want write a function in Javascrept then use it in any element in commcare ... can you help me in this issue ?

---

<div class="post-metadata">

**Author:** ![Phillip\_Mates](https://dimagi.b-cdn.net/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@Phillip\_Mates](https://forum.dimagi.com/u/Phillip_Mates)\
**Post date:** [October 19, 2016, 12:54pm UTC](https://forum.dimagi.com/t/write-a-function/3029/4 "2016-10-19T12:54:43Z")

</div>

Hello,

Unfortunately CommCare does not support using javascript to define form  
logic. Currently there is also no way define your own custom XPath function  
within the Form Builder on CommCareHQ.

If you want a unique id but don't care about the exact format, then I would  
suggest using uuid()  
[https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-uuid](https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-uuid)  
.  
If you have a particular id format in mind, you can string together the  
logic to construct that id using a series of hidden values that perform the  
necessary string / numeric manipulations. I like to put all those hidden  
values in the same group so that it is easy to copy that logic between  
forms.

> **···**
>
> -- Phillip Mates Mobile Engineer at Dimagi

---

<div class="post-metadata">

**Author:** ![Sunrise](https://dimagi.b-cdn.net/letter_avatar_proxy/v4/letter/s/9fc29f/32.png) [@Sunrise](https://forum.dimagi.com/u/Sunrise)\
**Post date:** [October 20, 2016, 3:43am UTC](https://forum.dimagi.com/t/write-a-function/3029/5 "2016-10-20T03:43:27Z")

</div>

Thank you .. i will try it
