Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Hey, i am trying to make an automation on emails.

Preben Tjemsland January 15, 2020

I have a object "System" where an admin should get email notice when a "System" is created and when only the attribute "Type" is changed. I´m struggling with the last one.

I have tried with "objectType = System AND anyAttribute = Type" and a couple other variants. 

 

1 answer

1 accepted

1 vote
Answer accepted
Yinon Negev
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.
January 15, 2020

Hi Preben.

        We assume that you are using an Automation Rule to trigger the email notifications.

For the Object Created event - it is enough to have the IQL: objectType=System

For Object Updated, there is no direct way (e.g. IQL) to know which Attribute was changed from / to which Value. You can achieve this by using the Action: Execute a Groovy Script, where you can query the Updated Object for its History, and send an email if required.

While Scripting is not within the Scope of our support, we can provide you with some scripts you can use as base:

This script will "Print" the History to the Insight Script Console's log:

import com.atlassian.jira.component.ComponentAccessor
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
def objectBean = (ObjectBean)object
def objectFacadeApi = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade.class)
def objectHistoryList = objectFacadeApi.findObjectHistoryBean(objectBean.id) //Provide the Object id//
objectHistoryList.each { objectHistory ->
log.info("history= ${objectHistory.created} ${objectHistory.affectedAttribute} : ${objectHistory.oldValue} -> ${objectHistory.getNewValue()} : Actor= ${objectHistory.getActorUserKey()}") }
return true

You will need to adjust this script and add the logic which you require (e.g.  the Type Attribute was changed, and/or is now having a specific Value...)

For sending an Email from your script - see this example in our documentation.

Kind regards,

Yinon

Mindville Support

Preben Tjemsland January 15, 2020

Thank you for the fast response! Yeah the email for when creating a Object works. I will try to make the script for sending emails based on a field changing.

Preben Tjemsland January 16, 2020

Hi again, 

i managed to get a bit of the way, but i'm unsure about the way the history work. 

So now works in the way that for every time anything changes a mail is sent. My hope was that this would work.

if(objectHistory.affectedAttribute == "Type" && objectHistory.oldValue != objectHistory.newValue && objectHistory.newValue)


My issue is that whenever any change happens in JI all of the history is back. I would like to keep the history in JI, but clear it for the script if possible so that a mail is sent only when the "Type" is edited.

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.atlassian.jira.component.ComponentAccessor
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import groovy.util.logging.Slf4j


def typeErEndret = false


Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade")
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass)
String objectKey = "KEY-7373"
def insightObject = objectFacade.loadObjectBean(objectKey)
int objectId = insightObject.getId()

int attributeRef = 267
String attributeRefS = "Type"
def objectAttribute = objectFacade.loadObjectAttributeBean(objectId, attributeRefS)
def attributevalue = objectAttribute.objectId

def objectBean = (ObjectBean)object
def objectFacadeApi = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade.class)
def objectHistoryList = objectFacadeApi.findObjectHistoryBean(9087)
def i = 0
objectHistoryList.each { objectHistory ->
log.info("history= ${objectHistory.affectedAttribute} : ${objectHistory.oldValue} -> ${objectHistory.getNewValue()} : Actor= ${objectHistory.getActorUserKey()}")
if(objectHistory.affectedAttribute == "Type" && objectHistory.oldValue != objectHistory.newValue && objectHistory.newValue) {
typeErEndret = true
}
}
objectHistoryList.each { objectHistory ->
objectHistory.setOldValue("")
objectHistory.setNewValue("")
}

if (typeErEndret == true){
//Mail is sent
}
}

Preben Tjemsland January 17, 2020

I found a solution for this by using the time the change was created. 

 

if(objectHistory.affectedAttribute == "Type" && objectHistory.oldValue != objectHistory.newValue && objectHistory.newValue && objectHistory.getCreated() > Date.from(input.toInstant().minusSeconds(20))) 

 By checking for time created is larger than time now minus some time. (20 seconds are only used to have time to run the script from the script console) 

Giorgi Tsitskishvili July 20, 2022

@Preben Tjemsland  Hi, 

This solution you achieved by using groovy script?

can you add more details

Suggest an answer

Log in or Sign up to answer