custom button and sc_redir, how to pass parameter?

Hello, I have a quick question.

I have a form that opens in a modal, and here you can add a new record.

The PK field is not displayed (recordID), because it’s autoincrement, so whenever you add a new record, it will auto assign an ID.

Up to here everything works, I can save the record and it will show in the Grid application.

I have an issue where I create a php Button for this form, called “Save and view details”.

What this button should do, once the form for the new record is filled, instead of just using the default Save button, I want another button that does the same, it saves this record, but then, it redirects to another form, where I can add more details for this record.

Usually what I would do is, add a new record, go to my Grid, see the new record and click on it (each record has a field link so you can click on it), after clicking I would get redirected to the second form (record_details) where I show some details about the record.

So to avoid all these extra steps, how can I code that custom button to Save as the default button, but then redirect to my other form? The other form takes only one parameter as input, which is the “recordID”, the one that gets autoassigned when creating a new record.

I tried creating a button as php, and adding this code:


$sql_insert = "INSERT INTO dbo.TblItem(ItemTyp, ItemName, ItemBeschreibung, ItemProduktID, ItemKundeID, ItemMitarbeiterID) VALUES ( '{ItemTyp}','{ItemName}','{ItemBeschreibung}','{ItemProduktID}','{ItemKundeID}','{ItemMitarbeiterID}')";
    sc_exec_sql($sql_insert);


sc_redir('record_details', parm1={ItemID}, '_parent');

so the insert part works correctly, the record gets saved with the correct values,
what doesnt work is the SC_REDIR, I get redirected to my “record_details” page, but without any record selected, so that means no record was passed (this form is just to modify a single record, so it takes as input the record ID and shows all the informations).

I’m not sure what the issue is.

Does it matter that when creating a new record, the ID as not being shown because of autoincrement, when I press my custom button, it cannot somehow retrieve the ID? even if I use the field value of the ID?

Is there any reason why you are not using a master/detail approach?
That would make things simpler.

As far as your approach is concerned, yes the issue is likely the not yet existing record id.
In that scenario, with MySQL as a back end i was able to workaround that issue by using a select statement, executed just after the insert statement, to retrieve the ID of the last inserted record.

Somehing like this:

$sql_insert = "INSERT INTO ....";

sc_exec_sql($sql_insert);

sc_lookup(id,"SELECT LAST_INSERT_ID()");

$new_itemID=$id[0][0];

sc_redir('record_details', parm1=$new_itemID, '_parent');

thank you for the answer.
So yes, I’m already using a Master/Detail view on the main grid, but then, on that grid there is a button to add a new record, which pops a modal.
In this modal you can only define the main and required parameters of the new record, then usually when added, the modal would close and you would see the new record in the main grid, and if you want to fill out some others parameters for that record, you open it up by clicking on a field link in the grid and it would open the detailed view of that record.

the reason I m trying to achieve this, is to being able to go directly from the modal where you add the record, to the detailed form.
This would be just a nice addition, as currently we can already go in the detailed form once a record is added with the main grid which is Master/detail.

I’ll try your solution, seems promising, thank you!

is the syntax correct? especially in the sc_lookup you use ‘id’, but then you call it $id? I tried different variations of this syntax but it isn’t working,
how can I console.log here in php to see if $new_itemID variable has the correct id?

Because this code is not working, I get redirected to my detailed form, but the id is not passed correctly as it doesnt open any record inside, just an empty form with above the message ‘‘no records found’’

Can’t you add the detailed form as detail to the modal (master) form?
That way you can show the details only on update; once the record in the master modal form is saved then the detail form will be correctly linked and usable within the master form
You can play with tabs\blocks and some js code to automatically select the detail tab (where the detail form is shown) after the record has been added.
The end rseult could be similar to what you’re trying to achieve with sc_redir.

ok I m not sure I got what you are suggesting, but maybe if I explain to you why I want this button, you can tell me if your idea would be a nice solution.

We want this button because usually we add records only with the important fields filled (the required ones) and only those are shown in this modal, so on the grid you click on the “add new record” button, and quickly fill those few fields and add the record and the modal closes and you are back to the grid.
So you can add quickly many records, and later on if you want to add more details, there is a Field link on the main grid, for each record, that if clicked, you ll be redirected to the “detailed view” for that record (the one I m trying to implement with this code you suggested) and there you can fill some secondary fields for that record.

The idea of this new button was, in case you want directly to fill those secondary fields on the “add new record” modal, instead of just saving and going back to the grid, and then selecting the record to go on the detail view, you press this new button from the modal “Save and go in detail view”, and it will add the record and redirect you directly on the detailed view

From what I understood by your suggestion, having a master/detail in that modal, would mean that the “detailed view” would always be visibile in that modal, correct?
As 70% of the record added don’t need the Detailed view as we don’t fill those secondary fields for them, this button was for the other 30% so you can quickly go into detail view directly from the modal!

The detailed view can be put in a block and the block can be conditionally shown\hidden: there’s an SC macro for that.

Hello, I will definetely try this approach, but I’m giving one last try to my attempt solution.

I made some progress, with LAST_INSERT_ID() I couldnt retrieve the ID as we discussed, but with:

SCOPE_IDENTITY() it works, I echo the result and the ID is correct and it gets retrieved right after the insert,

but someone the sc_redir doesnt work! it redirects corretly to the application I want, but the parameters isnt passed, as it opens a no records page, is my syntax correct? I tried multiple variants but no success

$new_itemID=$id[0][0];

sc_redir(‘item_details’, param = $new_itemID, ‘_parent’);

the new_itemID variable is storing correctly the newly created ID as I did an echo on it and it show correctly

the variants I tried are, using ‘;’ instead of a simple comma after the param,
then trying to only put the param without the declaration “param = …”

Don’t know what else to try! any suggestions?

EDIT: ok nvm, I found the solution myself, as I usally was passing an ID to this application by a Field Link, now that I was using sc_redir there was no variable defined to store the ID, so what I did is just declare a WHERE clause with a global var, and then pass the ID into that var inside the sc_redir, now it works perfectly!!

And for future reference, to retrieve the ID in the same code where the sql_insert is, we need to use SCOPE_IDENTITY() and not LAST_INSERT_ID()

as my issue above is solved, I would ask you one more thing, in this case I’m using sql_insert to add the record, but there is an issue.

When I add the record with the standard “save” button, if some required fields are not filled, I would get the error and the record wouldn’t be inserted!

But with this custom code, even if some required fields are not filled, the sql_insert will execute and I would get a SQL error and the application would freeze, as I have required fields also set in the DB, so they can’t be left empty!

Is there a way to add a check in the sql_insert, to see if the field is empty and pop an error message and not execute the insert?

That’s totally DB dependent.
LAST_INSERT_ID works with MySQL (as I wrote in my post).
Never heard of SCOPE_IDENTITY, but by a quick google search seems to be the equivalent function to be used with MS SQL:
https://stackoverflow.com/questions/…ntity-in-mysql

ok I see, indeed we are using MSSQL!!
any idea about the issue regarding required fields?

You can check that fields are populated before executing the sql insert, using the standard SC syntax for reading fields (i.e. {field_name}).

And if not all required fields are populated, run sc_error_message() instead of the SQL insert.

thank you for the answer,
I implemented this check, but there is a small issue.

Everything works, the insert doesn’t execture if all required fields aren’t filled, but the sc_error_message is acting strange.

If I just put in the if () condition this:

sc_error_message(“Error: every required field must be filled”);

when I don’t fill the required fields and try to save, an empty page will load in the same window, with just an “OK” button in the middle of it, if the button is pressed, I will get back to my form application where I can fill the required fields.

But the message is not displayed!

Then I tried this:

sc_error_message(“Error: every required field must be filled”);
sc_error_exit();

now I get the same white page, with the “OK” button, but below it there is my message! in a Yellow box.
And same as before, If I press the “OK” button I will get redirected correctly to the previous view where I can add the missing fields!

2 things aren’t working as expected,

first of all, I would like to have the yellow box error message displayed in the SAME place as my form, so no new white page loading with the OK button, but just a error message somewhere inside my form when I try to save without all required fields.
Then why without sc_error_exit my error message wouldn’t display?

And finally, the issue with the new white page loading is, that when I press the “OK” button and get redirected back to my form, all the fields I have already filled before pressing SAVE, are being reset and are empty!

So if someone forgets only 1 required field, and presses the button to Save, he will see a white page with the error message, but when he clicks it, the entire form will be empty again so he needs to fill it up again from scratch!! That is not good!

I just want a message to display on top of the form if the if() condition isn’t met!

i’m not sure at all, but setting Check Application \ Settings \ Ajax Error Output to on could help.
But maybe you don’t need to throw an error at all and you coud just use sc_ajax_message() ?

Ajax Error output is enabled.

I tried this:

sc_ajax_message(‘test’,‘Error’,‘modal=Y&button=Y&button_label=Understood’);

but nothing happens. (always redirects to empty page with an “OK” button)

I noticed that if the first IF clause is triggered (meaning not all fields have been filled), I will always have my modal (which contains the form) load a empty page with just a “OK” button,
this happens even if I dont put any code in that IF() clause, and once the OK button is pressed, the form will just load back in the modal like nothing happened, but it will be reset with all the fields empty!

If I could avoid to going to this empty page with the OK button, I could just ECHO a message to say to fill all the fields

to give more info, this is a FORM application, that gets opened in a Modal, here I have on this modal a Special button, that runs PHP code, and the code is the following:


if ({ItemName} == NULL || {ItemProduktID} == NULL || {ItemKundeID} == NULL || {ItemMitarbeiterID} == NULL) {


sc_ajax_message('test','Error','modal=Y&button=Y&button_label=Ok');


} else {

$sql_insert = "INSERT INTO dbo.TblItem(ItemTyp, ItemName, ItemBeschreibung, ItemProduktID, ItemKundeID, ItemMitarbeiterID) VALUES ( '{ItemTyp}','{ItemName}','{ItemBeschreibung}','{ItemProduktID}','{ItemKundeID}','{ItemMitarbeiterID}')";

sc_exec_sql($sql_insert);

sc_lookup(id,"SELECT SCOPE_IDENTITY()");

$new_itemID=$id[0][0];

sc_redir('item_details_newly_added', selectedItemID=$new_itemID, '_parent');

}

the ELSE part works perfectly when all required fields are filled, just the first part is not ok, even if no code is there, I will always get redirected to this empty page with the OK button

Try the sc_ajax_message with no options but the message itself.
Also, what kind of form button are you using?
PHP buttons “go” somewhere and have a specific target setting: “same window” or “other window”.
Maybe in your scenario an ajax button would be better? I don’t remember it sc_redir is usable with ajax buttons.

sadly the message without options was the first thing I tried!

The button is a PHP button with the code I copied in my last post!

Anyway I tried selecting “other window”, and this could be a temporary fix, because it always loads an empty page with the OK button, but if I select “other window”, it will open this in a new tab, so when I click “OK”, my modal form will still be there with the fields NOT reset, so if the user already filled something, it will stay like that!

Not ideal but better than nothing.

Basically I dont know what s best in my scenario, but the end result I would like is:

I have a form that opens in a modal, when you fill the form and press the custom button, there are 2 options:

A) you filled every field so you get redirected to another page using the new record just created, THIS WORKS perfectly
B) you forgot to fill some fields, you should just get a message on the SAME page (on the modal), anywhere, so you know you need to fill the rest of the fields, and once all are filled, if you press the custom button what is written above (point A) will happen

Did you try with an AJAX button instead of the PHP button?
By definition AJAX won’t load\reload any page.
But i don’t remember if sc_redir cen be executed inside an AJAX button

use a sc_exit(ref) or similar or use sc_redir as the last statement in your event code.

I just tried with an Ajax button and:

  • sc_ajax_message is shown as a popoup (no opening\redirecting to anywhere)
  • sc_redir() works

I think using an Ajax button instead of a PHP button should work in your scenario as well.