Groovy script to update an item
I am new to Groovy scripting. I would like to create an Event Handler that runs a Script PX, and the script is a Groovy script. When an ECO is released, I would like to loop through all of the Affected Items and take the value from REV.TEXT12 and save it to the Part PAGE_TWO.TEXT16 . How would this Groovy script be written? Thanks.
Hi.
I assume you have already configured the event. Try using the following script in handler.
import com.agile.agileDSL.ScriptObj.IBaseScriptObj
import com.agile.agileDSL.ScriptObj.AgileDSLException;
import com.agile.api.*;
import java.util.*;
import com.agile.px.IEventInfo;
import com.agile.px.EventConstants;
import com.agile.px.IWFChangeStatusEventInfo;
void invokeScript(IBaseScriptObj obj) {
IAgileSession session = obj.getAgileSDKSession();
IEventInfo req = obj.getPXEventInfo();
IWFChangeStatusEventInfo e = null;
try {
if (req instanceof IWFChangeStatusEventInfo) {
eventInfo = (IWFChangeStatusEventInfo)req;
}
String message = “Update: Nothing to process”;
if (eventInfo!=null) {
IDataObject dataObject = eventInfo.getDataObject();
IAgileClass relObjectClass = dataObject.getAgileClass();
if (!relObjectClass.isSubclassOf(ChangeConstants.CLASS_CHANGE_ORDERS_CLASS)){
//throw new AgileDSLException(“The PX is only applicable for Change Order”);
}
// Iterate through Affected Items table
ITable ai = dataObject.getTable(ChangeConstants.TABLE_AFFECTEDITEMS);
Iterator it = ai.iterator();
IRow row = null;
while(it.hasNext()) {
row = (IRow)it.next();
ai_item = (IItem)row.next();
String item_number = (String) row.getValue(ChangeConstants.ATT_AFFECTED_ITEMS_ITEM_NUMBER);
//You can give get desired change attribute value here or the base-ID
String your_bom_text_attribute = (String) row.getValue(ChangeConstants.ATT_AFFECTED_ITEMS_NEW_REV);
String m_value = (String) row.getValue(ChangeConstants.ATT_AFFECTED_ITEMS_NEW_REV);
IItem m_item = (IItem) m_session.getObject(ItemConstants.CLASS_ITEM_BASE_CLASS, item_number);
m_item.setValue(ATT_PAGE_TWO_TEXT17, m_value);
//You can enter your desired page two attribute value here.
}
obj.logMonitor(“Succesfully update the values”);
}
}
catch (Exception ex) {
ex.printStackTrace();
throw new AgileDSLException(ex);
}
}
Regards,
Arif
Thanks Arif. I edited your script slightly, but something is not working. First I created the Event Handler, which includes the script code. Then I created the Event, which is for ‘Change Status for Workflow’, for a specific workflow, and when the status changes to ‘Released’. Then I created the Event Subscriber which links the Handler to the Event. I then did a test, edited those values in the ECO Affected Items, released the ECO, but the item Page Two values did not get updated. Not sure what is missing. Those event things all say enabled. And by the way, where do messages go from the script ‘obj.logMonitor()’ ? Below is my version of Arif’s script. Thanks, Mark
//———–
import com.agile.agileDSL.ScriptObj.IBaseScriptObj
import com.agile.agileDSL.ScriptObj.AgileDSLException;
import com.agile.api.*;
import java.util.*;
import com.agile.px.IEventInfo;
import com.agile.px.EventConstants;
import com.agile.px.IWFChangeStatusEventInfo;
void invokeScript(IBaseScriptObj obj) {
IAgileSession session = obj.getAgileSDKSession();
IEventInfo req = obj.getPXEventInfo();
IWFChangeStatusEventInfo e = null;
try {
if (req instanceof IWFChangeStatusEventInfo) {
eventInfo = (IWFChangeStatusEventInfo)req;
}
String message = “Update: Nothing to process”;
obj.logMonitor(“Now looking at Affected Items LSR and MRR”);
if (eventInfo!=null) {
IDataObject dataObject = eventInfo.getDataObject();
IAgileClass relObjectClass = dataObject.getAgileClass();
if (!relObjectClass.isSubclassOf(ChangeConstants.CLASS_CHANGE_ORDERS_CLASS)){
//throw new AgileDSLException(“The PX is only applicable for Change Order”);
obj.logMonitor(“The PX is only applicable for Change Order”);
}
// Iterate through Affected Items table
ITable ai = dataObject.getTable(ChangeConstants.TABLE_AFFECTEDITEMS);
Iterator it = ai.iterator();
IRow row = null;
while(it.hasNext()) {
row = (IRow)it.next();
ai_item = (IItem)row.next();
String item_number = (String) row.getValue(ChangeConstants.ATT_AFFECTED_ITEMS_ITEM_NUMBER);
obj.logMonitor(“Item number: ” + item_number);
//You can get desired change attribute value here or the base-ID
// —– LSR —–
// Affected Item TEXT12 contains the LSR (Lowest Ship Rev)
// —————
String m_value_LSR = (String) row.getValue(ChangeConstants.ATT_AFFECTED_ITEMS_TEXT12);
obj.logMonitor(“Affected Item LSR: ” + m_value_LSR);
// —– MRR —–
// Affected Item TEXT14 contains the MRR (Min Repair Rev)
// —————
String m_value_MRR = (String) row.getValue(ChangeConstants.ATT_AFFECTED_ITEMS_TEXT14);
obj.logMonitor(“Affected Item MRR: ” + m_value_MRR);
// —– Now let’s update the item —–
IItem m_item = (IItem) m_session.getObject(ItemConstants.CLASS_ITEM_BASE_CLASS, item_number);
// Page Two of the Item will hold the LSR (Lowest Ship Rev) in TEXT16
m_item.setValue(ATT_PAGE_TWO_TEXT16, m_value_LSR);
// Page Two of the Item will hold the MRR (Min Repair Rev) in TEXT20
m_item.setValue(ATT_PAGE_TWO_TEXT20, m_value_MRR);
}
obj.logMonitor(“Succesfully updated the values”);
}
}
catch (Exception ex) {
ex.printStackTrace();
obj.logMonitor(“Update was not successful”);
throw new AgileDSLException(ex);
}
}
Update.. this is the first time we have used Events. I needed to add a privilege to the Administrator so I could ‘Enable Trigger Events’. Then I tried another test. This time I see a message in the Event Handler Monitor. It still failed. Any idea what this message means? No signature of method: com.agile.api.pc.change.RowAI.next() is applicable for argument types: () values: {}
Hi,
I believe i forgot to comment one line. Please remove the following line and re-try.
ai_item = (IItem)row.next();
Regards,
Arif
I commented that line, but got a new fail message this time. The Handler name is: Update_Part_LSR_MRR_from_ECO and the error I got is this:
com.agile.agileDSL.ScriptObj.AgileDSLException: No such property: m_session for class: Update_Part_LSR_MRR_from_ECO
Thanks,
Mark
Hi Mark,
Reviewed and tried running the code on my instance looks fine at the moment to me. I have edited your code. Please try now.
import com.agile.agileDSL.ScriptObj.IBaseScriptObj
import com.agile.agileDSL.ScriptObj.AgileDSLException;
import com.agile.api.*;
import java.util.*;
import com.agile.px.IEventInfo;
import com.agile.px.EventConstants;
import com.agile.px.IWFChangeStatusEventInfo;
void invokeScript(IBaseScriptObj obj) {
IAgileSession session = obj.getAgileSDKSession();
IEventInfo req = obj.getPXEventInfo();
IWFChangeStatusEventInfo e = null;
try {
if (req instanceof IWFChangeStatusEventInfo) {
eventInfo = (IWFChangeStatusEventInfo)req;
}
String message = “Update: Nothing to process”;
obj.logMonitor(“Now looking at Affected Items LSR and MRR”);
if (eventInfo!=null) {
IDataObject dataObject = eventInfo.getDataObject();
IAgileClass relObjectClass = dataObject.getAgileClass();
if (!relObjectClass.isSubclassOf(ChangeConstants.CLASS_CHANGE_ORDERS_CLASS)){
//throw new AgileDSLException(“The PX is only applicable for Change Order”);
obj.logMonitor(“The PX is only applicable for Change Order”);
}
// Iterate through Affected Items table
ITable ai = dataObject.getTable(ChangeConstants.TABLE_AFFECTEDITEMS);
Iterator it = ai.iterator();
IRow row = null;
while(it.hasNext()) {
row = (IRow)it.next();
ai_item = (IItem)row.next();
String item_number = (String) row.getValue(ChangeConstants.ATT_AFFECTED_ITEMS_ITEM_NUMBER);
obj.logMonitor(“Item number: ” + item_number);
//You can get desired change attribute value here or the base-ID
// —– LSR —–
// Affected Item TEXT12 contains the LSR (Lowest Ship Rev)
// —————
String m_value_LSR = (String) row.getValue(ChangeConstants.ATT_AFFECTED_ITEMS_TEXT12);
obj.logMonitor(“Affected Item LSR: ” + m_value_LSR);
// —– MRR —–
// Affected Item TEXT14 contains the MRR (Min Repair Rev)
// —————
String m_value_MRR = (String) row.getValue(ChangeConstants.ATT_AFFECTED_ITEMS_TEXT14);
obj.logMonitor(“Affected Item MRR: ” + m_value_MRR);
// —– Now let’s update the item —–
IItem m_item = (IItem) session.getObject(ItemConstants.CLASS_ITEM_BASE_CLASS, item_number);
// Page Two of the Item will hold the LSR (Lowest Ship Rev) in TEXT16
m_item.setValue(ItemConstants.ATT_PAGE_TWO_TEXT16, m_value_LSR);
// Page Two of the Item will hold the MRR (Min Repair Rev) in TEXT20
m_item.setValue(ItemConstants.ATT_PAGE_TWO_TEXT20, m_value_MRR);
}
obj.logMonitor(“Succesfully updated the values”);
}
}
catch (Exception ex) {
ex.printStackTrace();
obj.logMonitor(“Update was not successful”);
throw new AgileDSLException(ex);
}
}
Regards,
Arif
Hi Arif,
I had a recent case where instead of “IAgileSession session = obj.getAgileSDKSession();”, I had to use “m_session = object.getAgileSDKSession();”. And instead of “obj.logMonitor…”, I had to use “object.logMonitor…”
Not 100% if either one is important right now, but those were edits I did with another one of my scripts as my IT programmer and I tried to make work.
Hi Matt,
Not sure if this change will make much of difference. m_session or session are just variable. It should work either ways as when you don’t specify IAgileSession it will case m_session to session object. It works pretty much like varchar.
I have used in either ways and it worked. So whichever approach works u need to go with that 🙂
Regards,
Arif
Hi Arif, using your most recent code, I still am getting this fail message in the Event Handler Monitor:
No signature of method: com.agile.api.pc.change.RowAI.next() is applicable for argument types: () values: {}
I noticed there was no ; semi-colon at the end of the first line of code, so I added it. But still I get this error. Not sure what I am missing. We have Agile PLM 9.3.2.
Thanks,
Mark
Arif, Please ignore my last message. I needed to comment the ai_item line of code. It worked! Wow, that is great.
Thanks,
Mark