andy2609
9th May 2019, 16:09
When calling a session with a type 3 form (tipcf1101m000 - Product Features by Generic Item), I need to add a new record for a Generic Item that initially has no records (in table tipcf100), using an API command.

Using 'stpapi.put.field' to put the relevant values, followed by either 'stpapi.find' or 'stpapi.change.view', the process simply finds the next record in the table. Hence, when using 'stpapi.insert', an error of 'Record already exists' is encountered. This is because the API is still focused on the Generic Item that already exists.

Therefore, what I need to do is execute a 'start.set' command via the API and to enter the new Generic Item - or I need to find another method of adding a record for a new Generic Item.

Any ideas as to a solution?

mark_h
10th May 2019, 14:50
Are you putting all of the fields including the set fields?

Couple of samples I know that work in theory I do not even need to do a change view. I should just be able to do a flat out insert with no find since I do not care if it already exists.

function add.builditem()
{
stpapi.put.field( "tipgc8100m00b", "tipgc800.cprj", project )
e = stpapi.change.view( "tipgc8100m00b" )
stpapi.put.field( "tipgc8100m00b", "tipgc800.cprj", project )
stpapi.put.field( "tipgc8100m00b", "tipgc800.butm", builditem )
stpapi.put.field( "tipgc8100m00b", "tipgc800.dsca", "Simulation" ) | 20160113
stpapi.insert( "tipgc8100m00b", 1, errmsg)
|021902 Skip records already exist. This means the build item
| was already added.
if(strip$(errmsg)<>"" and pos(errmsg,"Record already exists")=0) then
mess.1= "1." & builditem & " " & errmsg
write.error(mess.1)
else
mess.1="1. Build item added. " & builditem
endif
display("mess.1")
stpapi.end.session( "tipgc8100m00b" )
}
function add.subitem()
{
stpapi.put.field( "tipgc8105m00b", "tipgc805.cprj", project )
stpapi.put.field( "tipgc8105m00b", "tipgc805.butm", builditem )
e = stpapi.change.view( "tipgc8105m00b" )
stpapi.put.field( "tipgc8105m00b", "tipgc805.cprj", project )
stpapi.put.field( "tipgc8105m00b", "tipgc805.butm", builditem )
stpapi.put.field( "tipgc8105m00b", "tipgc805.sbtm", subitem )
stpapi.put.field( "tipgc8105m00b", "tipgc805.dsca", "Simulation" ) | 20160113
stpapi.insert( "tipgc8105m00b", 1, errmsg )

|021902 Skip records already exist. This means the sub item
| was already added.
if(strip$(errmsg)<>"" and pos(errmsg,"Record already exists")=0) then
mess.2= "2. "&subitem&" "&errmsg
write.error(mess.2)
else
mess.2="2. Sub item added. " & subitem
endif
display("mess.2")
stpapi.end.session( "tipgc8105m00b" )
}

andy2609
20th May 2019, 19:25
Thanks for your reply, Mark.

The primary index fields are being put before the 'stpapi.insert' command. I had thought the same as you - that it would not matter whether an existing record already exists for the Generic Item or not.

Here is a snippet of my code:
stpapi.put.field("tipcf1101m000", "tipcf100.item", strip$(i.gitm))
stpapi.put.field("tipcf1101m000", "tipcf100.sern", str$(i.sern))
ret = stpapi.change.view("tipcf1101m000", fs.error)

stpapi.put.field("tipcf1101m000", "tipcf100.item", strip$(i.gitm))
stpapi.put.field("tipcf1101m000", "tipcf100.sern", str$(i.sern))
stpapi.put.field("tipcf1101m000", "tipcf100.cpft", i.cpft)
stpapi.put.field("tipcf1101m000", "tipcf100.dsca", strip$(i.cpft.dsca))
stpapi.put.field("tipcf1101m000", "tipcf100.indt", str$(i.cpft.indt))
stpapi.put.field("tipcf1101m000", "tipcf100.exdt", str$(i.cpft.exdt))
stpapi.put.field("tipcf1101m000", "tipcf100.cnsc", i.cnsc)
stpapi.put.field("tipcf1101m000", "tipcf100.sopt", str$(fs.yes))
|
do.update = 1
lret = stpapi.insert("tipcf1101m000", do.update, fs.error)
|
lret = stpapi.save("tipcf1101m000", fs.error)


A 'Record already exists' error is returned by the function server.

mark_h
20th May 2019, 21:27
My system has 2 unique indices on the table. tipcf100.item and tipcf100.sern is the primary index on the form. Then there is tipcf100.item and tipcf100.cpft - also unique. Are you 100% sure the information you are putting to the form for those fields are unique?

Addition to post: On my system I would only put the item and do a change view. That is what is the primary field on the form. The sern would be a variable. I would always want it to be max+1 for the generic item in question.

andy2609
21st May 2019, 19:00
Hi again Mark. Thanks for your thoughts on this.

The table on the system I am working on also has the same index values as yours and yes, the data is unique.

On my system, when putting the item and executing a 'change view', the Generic Item value in User Defaults for the session (for instance, when the session was last closed) is always retrieved.

This is borne out by the following command returning the default item to the variable 'view.gitm':
stpapi.get.field("tipcf1101m000", "tipcf100.item", view.gitm)

I have worked around this by ensuring that the first instance of inserting a Product Feature by Generic Item is added with db.insert - thereafter, the function server process works fine.

Thanks again for your input!

mark_h
22nd May 2019, 15:00
That is interesting and a good work around. I am glad I did not have that issue with the ones I did. I had to use an actual update in a few cases where fields would not update the the api code - but never a key index field. I think one of them was like the terms of pmt or delivery on the purchase order header - never could get it to update, so just did a select/find/db.update.

andy2609
23rd May 2019, 12:34
Thanks for taking the time to think about and respond to this Mark. Appreciated!