Simple renaming question

Post Reply
mlongo
Newbie
Posts: 11
Joined: Thu Nov 18, 2021 2:39 pm

Simple renaming question

Post by mlongo »

Hello, I am new to Switch and am learning about scripting within Switch. I have a very basic "tutorial" flow I made for myself where I am importing some XML and related PDF files. I know that I can leverage data within the XML to rename my PDF files. I did that and it works great. But I was trying to learn about renaming the PDF files by using scripting. We have the scripting and metadata modules installed.

I added the Rename Job tool to the flow and set the Action 1 to Act on = Filename proper and Replace by = Script Expression Defined. I then added what I thought was very simple code but the PDF filename refuses to change at all. I have some example of my code below.

First attempt:

Code: Select all

var orderid = job.getVariableAsString('[Metadata.Text:Path="/cXML/Request/JobTicketDetail/ItemOut/Extrinsic",Dataset="Xml",Model="XML"]');  
var sku = job.getVariableAsString('[Metadata.Text:Path="/cXML/Request/JobTicketDetail/ItemOut/ItemID/SupplierPartAuxiliaryID",Dataset="Xml",Model="XML"]');
// var pdfname += orderid + '_' + sku;
var pdfname = orderid + '_' + sku;
pdfname;
When that wasn't working I tried to just go simple with this:

Code: Select all

var pdfname = 'Testing';
return pdfname;
//pdfname;
In both cases and with each of the commented out sections attempted, I get absolutely nothing in terms of any name changing. Can someone let me know what I am doing wrong?
Thanks!
-Mike
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Simple renaming question

Post by jan_suhr »

Ok I understand if you want play with scripting but since you already have everything in the dataset you can just add the filename with variables from the present metadata.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
mlongo
Newbie
Posts: 11
Joined: Thu Nov 18, 2021 2:39 pm

Re: Simple renaming question

Post by mlongo »

jan_suhr wrote: Fri Dec 03, 2021 2:25 pm Ok I understand if you want play with scripting but since you already have everything in the dataset you can just add the filename with variables from the present metadata.
Yep, and I got that working perfectly fine.
But I want to understand using the scripting to accomplish basically the same thing. I may need that ability down the line.
Thanks!
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

Re: Simple renaming question

Post by freddyp »

In a script expression you NEVER return something, you just place the value (or the variable) on the last line of the script expression.

Code: Select all

var pdfname = 'Testing';
pdfname;
This is the correct method and it works. Do you have additional empty lines after the last line with "pdfname;"? If so, remove them and try again.
mlongo
Newbie
Posts: 11
Joined: Thu Nov 18, 2021 2:39 pm

Re: Simple renaming question

Post by mlongo »

freddyp wrote: Fri Dec 03, 2021 4:31 pm In a script expression you NEVER return something, you just place the value (or the variable) on the last line of the script expression.

Code: Select all

var pdfname = 'Testing';
pdfname;
This is the correct method and it works. Do you have additional empty lines after the last line with "pdfname;"? If so, remove them and try again.
Interesting. That might have been my issue. I went back to my scripts and just hit the delete key at the end of my script several hundred times (haha) and ran the flow again. This time it worked fine. I ran the first version that pulled metadata out and used it to name the PDFs.
Thank you for all of the feedback!
Post Reply