Issue with syncing Asset Filed from Zendesk to Jira on Creation

I’ve build a script for my incoming Jira, It’s all working fine including asset filed being populated or mostly!

The issue I’m having is the asset fields (Direct API Method) is that on creation these get missed as the issue id doesn’t exist so I’ve used:

 if (firstSync) {

    issue.projectKey                           = "DCS"

    issue.type                                 = nodeHelper.getIssueType(replica.type?.name, issue.projectKey) ?: nodeHelper.getIssueType("Incident", issue.projectKey)

    issue.customFields."Link to ZenDesk".value = remoteIssueUrl

    issue.summary                              = replica.summary

    issue.description                          = replica.description

    issue.comments                             = commentHelper.mergeComments(issue, replica)
// Other custom field code

    // Trigger a second pass to handle Assets after the issue is indexed
    syncHelper.syncBackAfterProcessing()
    return 
}

Then the asset filed one of 3 I have

// --- Platform Sync (Direct API Method) ---
        try {
            def remoteVal = (replica.customFields."Org Platform"?.value?.value ?: replica.customFields."Org Platform"?.value)?.toString()
            def workspaceId = "7d677423-68d8-4038-87eb-2f9f745855db"

            if (remoteVal) {
                def assetMap = [
                    "platform-field_acorn"          : "37",
                    "platform-field_banking-live"   : "35",
                    "platform-field_voucherengine"  : "36",
                    "platform-field_not-applicable" : "845"
                ]

                def targetId = assetMap[remoteVal.trim()] ?: "845"
                
                // Construct the raw JSON payload Jira requires for Assets
                def jsonBody = """
                {
                "update": {
                    "customfield_11995": [
                    {
                        "set": [
                        {
                            "workspaceId": "${workspaceId}",
                            "id": "${workspaceId}:${targetId}",
                            "objectId": "${targetId}"
                        }
                        ]
                    }
                    ]
                }
                }
                """

                // Send the update directly to the Jira Issue API
                def response = httpClient.put("/rest/api/3/issue/${issue.key}", jsonBody)
                
                if (response.status != 204 && response.status != 200) {
                    log.error("Asset API Update Failed: ${response.body}")
                } else {
                    log.info("Successfully forced Asset update for ${issue.key}")
                }
            }
        } catch (Exception e) {
            // Add Error to the syncErrors List, so an internal comment can by added to the ticlet at the end of the sync
            //syncErrors.add("Automatic Sync Notification: Platform Sync failed. Error: ${e.message}")
            
            // Logs the error in the Exalate Admin Panel
            log.error("Automatic Sync Notification: Platform Sync failed. Error: " + e.message)
        }

But it fails every time on creation, if anyone can help please.

Regards
Matt
   

Hi Matthew,

From your description, this seems to be a timing issue during the firstSync phase.

At creation time, the Jira issue is not yet fully persisted in the database, so when you attempt to update the Assets field via the REST API (/issue/${issue.key}), it may not work reliably because the issue is not fully available yet. That’s why the same logic works on subsequent updates but not during creation.

In this case, it would be worth explicitly persisting the issue before triggering the sync back by using store(issue). For example:

if (firstSync) {
    issue.projectKey = "DCS"
    issue.type = nodeHelper.getIssueType(replica.type?.name, issue.projectKey) ?: nodeHelper.getIssueType("Incident", issue.projectKey)

    issue.customFields."Link to ZenDesk".value = remoteIssueUrl
    issue.summary = replica.summary
    issue.description = replica.description
    issue.comments = commentHelper.mergeComments(issue, replica)

    store(issue)
    syncHelper.syncBackAfterProcessing()
    return
}

The key point here is that store(issue) forces the issue to be created and committed in Jira before any further processing happens. Without this, the sync-back may be triggered while the issue is still in a transient state, which can lead to situations where the issue key/id is not yet usable for downstream operations like your direct REST API call.

Let me know if this helps or if you’re still seeing issues after trying this approach.

Thanks,
Dhiren

Hi Dhiren

I’ve tested this and it doesn’t seem the fire the second sync at all, so as you can see in my code there is a “issue.customFields.“Pending reason”?.value = “More info required”“ (It’s something I need to work out in the workflow) but that doesn’t update, so clearly the “syncHelper.syncBackAfterProcessing()” isn’t initiating the second sync

// 1. STOP syncing into Jira once Jira is Closed
def jiraStatus = (issue.status?.name ?: "").toString()

if (jiraStatus.equalsIgnoreCase("Closed")) {
    return
}

// Error Collection List
def syncErrors = []

if (firstSync) {
    issue.projectKey = "DCS"
    issue.type = nodeHelper.getIssueType(replica.type?.name, issue.projectKey) ?: nodeHelper.getIssueType("Incident", issue.projectKey)

    issue.customFields."Link to ZenDesk"?.value = remoteIssueUrl
    issue.summary = replica.summary
    issue.description = replica.description

    //store(issue)
    // IMPORTANT: Trigger a second pass to handle Assets after the issue is indexed
    syncHelper.syncBackAfterProcessing()
    return 
} 



// CRITICAL: Must be in this order
issue.attachments = attachmentHelper.mergeAttachments(issue, replica)
issue.comments = commentHelper.mergeComments(issue, replica)
issue.customFields."Pending reason"?.value = "More info required"

Hi @matthew.hickman

Apologies for the late response!

Let me update you that we are working on this and will post and update soon.

Thanks,

Sonal

Hi @matthew.hickman ,

Just curious here, can you try something like this?

def jiraStatus = (issue.status?.name ?: "").toString()
if (jiraStatus.equalsIgnoreCase("Closed")) {
    return
}

if (firstSync) {
    issue.projectKey = "DCS"
    issue.type = nodeHelper.getIssueType(replica.type?.name, issue.projectKey) ?:
                 nodeHelper.getIssueType("Incident", issue.projectKey)

    issue.customFields."Link to ZenDesk"?.value = remoteIssueUrl
    issue.summary = replica.summary
    issue.description = replica.description
    issue.customFields."Pending reason"?.value = "More info required"

    issue.attachments = attachmentHelper.mergeAttachments(issue, replica)
    issue.comments = commentHelper.mergeComments(issue, replica)
    return
}

issue.attachments = attachmentHelper.mergeAttachments(issue, replica)
issue.comments = commentHelper.mergeComments(issue, replica)
issue.customFields."Pending reason"?.value = "More info required"

Thanks, Dhiren