How to Get Next Record Value by IDEA Script?
Forums
I try to add a Line-Item field with below script, but the GetNextValue can not be accepted in IDEA script, does anyone know how to get the next record value by IDEA script?
Doc
A001
A001
A001
B001
B001
-------------------------------------------------------------------
Doc Line-Item
A001 1
A001 2
A001 3
B001 1
B001 2
-------------------------------------------------------------------
Script:
Set db = Client.OpenDatabase("TEST.IDM")
Set NewField = db.TableManagementSet ThisTable = db.TableDefSet MyField = ThisTable.NewFieldMyField.Name = "LINE_ITEM"MyField.Description = ""MyField.Type = WI_EDIT_NUMMyField.Equation = 0
NewField.AppendField MyFieldNewField.PerformTask
n = 0
Set rs = db.RecordSet
rs.ToFirst
Set rec = rs.ActiveRecord
For j = 1 To db.Count rs.Next IF newRec.GetNumValue("DOCUMENTNO")= iGetNextValue("DOCUMENTNO") Then n = n + 1 rec.SetNumValue "LINE_ITEM", n rs.SaveRecord rec Else n = 1 rec.SetNumValue "LINE_ITEM", n rs.SaveRecord rec
End IFNext j
Hi Brian, thanks your
Hi Brian, thanks your response and re-copy the script. I copy&paste the script, but it could not keep the format after I posted the topic. Because some IDEA functions could be used to the script directly, such as MID, I thought the getNextValue could be used before. But if the getNextVaule could not be used, how to compare the next record? I also try to use the equation editor to add a line-item field, but it is not easy to write the equation. The line-item field could be created by Excel function, such as used the COUNTIF, however the raw data is more than one million records, use the COUNTIF would exhaust the computer resource and make the Excel crash. So the better way is used the IDEA script, but when the getNextValue not workable, I don't know how to write the script. Please give me some suggestion about how to write the script or use the customized equations could resolve it, thanks.
Hi Foster,
Hi Foster,
You can try out this code. I broke it down into two sections, the first creates the field, you will notice that I am creating an IDEA field (WI_NUM_FIELD), the reason being is that the code to update fields doesn't seem to work on anything but IDEA fields, if you try it I think you get an error or something, took me a while of head banging before I figured that out.
The second function does the update. I use the rs.GetAt function to get the different records, put the values in a variable and then compare them. I do the previous one first so that when I add on the information to the new field it gest added to the current one. I also added some code so that if we are looking at the first record it defaults to 1. Now I haven't tested the script much, the example file I used was limited so you might get some errors if so let me know what they might be.
As for not being able to use the iGetNextValue, this is a new function within IDEA and for what ever reason IDEA decided not to implement the new equation functions in IDEAScript.
Good luck.
Brian
Option Explicit
Sub Main
Call createField()
Call updateField()
End Sub
Function createField()
Dim db As database
Dim NewField As Object
Dim ThisTable As table
Dim MyField As field
Dim currentValue As Integer
Dim nextValue As Integer
Set db = Client.OpenDatabase("TEST.IDM")
Set NewField = db.TableManagement
Set ThisTable = db.TableDef
Set MyField = ThisTable.NewField
MyField.Name = "LINE_ITEM"
MyField.Description = ""
MyField.Type = WI_NUM_FIELD
MyField.Equation = "0"
MyField.Decimals = 0
NewField.AppendField MyField
NewField.PerformTask
Set myField = Nothing
Set ThisTable = Nothing
Set NewField = Nothing
Set db = Nothing
End Function
Function updateField()
Dim db As database
Dim table As table
Dim field As field
Dim rs As Object
Dim rec As Object
Dim i, j As Long
Dim n As Long
Dim currentValue As Integer
Dim prevValue As Integer
Set db = client.OpenDatabase("TEST.IDM")
Set table = db.TableDef
Set Field = table.GetField("LINE_ITEM")
Set rs = db.RecordSet
field.Protected = FALSE
For i= 1 To db.Count
'get the next value
If i = 1 Then
rs.GetAt(i)
Set rec = rs.ActiveRecord
n = 1
Else
j =i - 1
rs.GetAt(j)
Set rec = rs.ActiveRecord
prevValue = rec.GetNumValue("DOCUMENTNO")
rs.GetAt(i)
Set rec = rs.ActiveRecord
currentValue = rec.GetNumValue("DOCUMENTNO")
If currentValue = prevValue Then
n = n + 1
Else
n = 1
End If
End If
rec.SetNumValue "LINE_ITEM", n
rs.SaveRecord rec
Next i
field.Protected = TRUE
End Function
Hi Brian,
Hi Brian,
Thanks your code, it really give a great help. Thank you.
Just a question, the db needs to be closed and re-opened to show the updated results, if not re-open the db, the fileld value would keep the zero value.
So I add the "db.close" and "Client.OpenDatabase" to the script as the attachemnt to close the db and re-open it.
Thank you again.
Yes you have to close the db
Yes you have to close the db and then reopen it to see the changes. So when you run the script the values get changed in the source database but what you are seeing on screen has been loaded into memory so you don't see the changes immediately, took me a bit of time to figure that out. So for these type of scripts I always will add the options that you noted to close and then reopen the database so the changes show up.
Hi Forster, I just recopied