cancel
Showing results for 
Search instead for 
Did you mean: 

RAS: set section condition formula

ido_millet
Active Contributor
0 Kudos

Is it possible to set a section condition formula via the RAS API?

A similar question was posted a few months ago by another user, but was unanswered ()

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Hi ido,

It should work. I use this to get the collection so the add is commented out but should work for you:

{

    btnReportObjects.Text = "";

    flcnt = 0;

    foreach (CrystalDecisions.ReportAppServer.ReportDefModel.ReportObject reportObject in rptClientDoc.ReportDefController.ReportObjectController.GetAllReportObjects())

    {

        var reportSection = rptClientDoc.ReportDefController.ReportDefinition.FindSectionByName(reportObject.SectionName);

        for (int index = 0;

                index < reportSection.Format.ConditionFormulas.Count;

                ++index)

        {

            var formula = reportSection.Format.ConditionFormulas[(CrystalDecisions.ReportAppServer.ReportDefModel.CrSectionAreaFormatConditionFormulaTypeEnum)index];

            var NewFormula = reportSection.Format.ConditionFormulas[(CrystalDecisions.ReportAppServer.ReportDefModel.CrSectionAreaFormatConditionFormulaTypeEnum)index];

            try

            {

                if (formula.Text != null)

                {

                    textBox1 = "Condition Formula: " + formula.Text.ToString() + "\n";

                    textBox1 += "Section " + reportSection.Name + "\n";

                }

            }

            catch (Exception ex)

            {

                btnReportObjects.Text += "\nException: " + ex.ToString();

            }

            btnReportObjects.Text += textBox1;

            btnReportObjects.AppendText("\n");

            ++flcnt;

            btnCount.Text = flcnt.ToString();

            //// check the formula by adding it to the report to verify the syntax

            //CrystalDecisions.ReportAppServer.ObjectFactory.ObjectFactory objFactory = new CrystalDecisions.ReportAppServer.ObjectFactory.ObjectFactory();

            //CrystalDecisions.ReportAppServer.DataDefModel.FormulaField Formula = (CrystalDecisions.ReportAppServer.DataDefModel.FormulaField)objFactory.CreateObject("CrystalReports.FormulaField");

            //Formula.Type = CrystalDecisions.ReportAppServer.DataDefModel.CrFieldValueTypeEnum.crFieldValueTypeStringField;

            //Formula.Syntax = CrystalDecisions.ReportAppServer.DataDefModel.CrFormulaSyntaxEnum.crFormulaSyntaxCrystal;

            //Formula.Text = @"whilereadingrecords; ""A"""; // "hello Ludek = 1"; // resultField.Text; // "n=3"; {Customer.Customer Credit ID} + 1 @"whilereadingrecords; ""A"""

            //Formula.Name = "TestDon"; // "TestDon"; //  resultField.Name; //"testformula";

            //String FormulaMessage = rptClientDoc.DataDefController.FormulaFieldController.Check(Formula);

            //if (FormulaMessage == null)

            //{

            //    //then add the conditional formula if it's OK

            //    string newFormulaText = "'It Worked' = 'It Worked'";

            //    // Clone a Section Format and use ReportSectionController.SetPropety to modify the ConditionFormulas

            //    CrystalDecisions.ReportAppServer.ReportDefModel.SectionFormat newFormat = reportSection.Format.Clone(true);

            //    newFormat.ConditionFormulas[(CrystalDecisions.ReportAppServer.ReportDefModel.CrSectionAreaFormatConditionFormulaTypeEnum)index].Text = newFormulaText;

            //    rptClientDoc.ReportDefController.ReportSectionController.SetProperty(reportSection, CrReportSectionPropertyEnum.crReportSectionPropertyFormat, newFormat);

            //}

            //else

            //    btnReportObjects.Text += "There are errors in the formula: " + FormulaMessage.ToString() + "\n";

        }

    }

}

Don

ido_millet
Active Contributor
0 Kudos

Thanks, Don!

Note that checking the condition expression doesn't seem to work for background color expressions (which are Numeric).  After setting the formula type to numeric using:

oFormulaField.Type = CrystalDecisions.ReportAppServer.DataDefModel.CrFieldValueTypeEnum.crFieldValueTypeNumberField

The act of assigning the formula Text reverts the Type to StringField if the expression is a String.  It doesn't matter if the assignment of TEXT happens before or after attempting to set the formula data type.

0 Kudos

Thanks Ido,

I just posted one type of Condition to test for, Inteli-sense and Object Browser will list all of them...

ido_millet
Active Contributor
0 Kudos

Hi Don,
Perhaps my comment was not clear.  Adding a string expression ("""some text""") to a numeric formula object changes its data type to string (it overrides the data type explicitly set in code).  This then causes the .Check() method to fail to detect the problem.

0 Kudos

Hi Ido,

Isn't that what it is supposed to do?

When I modify the background color the formula Wizard adds this:

// This conditional formatting formula must return one of the following Color Constants:

//

// Color (red, green, blue)

// crBlack

// crMaroon

// crGreen

// crOlive

// crNavy

// crPurple

// crTeal

// crSilver

// crRed

// crLime

// crYellow

// crBlue

// crFuchsia

// crAqua

// crWhite

// crNoColor

//

if {Orders.Order ID} = 1430 then cryellow else crred

Can you paste your code in and I'll try it also, there are so many different object and other parts I can't guess which one you are using try them all. And a report with just the one object you are attempting to change.

You can send it directly to me if you want.

Thanks

Don

ido_millet
Active Contributor
0 Kudos

Hi Don,

The key objective is to detect BAD expressions in format condition formulas.

Problem 1: There is no .Check() method for such Formulas. Only for REGULAR formulas.

Solution 1: create a dummy regular formula with that same expression and .Check() it.

Problem 2: If the expression we check is bad due to it being a String rather than Number or Boolean, it will not be detected as bad because the act of assigning the bad expression to the dummy formula changes its data type.

Solution 2: ask DEV to not change the data type (if already assigned) of a formula when assigning an expression to it.

To replicate: 1. create a formula in code  2. Set its data type to numeric  3. set its expression to a string ("""Some Text""").  Note that the formula data type somehow just changed to String. And a .Check doesn't detect a problem.

One more thought: a much better solution would be if DEV added a .Check() method for Format Condition Formulas, so we don't have to jump through hoops to check them.

0 Kudos

Great suggestion Ido...

Add your Enhancement to Idea Place.

DEV does look at them...

PS - adding the POC as a print option for our button which uses P2P looks likes it's going to be added.... So you'll be able to select either one....

Thanks again

Don

ido_millet
Active Contributor
0 Kudos

That is good news (POC mode for Print button)!

I'll post the request for .Check() on the IdeaPlace

Thanks!

Answers (0)