Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Save csv file to shared folder using mid server

Dazler
Mega Sage

Hi,

 

I have been task with creating a csv in ServiceNow and saving it to a shared folder.  I know that I can use the scheduled export, but that only allows me to save it to a file path relative to the Mid Server directory and also I need specific headers (which I will create through script).  I want to use the mid server to save this file to that shared folder, but I am not sure how to go about that.

 

I am wondering if I should create a custom PowerShell to drop that file in that folder.

 

Does anyone have any suggestions?

4 REPLIES 4

AshishKM
Kilo Patron
Kilo Patron

Hi @Dazler 

You can configure the "Export Sets" for file transfer on MID Server path. 

 

Create a Export Definition ( What data to export , you can select the columns)

Create a Export Target ( MID Server Name and Path configure )

Create a Export Sets ( configure the export definition & target here along with fileName & Format )

 

Refer my reply here :

https://www.servicenow.com/community/developer-forum/how-to-export-txt-file-in-midserver-folder/td-p...

 

Please mark helpful and correct answer if it helps

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

@AshishKM 

 

Thank you for replying back but I can't use this because I needed special headers for my CSV that are not on the table.  I can't use the table label for what i need.

Sandhya V
Tera Contributor

Hi ,
Did you get solution to place the file from mid server export folder to shared drive?

Hi @Sandhya V,

 

I created an action that passed inputs of the Attachment Sys id (attachment that will be saved) and Target Folder Path (where the file is being saved)

 

1st: set inputs on action

 

Dazler_0-1722261105392.png

2nd: Set a script step to convert the csv to a GlideBase64 encoding

 

Dazler_1-1722261327327.png

 

3rd:  Set up a PowerShell that will take the encoded string and save it to the target folder path.

 

Dazler_2-1722261607430.png

PowerShell script:

 

function Convert-StringToBinary {
	[CmdletBinding()]
	param (
		[string] $EncodedString,
		[string] $FilePath = ('{0}\{1}' -f $env:TEMP, [System.Guid]::NewGuid().ToString())
	)

	try {
		if ($EncodedString.Length -ge 1) {
		     # decodes the base64 string
		    $ByteArray = [System.Convert]::FromBase64String($EncodedString);
		    [System.IO.File]::WriteAllBytes($FilePath, $ByteArray);
		}
	}
	catch {
	}
}

Convert-StringToBinary -EncodedString $Base64String -FilePath $TargetFileName

sleep 5

try{
 Get-Item -Path $TargetFileName -ErrorAction Stop
 Write-Output "File has been saved."
}
catch{
 Write-Output "File was not saved | $($error[0].exception.message)"
}

 

Hopefully this will help.