[Stactic type checking] - The variable [issue] is undeclared.

Pradeep Kumar February 12, 2018

We are getting below error. can someone please help me on thisErrors.png

1 answer

1 accepted

1 vote
Answer accepted
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2018

Where do you type the script? The issue variable is only available for post functions, validators, conditions. It is not available in Script Console, Listeners, behaviours.

Pradeep Kumar February 12, 2018

We are declaring in Script Listener and if we are not able to  find out issueTypename .

 

Below is the my code.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.bc.issue.IssueService.CreateValidationResult;
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.issue.IssueInputParametersImpl;
import groovy.json.*;
import com.atlassian.jira.issue.Issue;

def MutableIssue issueObject = (MutableIssue) issue
def MutableIssue parentIssue = issueObject.getParentObject() as MutableIssue

/* Set debugging level */
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug(parentIssue)

/* Initialize global objects */
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueTypeName = issue.issueTypeObject.name

log.debug("LISTENER CPI Wiki Markup==> STARTED")

if (issueTypeName == "Payment Inquiry" || issueTypeName == "Operational Inquiry" || issueTypeName == "Policy Inquiry" ) {
/* Retrieve Comment data */

//def actualComments = ComponentAccessor.getCommentManager().getComments(issueObject)
def actualComments = ComponentAccessor.getCommentManager().getLastComment(issueObject)

log.debug("Process each comment")
actualComments.each {
//def FromEmail = it.getUpdateAuthorFullName()
def FromEmail = actualComments.getAuthorApplicationUser().getEmailAddress()

log.debug("****** getAuthorApplicationUser FromEmail "+ FromEmail)

if(FromEmail != "")
{
def FromEmail1 = actualComments.getUpdateAuthorFullName()
//def FromEmail = it.getAuthorApplicationUser().getEmailAddress()

log.debug("****** getAuthorApplicationUser "+actualComments.getAuthorApplicationUser())

// FromEmail = it.getAuthorApplicationUser().toString()

log.debug("****** getAuthorApplicationUser FromEmail "+ FromEmail1)
// log.debug ("FromEmail.value: " + FromEmail.value)

def InternalCommunications = customFieldManager.getCustomFieldObjects(issueObject).find {it.name == "Internal Communications"}
def AllCommunication = customFieldManager.getCustomFieldObjects(issueObject).find {it.name == "AllCommunication"}
IssueChangeHolder localFieldChangeHolder = new DefaultIssueChangeHolder()


def newStr = "\n From:" + FromEmail1 + "\n Date and Time:" + actualComments.getCreated() + "\n" +" Body: " + actualComments.getBody() +"\n-----------------------------------------------------------------------------------------------------\n"


log.debug ("newStr: " + newStr)
def newStr1 = newStr
def newStr2 = newStr
def InternalCommunicationOldValue = issueObject.getCustomFieldValue(InternalCommunications)
def ii =InternalCommunicationOldValue.toString()
def OICV = newStr1.toString()
log.debug("New Internal Comments" + OICV)

if(InternalCommunicationOldValue && !ii.contains(OICV))
{
newStr1 = newStr.toString()+InternalCommunicationOldValue
InternalCommunications.updateValue(null, issueObject, new ModifiedValue(issueObject.getCustomFieldValue(InternalCommunications), newStr1), localFieldChangeHolder)
}

log.debug("InternalCommunicationOldValue" + InternalCommunicationOldValue)
if (InternalCommunicationOldValue == null)
{
InternalCommunications.updateValue(null, issueObject, new ModifiedValue(issueObject.getCustomFieldValue(InternalCommunications), OICV), localFieldChangeHolder)
}
// InternalCommunications.setForeground(Color.white)
log.debug("COMPLETED Updating Internal Communication field")

def AllCommunicationOldValue = issueObject.getCustomFieldValue(AllCommunication)
def ac =InternalCommunicationOldValue.toString()
def OACV = newStr2.toString()



if(AllCommunicationOldValue && !ac.contains(OACV))
{
newStr2 = newStr.toString()+AllCommunicationOldValue
AllCommunication.updateValue(null, issueObject, new ModifiedValue(issueObject.getCustomFieldValue(AllCommunication), newStr2), localFieldChangeHolder)
}
log.debug("AllCommunicationOldValue" + AllCommunicationOldValue)
if(AllCommunicationOldValue== null)
{
AllCommunication.updateValue(null, issueObject, new ModifiedValue(issueObject.getCustomFieldValue(AllCommunication), OACV), localFieldChangeHolder)
}
log.debug("COMPLETED Updating AllCommunication field")

}
log.debug("Completed processing all comments")


}
}
event.issue.store()

log.debug("LISTENER CPI Wiki Markup==> COMPLETED for " + issueTypeName)

/* Re-set debugging level */
log.setLevel(org.apache.log4j.Level.WARN)

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2018

If you write a listener then you should change issue to event.issue

Like # people like this
Pradeep Kumar February 12, 2018

Now I am getting below error.

Errors1.png

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2018

where do you type the script? Could you include other parts of your screen to the screenshot?

Pradeep Kumar February 12, 2018

Thank you very much, now I am not able to see errors after updating event.issue but I ma seeing below warning message 

I am updating script under script Listeners screen.

Errors2.png

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2018

It will work anyway. It is just a warning about deprecated API. 

Try to change event.issue.issueTypeObject.name to event.issue.issueType.name

Pradeep Kumar February 12, 2018

Thank you.

 

What about event.issue.store() ?

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 12, 2018

I do not think you need it at all. You can remove it.

david-guild September 24, 2019

@Alexey Matveev 

So that solved the problem with an event listener ... you get the context from event.issue.

But what about a post function?  Where "issue" is in context, when it running, but not in context from the IDE perspective:

So a line like this:

def cfThisFieldValue = issue.getCustomFieldValue(cfThisField)

Issue has a warning.

How do you fix that? 

Like Sekhar Chandra likes this
david-guild December 13, 2019

Well, since somebody liked my post, I will answer my question :-)

In Intellij, put your cursor on "issue".  Wait 1-2 seconds till a yellow exclamation mark appears.

From the drop down list, choose "Add Dynamic Property 'issue'"

In the property type box type (or navigate/select):

"com.atlassian.jira.issue.IssueImpl"

That should fix the issue.  It turns out, that when you let the plugin copy the code for you, it also handles this in the background.

So, if you create something from scratch, then you have to add the dynamic typing yourself.

At least that is how I solved this issue.  This is actually a little bit of a pain in the rear, as I don't do anything with the IntelliJ plugin anymore, so all new files need to have this done if I don't want to "import" packages that are already available in scope.

Kalyan Kumar Das December 30, 2019

@david-guild / @Alexey Matveev '/ @Pradeep Kumar 

Hi All,

Please help me on this. I am stuck for 2 day

The point is

1. I am unable to restrict for update the value of an issue at the time of issue update event.   

2. Instead of hard coded value for key, i want to use dynamic code on script-runner. Means if the ticket is subtask issue and project will be the same. 

 

Here the script is provided below and the requirement is duedate value for Task IssueType should be greater than the Duedate value for n number of subtask IssueType : 

 

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import groovy.transform.BaseScript
import com.onresolve.scriptrunner.runner.customisers.ContextBaseScript
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.opensymphony.workflow.InvalidInputException

//

@BaseScript ContextBaseScript script

Issue issue = getIssueOrDefault("TEST-258")

if(issue.parentObject.dueDate.compareTo(issue.getDueDate()) >= 0)
{
UserMessageUtil.success("Due Date Success"+issue.dueDate)
log.debug(issue.getDueDate())
}
else
{
UserMessageUtil.success('Please enter the Subtask duedate that is less than Parent-Task Due-Date ')
//throw new InvalidInputException("Input not valid")
}

 

The problem is that  when the condition fails then the date will be update first and next the pop up alert is raised. I am unable to restrict the due date update.    Also suggest for  second point. 

TEST-258.jpgDue_Date_code_Listener.jpgTEST-247.jpg

Suggest an answer

Log in or Sign up to answer