VMware Cloud Community
zdickinson
Expert
Expert
Jump to solution

Use Workflow Input in Attribute Value

Good morning, I have a workflow that powers off a VM and then powers it back on.  It sends an e-mail if it does not power on properly.  Is it possible to insert the VM name that was the workflow input into the value of either the subject or content of the e-mail?

Untitled.png

Something like %vmname% Did Not Reboot.

Thank you, Zach.

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

The correct one is

content = vm.name + " did not reboot";

as it concatenates two strings - the property 'name' of the object of type VirtualMachine (the type of the property is string) with another string. The result of this operation is again a string value, which is assigned to the attribute 'content' which is also of type string.


This one

content = vm + " did not reboot";

is incorrect as it tries to concatenate an object of type VirtualMachine with a string. What should be the effect of such operation?


You are getting this reference error because you haven't added the input parameter 'vm' on the IN tab inside the scriptable task. The code in a scriptable task can operate only on attributes/parameters of the workflow that are explicitly referenced on either IN or OUT tab; the non-referenced ones are simply not visible inside the scriptable task.


When you select existing attribute/parameter on IN tab, you are not redefining it; you are marking this attribute/parameter as visible/accessible within the scriptable task. When you select existing attribute/parameter on OUT tab, you are marking this attribute/parameter as keeping any changes of its value after you leave the scriptable task. In your example, you have included 'content' attribute on both IN and OUT tabs, which means you can use it in Javascript code inside scriptable task and that the changes you make to it (assigning the new value vm.name + " is powered off") will update the actual content value and won't be lost after you leave the scriptable task element.

View solution in original post

0 Kudos
10 Replies
shifter
Enthusiast
Enthusiast
Jump to solution

Hi,

You could try adding a scripting step in there that has this in it:

subject = name + " Did Not Reboot"

(assuming that name is an attribute value in your workflow)

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

One option would be to find where the subject or content attributes are used in the workflow, and add some code there to construct the desired string using the values of the attribute and the input parameter.

BTW, when referencing attributes/parameters, the syntax is not %attribute% but #attribute. This syntax, however, doesn't work everywhere, just in places where OGNL expression can be provided/evaluated.

0 Kudos
zdickinson
Expert
Expert
Jump to solution

Thank you for the reply.  Would this be creating a variable "subject" that can be used elsewhere in the work flow?  My apologies, very new to automation in general.  Thank you, Zach.

0 Kudos
zdickinson
Expert
Expert
Jump to solution

Thank you for the reply.  Can you elaborate on your explanation a bit?  I am new to automation.  Thank you, Zach.

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

It depends on how your workflow is using content and subject attributes shown in the first post to send email. It can be a new variable, or some Javascript code to update the value of the existing attributes, or a new attribute whose value you will set at runtime.

Could you attach your current workflow?

0 Kudos
zdickinson
Expert
Expert
Jump to solution

Good morning, the workflow is attached.  Thank you, Zach.

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

OK, so perhaps the easiest way to send emails with customized content/subject is to add one additional Scriptable task element. All red lines that currently navigate to Send notification workflow should be redirected to this new Scriptable task element, and the output link of this Scriptable task should go to Send notification workflow.

Open the new scriptable task for edit, and add content/subject/vm (and other attributes/inputs if desired) as IN parameters. Add subject/content as OUT parameters. Write some Javascript to construct the new values of subject/content attribute; for example

content = vm.name + " did not reboot";

That should be enough to send customized emails.

0 Kudos
zdickinson
Expert
Expert
Jump to solution

Interesting idea.  Might I need to re-define all the attributes for the workflow in the scrip table task?  Or perhaps I'm calling the vm.name incorrectly.  I tried both vm.name and vm.

content = vm + " did not reboot"  I have that as an action in the script tab.

I have attached the updated workflow as well.  Thank you, Zach.

[2016-05-25 11:50:41.310] [E] Error in (Workflow:Reboot, reset if not successful / Scriptable task (item0)#0) ReferenceError: "vm" is not defined.

[2016-05-25 11:50:41.342] [E] Workfow execution stack:

***

item: 'Reboot, reset if not successful/item0', state: 'failed', business state: 'null', exception: 'ReferenceError: "vm" is not defined.'

workflow: 'Reboot, reset if not successful' (254d4521-37e0-44bb-b2eb-a77d858dd98f)

|  'attribute': name=smtpHost type=string value=relay.rpionline.com

|  'attribute': name=pollingRate type=number value=5.0

|  'attribute': name=fromName type=string value=GVvOrchestrator1

|  'attribute': name=fromAddress type=string value=gvvorchestrator1@rpionline.com

|  'attribute': name=toAddressList type=Array/string value=#{#string#netadminalerts@rpionline.com#}#

|  'attribute': name=TimeOut_Shutdown type=number value=2.0

|  'attribute': name=TimeOut_Tools type=number value=2.0

|  'attribute': name=content type=string value=Server failed to reboot after updating.

|  'attribute': name=subject type=string value=Server Reboot

| 'input': name=vm type=VC:VirtualMachine value=dunes://service.dunes.ch/CustomSDKObject?id='gvvcenter2.rpionline.com/vm-213'&dunesName='VC:VirtualMachine'

|  'output': name=actionResult type=VC:Task value=null

|  'output': name=errorCode type=string value=Timeout: Timout waiting for tools to be started on a running OS (Dynamic Script Module name : vim3WaitToolsStarted#28)

*** End of execution stack.

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

The correct one is

content = vm.name + " did not reboot";

as it concatenates two strings - the property 'name' of the object of type VirtualMachine (the type of the property is string) with another string. The result of this operation is again a string value, which is assigned to the attribute 'content' which is also of type string.


This one

content = vm + " did not reboot";

is incorrect as it tries to concatenate an object of type VirtualMachine with a string. What should be the effect of such operation?


You are getting this reference error because you haven't added the input parameter 'vm' on the IN tab inside the scriptable task. The code in a scriptable task can operate only on attributes/parameters of the workflow that are explicitly referenced on either IN or OUT tab; the non-referenced ones are simply not visible inside the scriptable task.


When you select existing attribute/parameter on IN tab, you are not redefining it; you are marking this attribute/parameter as visible/accessible within the scriptable task. When you select existing attribute/parameter on OUT tab, you are marking this attribute/parameter as keeping any changes of its value after you leave the scriptable task. In your example, you have included 'content' attribute on both IN and OUT tabs, which means you can use it in Javascript code inside scriptable task and that the changes you make to it (assigning the new value vm.name + " is powered off") will update the actual content value and won't be lost after you leave the scriptable task element.

0 Kudos
zdickinson
Expert
Expert
Jump to solution

Bang-a-rang!  Thank you for the patience with a new comer.  Great explanation, makes sense!  The e-mail now includes the name of the VM.

0 Kudos