Forums

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

Unable to set user picker custom field with user

vishal goyal May 11, 2018

Hello EveryOne,

 

I am having issue on adding a user to the user Picker Custom Field "Customer Watchers".

I am using 2 parts user information are fetched from ADFS.
Part 1. I am removing watchers based on certain mailID.(it is working fine)

Part 2. I need to add same user which are being removed as watchers  must be added in the "Customer Watchers" field.
My script do-not through any error but still its not adding  user to field.
Manually i am able to add same user. please help. Script is here

//Custom Field Access
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.type.EventDispatchOption
import org.apache.log4j.Category
import com.atlassian.jira.issue.watchers.IssueWatcherAccessor
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.user.util.UserUtil;
import org.apache.log4j.Logger;
 

Logger log = Logger.getLogger("Deletion of Watchers from the issue");
log.setLevel(org.apache.log4j.Level.DEBUG);
def logPrefix ="Deletion of Watchers from the issue" + issue.getKey() + "***********************************************************************************" ;

try{
 
    Locale en = new Locale("en");
    IssueWatcherAccessor iwa = ComponentAccessor.getComponent(IssueWatcherAccessor.class)
    def list =  iwa.getWatchers(issue,en);
    log.debug(logPrefix+  "All watchers are::" + list);   
    
    CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();    
    def cfCustomerWatchers = "Customer Watchers";
    cfCustomerWatchersObj = customFieldManager.getCustomFieldObjectByName(cfCustomerWatchers);
    
    for (i in list )
    {
        def user = i;
        def watcherUser = ComponentAccessor.getUserManager().getUserByKey(user.toString().split(":")[0]);
        def emailIDUser =  watcherUser.getEmailAddress();
       
            

        if(!emailIDUser.contains("abc.com") && !emailIDUser.contains("def.com") && !emailIDUser.contains("ghz.com") )
        {  
            def watcherManager = ComponentAccessor.getWatcherManager();
            watcherManager.stopWatching(user, issue);
            log.debug(logPrefix + "Current removed watcher emailID is :: " + emailIDUser);   
                
            // List<ApplicationUser> users;
            // users.add(ComponentAccessor.getUserManager().getUserByName(user.toString().split(":")[0]))
            // issue.setCustomFieldValue(cfCustomerWatchersObj, users);
            issue.setCustomFieldValue(cfCustomerWatchersObj, watcherUser.toString());
        }
    }
}
catch(Exception e){
    log.error(logPrefix + e.getMessage());
}

 

 

3 answers

1 accepted

0 votes
Answer accepted
vishal goyal May 11, 2018
Arun_Thundyill_Saseendran
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.
May 14, 2018

@vishal goyal

Can you please try the below code and let me know.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.type.EventDispatchOption
import org.apache.log4j.Category
import com.atlassian.jira.issue.watchers.IssueWatcherAccessor
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.user.util.UserUtil;
import org.apache.log4j.Logger;


Logger log = Logger.getLogger("Deletion of Watchers from the issue");
log.setLevel(org.apache.log4j.Level.DEBUG);
def logPrefix ="Deletion of Watchers from the issue" + issue.getKey() + "***********************************************************************************" ;
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
List<ApplicationUser> users = new ArrayList<ApplicationUser>();

try{

Locale en = new Locale("en");
IssueWatcherAccessor iwa = ComponentAccessor.getComponent(IssueWatcherAccessor.class)
def list = iwa.getWatchers(issue,en);
log.debug(logPrefix+ "All watchers are::" + list);

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def cfCustomerWatchers = "Customer Watchers";
cfCustomerWatchersObj = customFieldManager.getCustomFieldObjectByName(cfCustomerWatchers);

for (i in list )
{
def user = i;
def watcherUser = ComponentAccessor.getUserManager().getUserByKey(user.toString().split(":")[0]);
def emailIDUser = watcherUser.getEmailAddress();



if(!emailIDUser.contains("abc.com") && !emailIDUser.contains("def.com") && !emailIDUser.contains("ghz.com") )
{
def watcherManager = ComponentAccessor.getWatcherManager();
watcherManager.stopWatching(user, issue);
log.debug(logPrefix + "Current removed watcher emailID is :: " + emailIDUser);
users.add(ComponentAccessor.getUserManager().getUserByName(user.toString().split(":")[0]))
}
}
cfCustomerWatchersObj.updateValue(null, issue, new ModifiedValue("", users), changeHolder);
}
catch(Exception e){
log.error(logPrefix + e.getMessage());
}
ION Information Systems
Contributor
May 14, 2018



ION Information Systems
Contributor
May 14, 2018

I tried that as well along with many other combinations earlier, nothing seems to works using server Jira V6.4

Arun_Thundyill_Saseendran
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.
May 14, 2018

This is strange. I am using add() on the List<ApplicationUser>. And add() is a method in list. My bad. I think I missed the initialization of list. Silly!

 Can you change 

List<ApplicationUser> users;

 to

List<ApplicationUser> users = new ArrayList<ApplicationUser>();

and try. I currently do not have an instance with me to try. That's why. 

vishal goyal May 14, 2018

Thanks @Arun_Thundyill_Saseendran for responding. 
I am getting error 

2018-05-14 16:15:34,409 http-bio-8449-exec-5 ERROR Vishal.Goel 975x38110x1 1p766ts **.***.**.*** ,0:0:0:0:0:0:0:1 /secure/WorkflowUIDispatcher.jspa [Deletion of Watchers from the issue] Deletion of Watchers from the issueCF-10010***********************************************************************************Cannot invoke method add() on null object

0 votes
Arun_Thundyill_Saseendran
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.
May 14, 2018

@vishal goyal @ION Information Systems I’m adding below the working code as answer rather than comment so that the community gets benefitted. Kinky mark this as accepted since it has worked good.

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.type.EventDispatchOption
import org.apache.log4j.Category
import com.atlassian.jira.issue.watchers.IssueWatcherAccessor
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.user.util.UserUtil;
import org.apache.log4j.Logger;


Logger log = Logger.getLogger("Deletion of Watchers from the issue");
log.setLevel(org.apache.log4j.Level.DEBUG);
def logPrefix ="Deletion of Watchers from the issue" + issue.getKey() + "***********************************************************************************" ;
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
List<ApplicationUser> users = new ArrayList<ApplicationUser>();

try{

Locale en = new Locale("en");
IssueWatcherAccessor iwa = ComponentAccessor.getComponent(IssueWatcherAccessor.class)
def list = iwa.getWatchers(issue,en);
log.debug(logPrefix+ "All watchers are::" + list);

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def cfCustomerWatchers = "Customer Watchers";
cfCustomerWatchersObj = customFieldManager.getCustomFieldObjectByName(cfCustomerWatchers);

for (i in list )
{
def user = i;
def watcherUser = ComponentAccessor.getUserManager().getUserByKey(user.toString().split(":")[0]);
def emailIDUser = watcherUser.getEmailAddress();



if(!emailIDUser.contains("abc.com") && !emailIDUser.contains("def.com") && !emailIDUser.contains("ghz.com") )
{
def watcherManager = ComponentAccessor.getWatcherManager();
watcherManager.stopWatching(user, issue);
log.debug(logPrefix + "Current removed watcher emailID is :: " + emailIDUser);
users.add(ComponentAccessor.getUserManager().getUserByName(user.toString().split(":")[0]))
}
}
cfCustomerWatchersObj.updateValue(null, issue, new ModifiedValue("", users), changeHolder);
}
catch(Exception e){
log.error(logPrefix + e.getMessage());
}
0 votes
vishal goyal May 14, 2018

Yup I am using the same, and included the

ApplicationUser package as well. This strange behavior even i am not able to understand, i again just copy pasted your suggested code as well. But no Hope.

I have raised a case with Adaptavist team as well.
Here is the case .. hoping to get solution from either way
https://productsupport.adaptavist.com/servicedesk/customer/portal/2/SRJSUP-4727

Arun_Thundyill_Saseendran
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.
May 14, 2018

I have update the full code as well. The null error for sure was my coding glitch. Please try and let me know.

vishal goyal May 14, 2018

PERFECT!!!!!!!!.. it worked. Though it overriding the older value every time but its fine i can fix that... Thanks a tonne @Arun_Thundyill_Saseendran.

I think issue was on this part

new ArrayList<ApplicationUser>(); 
Arun_Thundyill_Saseendran
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.
May 14, 2018

Yes. For it to not override, you can just read from it add it to the same list before you update. 

Good to know it helped.

Anytime!

Suggest an answer

Log in or Sign up to answer