Unable to fetch attachmentUrl of content from draft branch

Enonic version: 6.12.1

I’m facing an issue when i’m trying to fetch attachmentUrl of content stored in draft using Id,

url: portal.attachmentUrl({ id: attachment.id, download: true ,type :'absolute'})

i’m getting 404 error ( content with id was not found in master branch)

is it possible to get attachmentUrl of content stored in draft branch ?

Url’s will be generated based on your context. if you are using the portal to render/execute your code the branch will be part of your url - simply replace /master/ with /draft/ and you should be good to go

If you want to create the attachment URL for a different branch than the one in the current context (based on requested URL as explained above), you have to wrap your call to attachmentUrl in a new context, and set the branch ‘draft’.
For this you should use contextLib.run:

var url = contextLib.run({branch: 'master'}, function () {
    return portal.attachmentUrl({id: attachment.id, download: true, type: 'absolute'});