How to delete object using SDK?

No file attached

Add Comment
3 Answer(s)

Thanks for the tips. I created a PX script in the Event Handler and only enabled the Event Subscriber so I could run it once, then disable it.  Here is the script that worked.

// Purpose: Soft delete multiple items

// Agile PLM Groovy script, using Agile SDK

// For a bunch of parts, delete them in Agile. But only delete once because doing it 2nd

// time will permenantly delete it. To permanently delete, log in to Agile, go to the

// Recycle Bin, sort by item description, find those and select that begin with "del",

// and permanently delete multiple items from there.

//

// 1. Only do this for Item Lifecycle Phase = 'Preliminary'

// 2. pitem.isDeleted() should be false, meaning only delete if it has not been marked as deleted yet

// 3. pitem.delete(); delete item and put in Recycle bin

// 4. Append "del " to front of item description so we can find these easy in the recycle bin

// 4.1 Remove last 4 chars from item description before adding "del " to avoid going over char limit
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.IObjectEventInfo;
import java.util.*;

import java.util.regex.*;
void invokeScript(IBaseScriptObj obj) {
IAgileSession session = obj.getAgileSDKSession();

IEventInfo req = obj.getPXEventInfo();

Integer del_cnt = 0;
try {
if (req instanceof IObjectEventInfo) {

eventInfo = (IObjectEventInfo)req;

} else {

obj.logMonitor("req IS NOT AN instanceof IObjectEventInfo");

}
if (eventInfo!=null) {
del_cnt = delItem(obj, session, "09.4761V.001", del_cnt);

del_cnt = delItem(obj, session, "21.I1021.003", del_cnt);

del_cnt = delItem(obj, session, "21.M0630.L01", del_cnt);
// ---- Add more items above, that you wish to flag as deleted and move into Recycle Bin ----
obj.logMonitor("nbr of items flagged as deleted: " + del_cnt);

}
} catch (APIException ex) {
ex.printStackTrace();

obj.logMonitor(ex.getMessage());

}

}
//

// --------- delete item ---------

//

private static int delItem(IBaseScriptObj obj, IAgileSession session, String itemnbr, Integer delcnt) throws APIException {
IItem pitem = (IItem) session.getObject(ItemConstants.CLASS_ITEM_BASE_CLASS, itemnbr);

String lifecyc = (String) pitem.getValue(ItemConstants.ATT_TITLE_BLOCK_LIFECYCLE_PHASE);
String itemdesc = (String) pitem.getValue(ItemConstants.ATT_TITLE_BLOCK_DESCRIPTION);

itemdesc = itemdesc.reverse(); // reverse all the characters, ex: ABCDEFG becomes GFEDCBA

itemdesc = itemdesc.drop(4); // remove the first 4 chars, ex: GFEDCBA becomes CBA

itemdesc = itemdesc.reverse(); // reverse it again, ex: CBA becomes ABC

itemdesc = "del " + itemdesc;
if ( lifecyc == 'Preliminary' ) {

if (!pitem.isDeleted()) {

pitem.setValue(ItemConstants.ATT_TITLE_BLOCK_DESCRIPTION, itemdesc);

pitem.delete();

delcnt = delcnt + 1;

}

}

return (delcnt);

}

Agile Talent Answered on June 23, 2021.
Add Comment

You have to call delete() on an Object to put it in the recycle bin and call it again to delete it at all.
for example: for a change

IChange change= session.getObject(IChange.OBJECT_TYPE, “CO123XXX”);

//recycleBin
change.delete();

//delete at all
change.delete();

Agile Angel Answered on April 6, 2017.
Add Comment
Best answer

To add to Antonio, if your intention is to soft delete only. Just have a check at start for soft-deleted items so that you don’t end up hard-deleting it. 

//isDeleted() checks whether an object is deleted or not. 

If (!change.isDeleted())
{
   change.delete();  
}

Agile Angel Answered on April 6, 2017.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.