Step 3: Add code to the if and else statements
The code you write in this step should go between the braces in the if and else statements, as follows:
string hMode = Dispatch.EitherField("HiddenMode");
if (hMode == "Save")
{
// Add code to save the data entered by user. See step 1 below.
}
else
{
// Add code to display the Sage CRM forms. See step 2 below.
}
- Add code to the if statement.
If the HiddenMode field value is Save, the following code populates the three tables with the data the user entered on the Sage CRM forms, saves the changes, and then redirects the user to the summary screen of the created company:
// Populate tables with data.
Record recCompany = new Record("Company");
screenCompanyBoxLong.Fill(recCompany);
recCompany.SaveChanges();
Record recPerson = new Record("Person");
recPerson.SetField("pers_companyid",recCompany.GetFieldAsInt("comp_companyid"));
screenPersonBoxLong.Fill(recPerson);
recPerson.SaveChanges();
recCompany.SetField("comp_primarypersonid",recPerson.GetFieldAsInt("pers_personid"));
recCompany.SaveChanges();
Record recOppo = new Record("Opportunity");
recOppo.SetField("oppo_primarycompanyid",recCompany.GetFieldAsInt("comp_Companyid"));
screenOppo.Fill(recOppo);
recOppo.SaveChanges();
// Redirect the user to the new company summary screen.
// Action key 200 stands for the company summary screen.
// Make sure to pass the company ID
// for the created company record:
// split action key URL and add the key1=CompanyId.
string rUrl = Url("200");
string[] split = rUrl.Split(new Char[] {'&'});
string newUrl = split[0] + '&' + split[1] + "&Mode=1&CLk=T&key0=7&key1=" + recCompany.GetFieldAsInt("comp_companyid");
Dispatch.Redirect(newUrl);
// Do not add any code in the else statement after the Dispatch.Redirect method.
// Otherwise, system performance may be impaired. See the note after this procedure. - Add code to the else statement.
If the HiddenMode field value is any other except Save (that is, Edit), the code in this step creates a vertical panel and adds the three entry boxes to it.
Make sure to add the HiddenMode hidden field to the class for the form to work properly.
AddContent(HTML.InputHidden("HiddenMode", ""));
VerticalPanel vpMainPanel = new VerticalPanel();
vpMainPanel.AddAttribute("width", "100%");
screenCompanyBoxLong.GetHtmlInEditMode();
screenPersonBoxLong.GetHtmlInEditMode();
screenOppo.GetHtmlInEditMode();
vpMainPanel.Add(screenCompanyBoxLong);
vpMainPanel.Add(screenPersonBoxLong);
vpMainPanel.Add(screenOppo);
AddContent(vpMainPanel);
The redirect only occurs after the .NET dll is finished processing the code, and needs to provide the HTTP response before being unloaded from memory. Only use the Dispatch.Redirect() method inside the BuildContents() method and return it after the redirect is set, otherwise system performance could be seriously impacted. Use only one Dispatch.Redirect() method in your code, and the set the URL in any other places in the code.
Now add buttons as described in Step 4: Add Save, Cancel, and Help buttons.