Need to upload 168 different attachments - 1 each to 168 different records

Arka Banerjee1
Tera Contributor

Hi Team,

 

I need to upload 168 attachments, 1 each across 168 different records. Can you please help with how I can upload all of these in bulk.

 

Please note that there's no attachment field in the records, so all would go to sys attachment table itself. 

 

Could you please help with how I can upload them.

 

@Ankur Bawiskar 

12 REPLIES 12

Dr Atul G- LNG
Tera Patron

https://www.servicenow.com/community/itsm-forum/i-want-to-upload-attachments-through-transform-maps-...

 

@Arka Banerjee1 

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron

@Arka Banerjee1 

Custom solution (I haven't tested this though)

1) create a csv file with these columns

AnkurBawiskar_1-1783420940649.png

 

2) then write powershell script and have admin credentials, instance endpoint ready

I took help of AI to get the powershell script

Note: I assume all files are in path C:\Users\Arkesh\Downloads

attachment_map.csv file is stored in the C:\Temp folder

$instance   = "https://yourinstance.service-now.com"
$username   = "admin"
$password   = "your_password"
$csvPath    = "C:\Temp\attachment_map.csv"
$baseFolder = "C:\Users\Arkesh\Downloads"

$pair = "$username`:$password"
$encodedCreds = [Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))

$headers = @{
    Authorization = "Basic $encodedCreds"
    Accept        = "application/json"
}

$rows = Import-Csv $csvPath

foreach ($row in $rows) {
    $tableName = $row.table_name.Trim()
    $sysId     = $row.table_sys_id.Trim()
    $fileName  = $row.file_name.Trim()
    $filePath  = Join-Path $baseFolder $fileName

    if (-not (Test-Path $filePath)) {
        Write-Host "File not found: $filePath" -ForegroundColor Yellow
        continue
    }

    $encodedFileName = [System.Uri]::EscapeDataString($fileName)
    $uri = "$instance/api/now/attachment/file?table_name=$tableName&table_sys_id=$sysId&file_name=$encodedFileName"

    try {
        $response = Invoke-RestMethod `
            -Uri $uri `
            -Method Post `
            -Headers $headers `
            -InFile $filePath `
            -ContentType "application/octet-stream"

        Write-Host "Uploaded: $fileName -> $tableName / $sysId" -ForegroundColor Green
    }
    catch {
        Write-Host "Failed: $fileName -> $tableName / $sysId" -ForegroundColor Red
        Write-Host $_.Exception.Message -ForegroundColor Red
    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Danish Bhairag2
Tera Sage

Hi @Arka Banerjee1 ,

 

If there is any naming convention on the files via which we can identify which files needs to go to which record then its possible via script in servicenow. Can u tell me or show me the snip of the files n the record (1 is enough) i can share the script with u.

 

Thanks,

Danish