0 Replies Latest reply on Aug 20, 2019 4:42 AM by Hanna Jean

    How to create a dynamic search expression.

    Hanna Jean

      HI All,

       

      I have a custom object and there are some custom fields in it. From a standard object, I want to search record in this custom object.

       

      Here is the details of my custom object:

      Object Name: ObjectFieldVal

      Object API Name: ObjectFieldVal_c

      Custom Fields:

              Name                          API Name                      Value

      a.    ObjectName               ObjectName_c                Contact

      b.    FieldName                 FieldName_c                   Name

       

      Here is the query I'm using:

      I. First Expression

      a) Multiple search expression

      def objectName = getAttribute('ObjectName_c')

      def fieldName = getAttribute('FieldName_c')

       

      def vo=newView('ObjectFieldVal_c')

      vo.appendViewCriteria("ObjectName_c = '" + objectName + "' + AND FieldName_c = '" + fieldName + "'") or vo.appendViewCriteria("ObjectName_c = Contact " + AND FieldName_c = '" + fieldName + "'")

      vo.executeQuery();

       

      b) Single search expression

      def objectName = getAttribute('ObjectName_c')

      def fieldName = getAttribute('FieldName_c')

       

      def vo=newView('ObjectFieldVal_c')

      vo.appendViewCriteria("ObjectName_c = '" + objectName + "'") or   vo.appendViewCriteria("ObjectName_c = 'Contact '")

      vo.executeQuery();

       

      Both of them doesn't work. But when I try to use this one:

      II. Second Expression

      def objectName = getAttribute('ObjectName_c')

      def fieldName = getAttribute('FieldName_c')

       

      def vo=newView('ObjectFieldVal_c')

      def view_Criteria = newViewCriteria(vo);

      def view_criteria_row = view_Criteria.createRow();

       

      def view_condition1 = view_criteria_row.ensureCriteriaItem('ObjectName_c');

      view_condition1.setOperator('=');

      view_condition1.setValue(objectName );

       

      def view_condition2 = view_criteria_row.ensureCriteriaItem('FieldName_c');

      view_condition2.setOperator('=');

      view_condition2.setValue(fieldName);

       

      view_Criteria.insertRow(view_criteria_row);

      vo.appendViewCriteria(view_Criteria);

      vo.executeQuery();

       

      It works.

       

      But we need to use the first one for our dynamic search expression. We really don't know why the first expression doesn't work even if we directly assigned a value to it.

       

      If you know why it doesn't work or you know other way to create a dynamic search expression, I'll be glad to know it.

      Thank you in advance.