Skip to main content
1-Visitor
December 10, 2013
Solved

Duplicate Checking in Trigger

  • December 10, 2013
  • 1 reply
  • 8602 views

Hello,

My item has kind of composit primary key. Combination of 4 fields make my Item unique. Somehow duplicate checking functionality offered by Integrity is not helpful or not working for me.

I created Unique Name field, and populated this field via pre-triggers. Now before I commit this item, I would like to check uniqueness of this calculated string but I do not find any direct call to get items based on string value. Neither the query is allowing to dynamically substitute parameter and execute.

What would be best suggession or solution in this case for duplicate checking?

Thanks for reading my post,

Regards

Adhik

Best answer by mrump

Hi Adhik,

a trigger like this should do (see attachment)

....

/**
* creates the Command to Run the temporary Query
* @return Packages.com.mks.api.Command queryCommand
*/
function getRunQueryCommand()
{
var queryCommand = new Packages.com.mks.api.Command(Packages.com.mks.api.Command.IM, "issues");
queryCommand.addOption(new Packages.com.mks.api.Option("fields", "Type,ID"));

var queryBuf = new java.util.StringBuffer();
queryBuf.append('(');
queryBuf.append("field[");
queryBuf.append(singletonField);
queryBuf.append("]=");
queryBuf.append(currentValue);
queryBuf.append('"');
queryBuf.append(')');

queryCommand.addOption(new Packages.com.mks.api.Option("queryDefinition", queryBuf.toString()));
return queryCommand;
}

/**
* check whether the temp query finds at least one Item of the same value as the current Item
* @return true if successful; else always false
*/
function check4Singleton()
{

// Create an API Object to run IM Command Line commands
var api = environmentBean.createAPISessionBean();

// Execute the Query command
var runQueryOut = api.executeCmd(getRunQueryCommand());
if( createQueryOut.getExitCode() == 0 ){
logDebug("Successfully run temporary Query");
if( runQueryOut.getExitCode() == 0 ){
// analyse result
if (runQueryOut.getWorkItemListSize() == 0 ){
logDebug("no duplicate found, proceed");
return true;
}
else{
print("found "+runQueryOut.getWorkItemListSize()+" Item(s) of the exact same value for Field "+ singletonField );
logDebug("abort");
return false;
}
}

else
{
print("Failed to run temporary Query");
logDebug("leave check4Singleton()");
return false;
}

} // END if( createQueryOut.getExitCode() == 0 )
else{
print("Failed to create temporary Query");
}
return false;
}

....

1 reply

mrump16-PearlAnswer
16-Pearl
December 16, 2013

Hi Adhik,

a trigger like this should do (see attachment)

....

/**
* creates the Command to Run the temporary Query
* @return Packages.com.mks.api.Command queryCommand
*/
function getRunQueryCommand()
{
var queryCommand = new Packages.com.mks.api.Command(Packages.com.mks.api.Command.IM, "issues");
queryCommand.addOption(new Packages.com.mks.api.Option("fields", "Type,ID"));

var queryBuf = new java.util.StringBuffer();
queryBuf.append('(');
queryBuf.append("field[");
queryBuf.append(singletonField);
queryBuf.append("]=");
queryBuf.append(currentValue);
queryBuf.append('"');
queryBuf.append(')');

queryCommand.addOption(new Packages.com.mks.api.Option("queryDefinition", queryBuf.toString()));
return queryCommand;
}

/**
* check whether the temp query finds at least one Item of the same value as the current Item
* @return true if successful; else always false
*/
function check4Singleton()
{

// Create an API Object to run IM Command Line commands
var api = environmentBean.createAPISessionBean();

// Execute the Query command
var runQueryOut = api.executeCmd(getRunQueryCommand());
if( createQueryOut.getExitCode() == 0 ){
logDebug("Successfully run temporary Query");
if( runQueryOut.getExitCode() == 0 ){
// analyse result
if (runQueryOut.getWorkItemListSize() == 0 ){
logDebug("no duplicate found, proceed");
return true;
}
else{
print("found "+runQueryOut.getWorkItemListSize()+" Item(s) of the exact same value for Field "+ singletonField );
logDebug("abort");
return false;
}
}

else
{
print("Failed to run temporary Query");
logDebug("leave check4Singleton()");
return false;
}

} // END if( createQueryOut.getExitCode() == 0 )
else{
print("Failed to create temporary Query");
}
return false;
}

....
akadam1-VisitorAuthor
1-Visitor
December 17, 2013

Thanks Matthias,

With little tweaks it worked. Now I have to make it generic enough so that others can use too like library function.

Regards

Adhik