3

I'm getting user name and mail id through sitecore form. on clicking submit the sitecore form submit action has to get triggered. In turn the following code has to get triggered. But the debug point is not hitting. But its showing the thank you message which i have mentioned in the sitecore form. I need to create a contact using the given user credentials which i'll be viewing in Experience profile. Since the code is not getting triggered the contacts are not getting saved so i can't view them in Experience Profile. Kindly suggest me some solutions. Thanks in advance.

public class LoginFormSubmit : SubmitActionBase<string>
    {
        public LoginFormSubmit(ISubmitActionData submitActionData) : base(submitActionData)
        {

        }
        protected override bool TryParse(string data, out string target)
        {
            target = string.Empty;
            return true;
        }
        //Override the Execute method to insert data to API
        protected override bool Execute(string data, FormSubmitContext formSubmitContext)
        {
            ApiModel customData = new ApiModel();
            try
            {
                Log.Info("FormSubmit Execute Method Started..", typeof(FormSubmit));
                var firstNameField = GetFieldByName(ApiConstants.FirstName, formSubmitContext.Fields);
                customData.First_Name__c = GetValue(firstNameField);

                var emailField = GetFieldByName(ApiConstants.Email, formSubmitContext.Fields);
                customData.Email__c = GetValue(emailField);

                CreateContact contact = new CreateContact();
                contact.CreateContactBasedOnSession(customData.First_Name__c, customData.Email__c);

                return true;
            }
            catch (Exception e)
            {
                Log.Error("Login Save:", e, typeof(FormSubmit));
                Log.Error(string.Concat("Login save", e.Message.ToString()), typeof(FormSubmit));
                return false;
            }
        }
        private static IViewModel GetFieldByName(string fieldName, IList<IViewModel> fields)
        {
            return fields?.FirstOrDefault(f => f.Name == fieldName) != null ? fields.FirstOrDefault(f => f.Name == fieldName) : null;
        }
        private static string GetValue(object field)
        {
            return field?.GetType().GetProperty("Value")?.GetValue(field, null)?.ToString() ?? string.Empty;
        }
    }

enter image description here

8
  • Welcome to Sitecore Stack Exchange. How did you create the custom form action in sitecore? Does it inherit from /System/Forms/Submit Action template, also the Model Type field is correctly setup with your class name and the action is assigned to the form? Commented Jul 13, 2021 at 14:58
  • I have added the image link for your reference. i.sstatic.net/2XKyo.png
    – Indu Mathi
    Commented Jul 13, 2021 at 15:27
  • I assume that the custom action is also added to the form and you are using ajax post, are you getting any errors in the log files when submitting the form? Also in visual studio you are seeing the red breaking point correctly, right? Commented Jul 13, 2021 at 15:43
  • Welcome to Sitecore stack exchange, make sure you deployed the dll from debug mode Commented Jul 13, 2021 at 22:09
  • 1
    Can you try setting the value in your ModelType without the assembly name, like Prudential.Feature.Forms.Models.Product_Form.LoginFormSubmit and see if there is any difference or error happening in log files aswell? Commented Jul 14, 2021 at 6:30

2 Answers 2

3

Yes, This issue is related to parsing the TParametersData from the SubmitActionBase as @Zephyr mentioned.

If you don't want to create a separate model based on TParametersData and you want to bypass parsing like me. You can follow the below approach, It works for me!

Solution below,

We have a Tryparse method from the SubmitAction, You have the control to override this!

enter image description here

Under your custom form submit action, have a method like this,

 public class CustomFormSubmit : SubmitActionBase<string>
    {
        protected override bool TryParse(string value, out string target)
        {
            // Do your own logic to parse
            return true;
        }
    }

This will avoid parsing-related issues & your debugger will hit at every step in Visual Studio.

1

Here's my 2 cents for future reference though I'm late to the party.

Assuming error message was shown after form submission, this would mean we failed parsing to desired data type TParametersData (for scenario above, it's of string type).

This logic was defined in ExecuteAction under SubmitActionBase class and we can override it if needed with following method signature:

public virtual void ExecuteAction(FormSubmitContext formSubmitContext, string parameters)

In this case, Execute will then execute only if the parsing was completed successfully. I suggest to create an appropriate model for TParametersData instead of using string as shown in official documentation below: https://doc.sitecore.com/xp/en/developers/90/sitecore-experience-manager/walkthrough--creating-a-custom-submit-action-that-updates-contact-details.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.