Creating a New Query
In this example, you are creating a new query, adding a record and two fields. The following is the complete code sample: the steps explain each line.
Local ApiObject &aQuery, &aQrySelCol;
Local ApiObject &COLL, &ERROR;
Local String &TEXT;
Local Session &MySession;
Local Record &aQryRcd;
Local Field &aQryFld;
&MySession = %Session;
If &MySession <> Null Then
&aQuery = &MySession.GetQuery();
&aQuery.Create("TEST1", False, %Query_Query, "PIA Test 1", ⇒
"Creating Test Query 1 from PIA Page");
&aQrySel = &aQuery.AddQuerySelect();
&aQryRcd = &aQrySel.AddQueryRecord("ABSENCE_HIST");
&aQryFld = &aQrySel.AddQuerySelectedField("ABSENCE_HIST", "A", "EMPLID", "ID");
If &aQryFld <> Null Then
&aQryFld.ColumnNumber = 1;
&aQryFld.HeadingType = %Query_HdgRftShort;
End-If;
&Rslt = &aQuery.Save();
If &Rslt <> 0 Then
/* save didn't complete */
&COLL = &MySession.PSMessages;
For &I = 1 to &COLL.Count
&ERROR = &COLL.Item(&I);
&TEXT = &ERROR.Text;
/* do error processing */
End-For;
&COLL.DeleteAll();
End-if;
/* error processing for not getting a session */
End-if;To create a new query:
-
Get a session object.
Before you can create a query, you have to get a session object. The session controls access to the query, provides error tracing, enables you to set the runtime environment, and so on. Then this program checks to verify that the session object is valid.
&MySession = %Session; &aQuery = &MySession.GetQuery(); If &MySession <> Null Then -
Create the query.
Use the Create method to create the query. This query is a private query, of type query.
&aQuery.Create("TEST1", False, %Query_Query, "Test 1", "Creating Test Query"); -
Add a QuerySelect.
The QuerySelect contains the main query statement for the query. There can be multiple QuerySelect objects for queries that involve unions or subqueries. Each select (or union or subquery) consists of QueryRecords, QueryOutputFields, QuerySelectedFields, and QueryCriteria and is treated as a child of the MAIN select statement.
&aQrySel = &aQuery.AddQuerySelect(); -
Add a record and a field.
The AddQueryRecord method adds a query record to the query. The AddQuerySelectedField adds a field, using the record alias "A". The ID is what gets displayed in the heading for the query.
&aQryRcd = &aQrySel.AddQueryRecord("ABSENCE_HIST"); &aQryFld = &aQrySel.AddQuerySelectedField("ABSENCE_HIST", "A", "EMPLID", "ID"); -
Make the field an output field.
The field was added as a selected field. By setting the ColumnNumber to a number greater than one, the field is now an output field. The text that's displayed in the heading comes from the RFT short description of the field.
If &aQryFld <> Null Then &aQryFld.ColumnNumber = 1; &aQryFld.HeadingType = %Query_HdgRftShort; End-If; -
Save the data.
When you execute the Save method, the new query is saved to the database.
&Rslt = &aQuery.Save();If &Rslt <> 0 ThenThe Save method returns a numeric value: 0 if successful. You can use this value to do error checking.
-
Check Errors.
You can check if there were any errors using the PSMessages property on the session object.
/* save didn't complete */ &COLL = &MySession.PSMessages; For &I = 1 to &COLL.Count &ERROR = &COLL.Item(&I); &TEXT = &ERROR.Text; /* do error processing */ End-For; &COLL.DeleteAll(); End-if;If there are multiple errors, all errors are logged to the PSMessages collection, not just the first occurrence of an error. As you correct each error, you may want to delete it from the PSMessages collection.