Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > Insert Record into Custom Table Using BizForm OnAfterSave Sample Code View modes: 
User avatar
Certified Developer 9
Certified Developer 9
rweber-weblications - 8/3/2012 10:07:22 AM
   
Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
I was hoping one of the resident Kentico experts could provide me with some sample code for opening a database connection and inserting field data from the the bizform into a custom table. I want to execute this code after the bizform has been saved. Thought I could use the OnAfterSave Event. Thanks.

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/6/2012 6:16:20 PM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
 public void Insert_After(object sender, ObjectEventArgs e)
{

if (e.Object.ObjectType.ToLower() == "bizformitem.bizform.your_bizform_codename")
{
CustomTableItem item = new CustomTableItem("customtable.yourtableclassname");
item["ColumnName"] = e.Object.GetValue("ColumnName");
item.Insert();
}
}

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/6/2012 6:19:03 PM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
You can put this in the file /app_code/samples/modules/sampleclassloadermodule.cs
   public override void Init()

ObjectEvents.Insert.After += new EventHandler<ObjectEventArgs>(Insert_After);
}
public void Insert_After(object sender, ObjectEventArgs e)
{

if (e.Object.ObjectType.ToLower() == "bizformitem.bizform.your_bizform_codename")
{
CustomTableItem item = new CustomTableItem("customtable.yourtableclassname");
item["ColumnName"] = e.Object.GetValue("ColumnName");
item.Insert();
}
}

User avatar
Certified Developer 9
Certified Developer 9
rweber-weblications - 8/8/2012 4:39:58 PM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
Thanks - I will try this out tomorrow.

Bob

User avatar
Certified Developer 9
Certified Developer 9
rweber-weblications - 8/10/2012 10:00:50 AM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
I created this code from your sample and am getting an object error:

System.EventArgs' does not contain a definition for 'Object' and no extension method 'Object' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?

Here is the code I added the viewBiz_OnAfterSave:
        //Insert Information into message table after the form is processed
if (e.Object.ObjectType.ToLower() == "bizformitem.bizform.check_request")
{
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
string customTableClassName = "Messaging_Message";
CustomTableItem item = CustomTableItem.New(customTableClassName, customTableProvider);

//CustomTableItem item = new CustomTableItem("Messaging_Message");
item["MessageSenderUserID"] = e.Object.GetValue("UserID");
item["MessageSenderNickName"] = e.Object.GetValue("FirstName") + " " + e.Object.GetValue("LastName");
item["MessageRecipientUserID"] = e.Object.GetValue("ManagerID");
item["MessageRecipientNickName"] = e.Object.GetValue("ManagertName");
item["MessageSent"] = DateTime.Now;
item["MessageSent"] = DateTime.Now;
item["MessageSubject"] = "Check Request Approval Required";
item["MessageBody"] = "Sample Body of EMail Test";
string aguid = Guid.NewGuid().ToString();
item["MessageGUID"] = aguid;
item.Insert();
}

Any ideas?

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/10/2012 12:37:41 PM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
Yeah, you've got the wrong signature for your event handler.
The type of the argument for your handler should be ObjectEventArgs and not just EventArgs

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/10/2012 12:39:45 PM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
Pay Close attention to the type being passed to the Generic EventArgs<T> type when adding the event handler and the signature of the eventhandler:
   public override void Init()

ObjectEvents.Insert.After += new EventHandler<ObjectEventArgs>(Insert_After);
}
public void Insert_After(object sender, ObjectEventArgs e)
{

if (e.Object.ObjectType.ToLower() == "bizformitem.bizform.your_bizform_codename")
{
CustomTableItem item = new CustomTableItem("customtable.yourtableclassname");
item["ColumnName"] = e.Object.GetValue("ColumnName");
item.Insert();
}
}

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/10/2012 12:43:40 PM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
Hang on, I jumped the gun on that one.

I see that you are doing thins in the OnAfterSave event instead of using the Object Events in the SampleClassLoaderModule like I suggested.

You need to gain reference to the Item that was just inserted. I haven't done it this way before so let me look it up.

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/10/2012 12:50:18 PM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
try this :


CMS.FormEngine.BizFormItem formItem = new CMS.FormEngine.BizFormItemProvider().GetItem(viewBiz.ItemID, "bizformitem.bizform.check_request");

if (formItem != null)
{
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(CMSContext.CurrentUser);
string customTableClassName = "Messaging_Message";
CustomTableItem item = CustomTableItem.New(customTableClassName, customTableProvider);

//CustomTableItem item = new CustomTableItem("Messaging_Message");
item["MessageSenderUserID"] = formItem.GetValue("UserID");
item["MessageSenderNickName"] = formItem.GetValue("FirstName") + " " + formItem.GetValue("LastName");
item["MessageRecipientUserID"] = formItem.GetValue("ManagerID");
item["MessageRecipientNickName"] = formItem.GetValue("ManagertName");
item["MessageSent"] = DateTime.Now;
item["MessageSent"] = DateTime.Now;
item["MessageSubject"] = "Check Request Approval Required";
item["MessageBody"] = "Sample Body of EMail Test";
string aguid = Guid.NewGuid().ToString();
item["MessageGUID"] = aguid;
item.Insert();

}



User avatar
Certified Developer 9
Certified Developer 9
rweber-weblications - 8/12/2012 10:28:01 AM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
J-,

Thanks for your help on this. I updated the code and used your suggested code above and got the following error when submitting the form (when trying to save the data in the background) (Making progress I think)
EventDescription: Message: [GeneralConnection.ExecuteQuery]: Query 'bizformitem.bizform.check_request.select' not found.
Stack Trace:
at CMS.DataEngine.GeneralConnection.ExecuteQuery(String queryName, QueryDataParameters parameters, String where, String orderBy, Int32 topN, String columns)
at CMS.FormEngine.BizFormItemProvider.GetItem(Int32 itemId, String className)
at CMSWebParts_BizForms_bizform.viewBiz_OnAfterSave(Object sender, EventArgs e)
at CMS.FormControls.CMSBaseForm.InvokeOnAfterSave()
at CMS.FormControls.BizForm.SaveDataToDB()

The bizform does exist it is called Check_Request (Form Code Name).

any other ideas. I think we are close.

Thanks again.
Bob

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 8/12/2012 7:27:34 PM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
Hi,

could you please check in the CMS_Class table if you are really using a correct code name. For example default ContactUs form has a code name BizForm.ContactUs.

Maybe you should add as a parameter only bizform.check_request.

Best regards,
Ivana Tomanickova

User avatar
Certified Developer 9
Certified Developer 9
rweber-weblications - 8/13/2012 8:36:11 AM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
That did not work. Tried bizform.check_request and form.check__request. I should have probably mentioned this before but I am using Version 7. Have the class names or calls changed in the newest release?

Thanks for you help.

Bob

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/13/2012 9:20:16 AM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
Check the CMS_Class table in the database to make sure what you are passing as an argument matches.

User avatar
Certified Developer 9
Certified Developer 9
rweber-weblications - 8/13/2012 4:40:35 PM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
J-,

Sorry to be so lame, but I do not know what I should be looking for. I looked at the Form record in the CMS_Class Table, but that does not shed any light on what I need to be passing here.

Hasn't anyone ever updated another table following the submission of a form? I could do it with an external file, but would like to do it using the onAfterSave. Aren't there any working examples of it anywhere is the documentation? The Sample stuff is not the same.

Let me know and thanks again for your help.

Bob

User avatar
Member
Member
kentico_michal - 8/7/2012 3:34:42 AM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
Hi,

If you need more information, please also check API examples related to custom tables that are available in the ~\CMSAPIExamples\Code\Development\CustomTables\Default.aspx.cs file.

Best regards,
Michal Legen

User avatar
Member
Member
udaykumarreddy0404-gmail - 11/5/2013 10:56:10 PM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
Hi,

Can you provide me an example on how to update a custom table record from bizform which includes retrieving record from custom table and show that bizform once you modify and click save it should be updated in custom table.


Regards,
Uday Arikatla

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 11/6/2013 1:13:50 PM
   
RE:Insert Record into Custom Table Using BizForm OnAfterSave Sample Code
Take a look at the webpart in the Marketplace that will do all this heavy lifting for you already, all you do is specify the custom table you want to work with and the rest is history.