<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: How can i access a ManagedBean in a IVY-Script-Step ??</title><link>https://answers.axonivy.com/questions/756/how-can-i-access-a-managedbean-in-a-ivy-script-step</link><description>&lt;p&gt;How can i access to a method or getter/setter of a managed bean (e.g. session scoped) in an ivy-script-step ??&lt;br&gt;
I know from an xhtml page it is possible, but i want to start a process in ivy (event) and read some data in a db-Step and write the data back to the managed bean !!&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/756/how-can-i-access-a-managedbean-in-a-ivy-script-step" rel="self"></atom:link><language>en</language><lastBuildDate>Wed, 11 Jun 2014 09:27:03 -0400</lastBuildDate><item><title>Answer by Wanderhirt</title><link>https://answers.axonivy.com/questions/756/how-can-i-access-a-managedbean-in-a-ivy-script-step/774</link><description>&lt;p&gt;The creation of the object (managed bean) happens automatically, you must not do that by yourself (because you will receive/create another instance!). 
The point is now to get the correct object (created by faces context usually by the first access!).&lt;/p&gt;
&lt;p&gt;The code in the script step looks like follows:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import javax.faces.context.FacesContextFactory;
import javax.faces.context.FacesContext;
import ch.soreco.mbean.mb.SimpleBean;

SimpleBean sb;
FacesContext context = FacesContext.getCurrentInstance();

sb = context.getApplication().evaluateExpressionGet(context, "#{simpleBean}", SimpleBean.class) as SimpleBean;

// invoke method of the managed bean
sb.setS1(in.input);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The code of the managed bean:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;package ch.soreco.mbean.mb;

import javax.faces.bean.SessionScoped;
import javax.faces.bean.ManagedBean;

import ch.ivyteam.ivy.environment.Ivy;


@ManagedBean(name = "simpleBean")
@SessionScoped
public class SimpleBean 
{
    private String s1;
    private int counterSet = -1;
    private int counterGet = -1;


    public SimpleBean() 
    {
        super();
        s1 = "--";
        counterSet = 0;
        counterGet = 0;
        Ivy.log().warn("BEAN created!");
    }

    public String getS1() {
        counterGet++;
        Ivy.log().warn("BEAN getter called: "+counterGet);
        return s1;
    }

    public void setS1(String s1) {
        counterSet++;
        Ivy.log().warn("BEAN setter called: "+counterSet);
        this.s1 = s1;
    }

    public int getCounterSet() {
        return counterSet;
    }

    public void setCounterSet(int counterSet) {
        this.counterSet = counterSet;
    }

    public int getCounterGet() {
        return counterGet;
    }

    public void setCounterGet(int counterGet) {
        this.counterGet = counterGet;
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This approach works surely with session or application scoped beans.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Wanderhirt</dc:creator><pubDate>Wed, 11 Jun 2014 09:27:03 -0400</pubDate><guid>https://answers.axonivy.com/questions/756/how-can-i-access-a-managedbean-in-a-ivy-script-step/774</guid></item><item><title>Answer by trungdv</title><link>https://answers.axonivy.com/questions/756/how-can-i-access-a-managedbean-in-a-ivy-script-step/761</link><description>&lt;p&gt;Usually i do that way:
First i will create instance:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;FacesContext context = FacesContext.getCurrentInstance();       
            FileManagerBean fileManagerBean=new FileManagerBean();
            context.getExternalContext().getSessionMap().put("fileManagerBean", fileManagerBean);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;then i get it when i want to use like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import javax.faces.context.FacesContext;
import ch.ivyteam.ivy.addons.filemanager.html.managedbean.FileManagerBean;
FacesContext context = FacesContext.getCurrentInstance();               
FileManagerBean fileManagerBean=context.getExternalContext().getSessionMap().get("fileManagerBean") as FileManagerBean;
in.enablePreviewLink = fileManagerBean.enablePreviewLink;
&lt;/code&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">trungdv</dc:creator><pubDate>Tue, 10 Jun 2014 11:12:57 -0400</pubDate><guid>https://answers.axonivy.com/questions/756/how-can-i-access-a-managedbean-in-a-ivy-script-step/761</guid></item><item><title>Answer by Nikel Weis</title><link>https://answers.axonivy.com/questions/756/how-can-i-access-a-managedbean-in-a-ivy-script-step/757</link><description>&lt;p&gt;In your Script-Step:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import de.path.to.MyBean;
MyBean instanceOfMyBean = new MyBean(); 
instanceOfMyBean.setWhatever(10);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you want to use this bean in your view just declare a data-object of the given type (de.azt.exampleData.MapBean) in your data-class and bind this as model in your view code, f.e.:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;p:gmap zoom="6" type="TERRAIN" center="#{data.geoLat}, #{data.geoLng}" id="gmap" model="#{data.mapModel}"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The corresponding code in your script step would then be:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import de.azt.exampleData.MapBean;
out.mapModel = new MapBean(out.projects, true, out.auditors, false, true);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Whereas you need to pass the attributes from your database-query as parameters to the constructor of your bean.&lt;/p&gt;
&lt;p&gt;Hope that helps.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nikel Weis</dc:creator><pubDate>Fri, 06 Jun 2014 13:43:29 -0400</pubDate><guid>https://answers.axonivy.com/questions/756/how-can-i-access-a-managedbean-in-a-ivy-script-step/757</guid></item></channel></rss>