0

Has anyone created a macro to update the Project/Account/Sales Managers?

mark 2 years ago updated 2 years ago 3

I'm hoping to be able to change the Project Manger (and/or account manager, sales manager, project coordinator) through a macro and trigger it via api. Has anyone done this? Does this seem doable? Thanks!

Yes, we do that all the time..... but.... there is a problem.

It seems that there are two database fields within the XTRF database, and only one can be updated through the macro. The other should be updated through a command, but XTRF doesn't provide support for that so in that sense it is a black box.

So does it matter that only one field is updated? That depends on how you retrieve the project manager (etc.)

I'm not 100% sure, but I think it is like this:

- If you retrieve that field through the API, it shows you the wrong/old value

- if you retrieve that field through the browser view, it shows the correctly updated value

- if you look at the field through the browser itself (navigating to the project) it shows the correctly updated value.

So to recap: updating is possible, but then you can't trust the API values anymore.

This is the macro that we have for updating them. Groovy macro on Project. Smart projects.

import com.radzisz.xtrf.utils.velocity.VelocityTagUtils
def isDebug = false

class MacroExecute {
    def isDebug
    def list
    def params
    String result = ""
    VelocityTagUtils utils = new VelocityTagUtils()
    
    MacroExecute(list, params, isDebug) {
        this.list = list
        this.params = params
        this.isDebug = isDebug
    }

    def run() {
        if(this.isDebug) {
            this.result += "Amount items: " + this.list.size() + "\n"
        }
        list.each {
            def item -> this.result += new SingleElement(item, params, isDebug).run()
        }
        
        if(this.isDebug) {
            this.result += "\nPARAMS: \n"
            this.result += new Debug(this.params).trace()
        }
    }
}

class SingleElement {
    def item
    def params
    def isDebug
    String result = ""
    VelocityTagUtils utils = new VelocityTagUtils()
    
    SingleElement(item, params, isDebug) {
        this.item = item
        this.params = params
        this.isDebug = isDebug
    }
    
    def run() {
        def userService = this.utils.getService('com.radzisz.xtrf.service.UserService')
        def projectService = this.utils.getService('com.radzisz.xtrf.service.ProjectService')

        if(this.params.projectManager != null && this.item.getProjectManager().id != this.params.projectManager)
        {
            def pm = userService.getById(this.params.projectManager)
            this.item.setProjectManager(pm)
            this.result += "Setting PM: " + pm.fullName
        }
        if(this.params.salesPerson != null && this.item.getSalesPerson().id != this.params.salesPerson)
        {
            def sp = userService.getById(this.params.salesPerson)
            this.item.setSalesPerson(sp)
            this.result += "Setting SP" + sp.fullName
        }
        
        projectService.update(this.item)
        
        return this.result;
    }
    
}

class Debug {
    def debugItem
    
    Debug(debugItem) {
        this.debugItem = debugItem
    }
    
    def trace() {
        def result = ""
        
        def fields = this.debugItem.getClass().getDeclaredFields()
        for (def i = 0; i < fields.size(); i++) {
           fields[i].setAccessible(true)
           result += fields[i].getName() + " = " + fields[i].get(debugItem) + "\n"
        }
        
        return result;
    }
}

def testParams = new Object() { 
    int salesPerson = 1
    int projectManager = 1
}

if(isDebug) {
    params = testParams
}
MacroExecute r = new MacroExecute(list, params, isDebug)
r.run()
r.result
+1

Thanks Dennis for your insight. I'm going to look into this asap. And thanks for the code, we'll see what we can learn from it. I appreciate it!! But I'll also add... Sigh and damn...