Scripting

Sofvrg
Tera Contributor

Write an example of a GlideRecord script to retrieve all records from a table called "incident" where the status is "New". is it right? 

var gr = new GlideRecord('incident');

gr.addQuery('state', '1');

gr.query();

while (gr.next()) {

gs.print('Incident Number: ' + gr.getValue('number') + ' - State: ' + gr.getValue('state'));

GlideRecord is a class that allows interaction with tables (in this case, the "incident" table), enabling queries and manipulation of records. In the provided example, "GlideRecord" is used to retrieve incidents with the value "1" associated with the "State" "New". A variable gr is created using the GlideRecord class to interact with the database table. The addQuery('state', '1') method is used to filter incidents where the state equals "1". The query() method executes the actual query to retrieve records that meet the condition.

The while (gr.next()) loop iterates through all records retrieved by the query. For each record, the code executes gs.print('Incident Number: ' + gr.getValue('number') + ' - State: ' + gr.getValue('state')), which prints the incident number and the state associated with the incident. The getValue() method is used to retrieve specific values from the "number" (incident number) and "state" (incident state) columns.

1 REPLY 1

Nikhil Bajaj9
Tera Sage

Hi @Sofvrg ,

 

Yes it is correct.

Regards,

Nikhil Bajaj

Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.
Regards,
Nikhil Bajaj