1
0
-1

I'm trying to sync a lot of fields and I would like a more general solution in the code than writing the full custom field name at multiple places. I want to make it less likely to make spelling errors and copy-paste errors. Is it possible to use a variable instead of directly writing the custom field name string?


I would like to do something like the following but : 

String[] fieldsToSync = ["Platform", "Division", "Name"]

fieldsToSync.each{String field ->
	replica.customFields[field] = issue.customFields[field]?.value
}

instead of duplicating fields like this: 

replica.customFields."Platform" = issue.customFields."Platform"?.value
replica.customFields."Division" = issue.customFields."Division"?.value
replica.customFields."Department" = issue.customFields."Department"?.value

    CommentAdd your comment...

    2 answers

    1.  
      1
      0
      -1

      Hi again, 

      Yes you were right. This is my solution to make it as generic as possible. I this case I also check which server on which I'm running the script - in that way I can use exactly the same script on both servers. And I don't need to be dependant on Custom field names, but rely on custom field ID:s. 


      Outgoing sync:

      /*****************************************
      	 SELECT ENVIRONMENT (internal or external depending on the URL)
      ******************************************/
      @Field def isInternalServer
      def baseurl = com.atlassian.jira.component.ComponentAccessor.getApplicationProperties().getString("jira.baseurl")
      @Field def isInternalServer
      if (baseurl.contains("internal-jira"))
      	isInternalServer = true
      else if (baseurl.contains("external-jira"))
          isInternalServer = false
      else
          throw new Exception("Base URL not found in Exalate script")
      
      /**************************************
      	Text fields
      **************************************/
      //field name, custom field ID on internal server, custom field ID on external server.
      def textFields = [
        [fieldName:"Platform", internalId:"11086", externalId:"10172"],
        [fieldName:"Division", internalId:"10701", externalId:"10147"],
        [fieldName:"Name", internalId:"11100", externalId:"10170"]
      ]
      
      textFields.each{ field ->
      	def fieldId = isInternalServer ? field.internalId : field.externalId
      	
      	//field name will act as key for the replica object
      	replica.customFields[field.fieldName] = issue.customFields[fieldId]
      }
      
      

      Incoming sync:




      /**************************************
      	Text fields
      **************************************/
      //field name, custom field ID on internal server, custom field ID on external server. 
      def textFields = [
        [fieldName:"Platform", internalId:"11086", externalId:"10172"],
        [fieldName:"Division", internalId:"10701", externalId:"10147"],
        [fieldName:"Name", internalId:"11100", externalId:"10170"]
      ]
      
      textFields.each{ field ->
      	def fieldId = isInternalServer ? field.internalId : field.externalId
      	
      	issue.customFields[fieldId].value = replica.customFields[field.fieldName]?.value
      }
      
      
      CommentAdd your comment...
    2.  
      1
      0
      -1

      Yes - it should be.

      customFields is a standard groovy array of customField objects.

      Can you give it a try and let us know?


        CommentAdd your comment...