Sage MAS 90 and 200 Sage MAS 500 blogs Product Feedback Support Training
Reply
unidentified user
jack6400
Posts: 6
Registered: 01-31-2012
0

Preventing a record write from a script triggered at table write pre-write

Hi All,

Long time lurker needing some help.  We've been having some inventory issues at my organization and as a result one of our production workers asked for, and received the go ahead to, have a udf added to the Sales Order Detail table which validated off a UDT of possible part statuses.  I'm the developer that's been tasked with making this happen and what I'm trying to do now is write a script so that if he, accidentally or otherwise, modifies data other than this UDF the script will detect the attempted write and halt it.  I've got the script correctly detecting the user but is there any strategy that anyone knows, programatically, to prevent a write from a user manipulating the UI?

Sage Employee
jepritch
Posts: 233
Registered: 08-25-2009
0

Re: Preventing a record write from a script triggered at table write pre-write

Hi Jack,

 

There are a number of ways to prevent the write from occurring, you do want to make sure you are honing down the conditions for this though.  From what I can get in your original post, you want to prevent the write of the sales order under the following situations:

 

1) This One specific user

2) If any data on the line changes OTHER than your UDF

3) Only during data entry

 

This would mean this user would only ever be able to change this 'Part Status' UDF on the lines.  Is that what you are looking for?

 

Elliott

 

unidentified user
jack6400
Posts: 6
Registered: 01-31-2012
0

Re: Preventing a record write from a script triggered at table write pre-write

Elliot,

That's exactly it. 

Sage Employee
jepritch
Posts: 233
Registered: 08-25-2009
0

Re: Preventing a record write from a script triggered at table write pre-write

Ok, so assuming that the work flow is someone enters the Sales Order details, accepts them, then your special user goes back to modify this sales order, setting the appropriate UDF value for each line.  On a Line by Line basis the user will get a message if they change anything other than the "part status" UDF?

 

During the pre-write of the SO_SalesOrderDetail record we need to determine if the only thing that has changed in the record was the UDF in question.

 

I guess what I would do, and I'm sure there are other ways to accomplish this,

 

1) Would be to setup a Post-Read event script on the SO_SalesOrderDetail, storing off the value of the UDF prior to any changes.

 

origVal = ""

retVal = oBusObj.GetValue("UDF_PartStatus$", origVal)

retVal = oScript.SetStorageVar("origPartStatus", origVal)

 

2) On a Pre-Write event script of the SO_SalesOrderDetail, we need to retrieve the entire record, and test to see if ONLY the UDF in question has changed.  Only for this user and only during data entry.

 

If oSession.UserCode = "XXX" and oSession.Updating=0 Then

   currVal = ""

   retVal = oBusObj.GetValue("UDF_PartStatus$", currVal)

   origVal = ""

   retVal = oScript.GetStorageVar("origPartStatus", origVal)

   ' retrieve orig record

   origRec = ""

   origRec = oBusObj.GetOrigRecord()

   ' temporarily set original UDF value back for comparison

   retVal = oBusObj.SetValue("UDF_PartStatus$", origVal)

   compRec = ""

   retVal = oBusObj.GetRecord(compRec, tmpIOL)

   ' put back changed value

   retVal = oBusObj.SetValue("UDF_PartStatus$", currVal)

 

   ' compare records if NOT the same then something else changed

   If origRec<>compRec Then

      retVal = oScript.SetError("Can ONLY change the Part Status")

   End If

 

 

I think that should work, but please keep in mind I have not test this.  Also, you may want to add a check to ensure they haven't added a new line.

 

Hope that helps

Elliott

 

unidentified user
jack6400
Posts: 6
Registered: 01-31-2012
0

Re: Preventing a record write from a script triggered at table write pre-write

Elliot,

I implemented what you gave me but I'm running into a problem.  The two record strings aren't matching, even when there has been no change to the records besides the Part status.  I printed the record strings of my last try and they are identical except for the last several characters.  Maybe you can make sense of this but, for the life of me, I cannot figure out why they are different. 

OrigRecord

***0051585Š000001Š00000100000000ŠMOB-FC-AFŠ1ŠMobil 3 Micron Filter

ElementŠŠNŠYŠNŠ000Š3ŠŠEACHŠNŠNŠŠ450000000Š410000000ŠNŠNŠNŠŠŠŠŠŠŠŠŠŠŠNŠNŠ20110831ŠŠ00000000000001ŠNTŠNŠNŠŠŠNŠNŠŠŠŠ29Š0Š0Š0Š0Š0Š0Š48.72Š22.6819Š1412.88Š1Š0Š0Š0ŠŠŠWOPŠŠŠ***

CompRecord

***0051585Š000001Š00000100000000ŠMOB-FC-AFŠ1ŠMobil 3 Micron Filter ElementŠŠNŠYŠNŠ000Š3ŠŠEACHŠNŠNŠŠ450000000Š410000000ŠNŠNŠNŠŠŠŠŠŠŠŠŠŠŠNŠNŠ20110831ŠŠ00000000000001ŠNTŠNŠNŠŠŠNŠNŠŠŠŠ29Š0Š0Š0Š0Š0Š0Š48.72Š22.6819Š1412.88Š1Š0Š0Š0ŠŠŠWOPŠMŠŠ
***

Moderator
Natasha
Posts: 180
Registered: 07-15-2009
0

Re: Preventing a record write from a script triggered at table write pre-write

It looks like a UDF is null in OrigRecord and it is "M" in CompRecord.  Is "M" the Part status?  CompRecord shows the new value after you put the old value back?

Natasha Chang
Sr. Software Engineer
Sage 100 ERP
Sage Employee
jepritch
Posts: 233
Registered: 08-25-2009
0

Re: Preventing a record write from a script triggered at table write pre-write

I think the "M" is the rowstatus variable that we use internally.  I didn't think this would be there until after the write occurred however.  Is the script on the pre-write event?  or post-write?

 

If this is the pre-write, then we may have a slight problem if this is our internal rowstatus variable as this cannot be set through conventional means.

 

To confirm you can in the Post-Read script retrieve and print the value of "rowStatus$"

 

rVal = oBusObj.GetValue("rowStatus$", rowS)

 

and then do the same in the Pre-Write script.  I would think these should always be the same.

 

Elliott

Sage Employee
jepritch
Posts: 233
Registered: 08-25-2009
0

Re: Preventing a record write from a script triggered at table write pre-write

Hi Jack,

 

So, after setting up your example here, I too ran into the problem you are encountering.  This becaues we internally add fields to indicate whether the record has changed or not, but that field you cannot manipulate as the system maintains it.  I have modified my original script for the Pre-Write to do a field by field compare.  Forgive my VBScript, as there may be better ways to do what I've done, but hopefully it shows what you can do to overcome your problem.

 

If oSession.UserCode = "XXX" and oSession.Updating=0 Then

   currVal = ""
   retVal = oBusObj.GetValue("UDF_PartStatus$", currVal)
   origVal = ""
   retVal = oScript.GetStorageVar("origPartStatus", origVal)

   ' retrieve orig record
   origRec = ""
   origRec = oBusObj.GetOrigRecord()

   ' temporarily set original UDF value back for comparison
   retVal = oBusObj.SetValue("UDF_PartStatus$", origVal)

   compRec = ""
   retVal = oBusObj.GetRecord(compRec, tmpIOL)

   ' put back changed value
   retVal = oBusObj.SetValue("UDF_PartStatus$", currVal)

   ' compare records if NOT the same then something else changed
   
   ' set up arrays for old record, new record and fields in the table
   sep  = Chr(138) ' record separator
   
   ' retrieve column names from MAIN data source
   cols = ""
   cols = oBusObj.GetColumns("MAIN")

   oldR = Split(origRec, sep)
   newR = Split(newRec, sep)
   colN = Split(cols, sep)

   flds = 0
   flds = UBound(colN) 

   ' use the column array to define how many fields to loop through, this does NOT contain our internal fields

   If flds>0 Then
      x = 0
      Do Until x = flds
 
         If oldR(x)<>newR(x) Then
            retVal = oScript.SetError("Can ONLY change the Part Status")
            Exit Do
         End If

         x = x + 1

      Loop
   End If
End If

 

Hopefully that makes some sense and works for you.


Elliott