VMware Cloud Community
zdingelit
Contributor
Contributor

apply a function to 2 attributes ?

Hi,
I am newbie in Javascript Dev/vRO
I need some help to use this script.
 
attrib1 variable contains /Fox/Sdf/Acd/Opf/ > after formatting the output is SDF_ACD_OPF
attrib2 variable contains /Fox/Aqsz/Dre/Pqf/ > after formatting the output is AQSZ_DRE_PQF
The code works for on attribute but I want to create a function that is used on the 2 attributes and store the result in attrib1 and attrib2
 
How to apply the function to each attribute ?
here is my code :
 str = attrib1  function formatEntity (str) { // delete /Alpha/Dr/Oerd/ // delete Alpha/Aqs/ str = str.replace( /\/Alpha\/Dr\/Oerd\/?/gi,""); str = str.replace( /\/Alpha\/Aqs\/?/gi,"");  //replace           ----/Fox/Sdf/---- by Sdf/ str = str.replace( /\/Fox\/Sdf\//g,"Sdf/"); //replace          ----/Fox/Apcr/---- by Apcr/ str = str.replace( /\/Fox\/Apcr\//g,"Apcr/"); //replace          ----/Fox/Aqsz/---- by Aqsz/ str = str.replace( /\/Fox\/Aqsz\//g,"Aqsz/"); //replace          ----/Oxa/Abdr/---- by  Abdr/ str = str.replace( /\/Oxa\/Abdr\//g,"Abdr/"); // UpperCase the string str = str.replace( /\w/gi, function (x) {     return x.toUpperCase(); }); // delete the last  "/" str = str.replace(/\/$/g, ""); // replace remain "/" by  _ str = str.replace(/\//g, "_"); System.log(str) }
Merci !
0 Kudos
6 Replies
iiliev
VMware Employee
VMware Employee

Hi,

Your function lacks the 'return' statement that will return the formatted value.

Something like the following should work:

// Original attributes

attrib1 = "/Fox/Sdf/Acd/Opf/";

attrib2 = "/Fox/Aqsz/Dre/Pqf/";

// Format and assign back

attrib1 = formatEntity(attrib1);

attrib2 = formatEntity(attrib2);

// Print formatted attributes

System.log(attrib1);

System.log(attrib2);

function formatEntity (str) {

  // Your original function code goes here...

  // ...

  // At the end, return the formatted result as computed by your function

  return str;

}

0 Kudos
zdingelit
Contributor
Contributor

Woow thank you very much for your reactivity !

is it possible to store all the regex part in a resource element and call it ?

Thanks again !!!!

0 Kudos
iiliev
VMware Employee
VMware Employee

The right approach is to store the formatEntity function as a vRO scripting action (not as resource element), which will make it available to be called by other scripting actions / workflows.

0 Kudos
zdingelit
Contributor
Contributor

I think it will simplify regex modification!
0 Kudos
zdingelit
Contributor
Contributor

Thank you very much ! I will try the solution.
0 Kudos
iiliev
VMware Employee
VMware Employee

Sure, you can extrnalize the regular expression and store it in resource element (or configuration element). But be aware that:

  • reading the value stored in the resource or configuration element require some script code to be written
  • once you fetch the stored regular expression, you won't be able to use it using the same in-place syntax as before. Instead, you should use it together with the RegExp class
0 Kudos