NetTalk Central

Author Topic: Change form action  (Read 3942 times)

Matthew51

  • Full Member
  • ***
  • Posts: 151
    • View Profile
    • Email
Change form action
« on: August 06, 2019, 01:28:27 PM »
NT: 10.36

My solution to a form with a child browse on it is to crate the parent record right before the first child is added. I'm having trouble convincing the program to switch from change to inset (If the user doesn't add a child record I was to remain on insert).

In a test button I've put:
Code: [Select]
p_web.ssv('PromotionForm_CurrentAction', Net:ChangeRecord)
p_web.FormSettings.action = Net:ChangeRecord
Do SetAction
Do CompleteForm
!p_web.SessionQueueToFile(Promotions)
dbgView('StoreID: ' & PMS:StoreID)
dbgView('Description: ' & PMS:Description)
PMS:PromotionID = st.MakeGuid()
dbgView('PromotionID: ' & PMS:PromotionID)
p_web.formsettings.recordid[1] = PMS:PromotionID
p_web.formsettings.FieldName[1] = 'PMS:PromotionID'
!Access:Promotions.Insert()
p_web.AddFile(Promotions)
p_web.ssv('UpdateNow', 1)
p_web.ssv('PromotionForm:Primed', 1)

And in procedure setup I've put:
Code: [Select]
  IF band(p_stage, Net:InsertRecord) = Net:InsertRecord AND p_web.gsv('UpdateNow') = 1
        p_stage -= Net:InsertRecord
        p_stage += Net:ChangeRecord
        p_web.FormSettings.action = Net:ChangeRecord
        dbgView('Changing State')
  END
  dbgView('Stage: ' & p_stage)
  dbgView('UpdateNow: ' & p_web.gsv('UpdateNow'))
  dbgView('Form action: ' & p_web.FormSettings.action)

But I still get a duplicate key error or the primary key.

Also I'm trying to prime the PK using the template, but the value keeps getting erased. The value used to be a SQL Identity, but I changed to make knowing the PK easier.

Thanks Matthew
Contractor with 10+ years of NetTalk experience looking for work.
www.linkedin.com/in/matthew-leavitt
BisWare.ca
Check out my free EasyTime Template

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11193
    • View Profile
Re: Change form action
« Reply #1 on: August 07, 2019, 03:57:09 AM »
>> My solution to a form with a child browse on it is to crate the parent record right before the first child is added.

The standard NetTalk approach is to create the parent record when the form is opened.
that's done automatically, assuming the settings on the Form Advanced tab which relate to this are on.

cheers
Bruce

Matthew51

  • Full Member
  • ***
  • Posts: 151
    • View Profile
    • Email
Re: Change form action
« Reply #2 on: August 07, 2019, 02:22:29 PM »
That isn't working even if I set "Prime Auto Inc even if not necessary", and it's not telling me why. I'm guessing it has something to do with how a few columns need to be set null, but I'm not sure where to do this.  I tried after "PreInsert" but that didn't work.

I also would like to create the parent right before the first child is added as I can force the user to fill in all the required information, and I am very confident the user is committed to creating the record, and won't navigate away leaving a ghost record that needs to be deleted (or click that convenient X in the popup window).

Thanks
Matthew
Contractor with 10+ years of NetTalk experience looking for work.
www.linkedin.com/in/matthew-leavitt
BisWare.ca
Check out my free EasyTime Template

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11193
    • View Profile
Re: Change form action
« Reply #3 on: August 07, 2019, 06:44:25 PM »
>> I also would like to create the parent right before the first child is added

go for it then. I don't know if I'll be much help though because I've not done it this way myself.
I imagine you'll need to change the action in the FormSettings queue. See netweb.clw for what I mean.
(all the "details" of a form are stored in the FormSettings so if you change a form from insert to change half way through it's likely this queue record will need a few changes at that time.)

cheers
Bruce

Matthew51

  • Full Member
  • ***
  • Posts: 151
    • View Profile
    • Email
Re: Change form action
« Reply #4 on: August 09, 2019, 01:36:49 PM »
I did some poking around in that file and I got a solution that works. I'll post it here in case anyone else wants to do something similar.

At the end of SetFormSettings I added:
Code: [Select]
IF p_web.FormSettings.action = Net:InsertRecord
    p_web.ssv('PromotionFormSate', p_web.FormState)
ELSE
    p_web.DeleteSessionValue('PromotionFormSate')
END
IF p_web.IfExistsSessionValue('PromotionFormTempCreated')
    p_web.DeleteSessionValue('PromotionFormTempCreated')
END
If I was using a form to create the child record I could probably just read p_web.FormSettings.ParentState when writing the child record. But I'm using a button on a child browse, so I needed to save the form state in a session value

At the start of CancelForm I added:
Code: [Select]
IF p_web.FormSettings.action = Net:ChangeRecord AND p_web.gsv('PromotionFormTempCreated') = 1
    p_web.OpenFile(SQLFile)
    PMS:PromotionID = p_web.gsv('PMS:PromotionID')
    Bind('PromotionID', PMS:PromotionID)
    Bind('Result', ErrorCode#)
    SQLFile{PROP:SQL} = '&Result = CALL [Inventory].[DeletePromotion](&PromotionID[IN])'
    p_web.CloseFile(SQLFile)
END
The template does add code to delete the record if the session value [ProcedureName]:Primed is set to 1, but I wanted to delete it my way.

Right before the child record is added we check it we need to create the parent:
Code: [Select]
IF p_web.IfExistsSessionValue('PromotionFormSate')
    FormStateString = p_web.gsv('PromotionFormSate')
    p_web.RequestData.WebServer._GetSettingsQueue(p_web.SessionID, FormStateString, TempFormState)
    IF TempFormState.action = Net:InsertRecord
        p_web.OpenFile(Promotions)
        p_web.ssv('PromotionForm_CurrentAction', Net:ChangeRecord)
        p_web.SessionQueueToFile(Promotions)
        SetNull(PMS:EventID)
        IF p_web.AddFile(Promotions) = Level:Benign
            p_web.RequestData.WebServer._Wait()
            p_web.RequestData.WebServer._GetSettingsQueue(p_web.SessionID, FormStateString, TempFormState)
            p_web.RequestData.WebServer._SettingsQueue.Settings.action = Net:ChangeRecord
            Put(p_web.RequestData.WebServer._settingsQueue)
            p_web.RequestData.WebServer._Release()
            p_web.DeleteSessionValue('PromotionFormSate')
            p_web.ssv('PromotionFormTempCreated', 1)
        END
        p_web.CloseFile(Promotions)
    END
END
If you adding the child using a form then something similar would likely go in the PostInsert embed.

Edit: I wasn't comfortable putting file I/O in a critical section so I changed the last embed. I get the form state twice just in case, but the critical section is very small.
« Last Edit: August 09, 2019, 01:52:04 PM by Matthew51 »
Contractor with 10+ years of NetTalk experience looking for work.
www.linkedin.com/in/matthew-leavitt
BisWare.ca
Check out my free EasyTime Template