FoxMcCloud
FoxMcCloud

Reputation: 139

Quickbase delete_record

I am unable to get delete_record function to work. Here is the delete_record code that I am currently using:

$qb->db_id = $myQbTables["table1"]; //using table 1 now

$queries = array( //this is the where clause now for the delete query
  array(
    'fid'  => '12',
    'ev'   => 'EX',
    'cri'  => '1175'
  ),
  array(
    'fid'  => '8',
    'ev'   => 'EX',
    'cri'  => $Child_ID
  )
);

$results = $qb->do_query($queries, '', '', 'a', '1', 'structured', 'sortorder-A'); //get all results
if($results->errCode !== 0) {
  echo $results->errTxt;
}

print_r($results);

foreach($results->table->records->record as $record){ //getting results of delete query
$Deletevar = $record->f[11];    

$deleted = $qb->delete_record($Deletevar); //deleting results

print_r($deleted);  
}

I know that the 'cri' matches things in my quickbase but I can't seem to use f[3] (the record id) to delete it!

NOTE

I have found the main issue I was having! If you are making API calls using the QB API, delete records and purge records does not include the app token!! Please use the following code to update the delete_records function!!!!

public function delete_record($rid) {
if($this->xml) {
                        $xml_packet = new SimpleXMLElement('<qdbapi></qdbapi>');
                        $xml_packet->addChild('rid',$rid);
                        $xml_packet->addChild('ticket',$this->ticket);
                        $xml_packet->addChild('apptoken',$this->app_token);
                        $xml_packet = $xml_packet->asXML();                        

                        $response = $this->transmit($xml_packet, 'API_DeleteRecord');
                }
                else {
                      $url_string = $this->qb_ssl . $this->db_id. "?act=API_DeleteRecord&ticket=". $this->ticket
                                        ."&apptoken=".$this->app_token."&rid=".$rid;

                        $response = $this->transmit($url_string);
                }

return $response;          
}

Upvotes: 0

Views: 454

Answers (1)

Nathan Hawe
Nathan Hawe

Reputation: 281

I think you may be missing something in your query. If you're trying to get a list of records where the field 12 is 1157 and field 8 is equal to $Child_ID you'll need to add AND into your query. When using the API in a URL the query would be &query={12.EX.1175}AND{8.EX.77} if, for example, the child ID were 77. To do that in the PHP SDK for Quickbase you add 'ao' => 'AND' to all arrays following the first query array. Try updating your $queries array to this:

$queries = array( //this is the where clause now for the delete query
                array(
                    'fid'  => '12',
                    'ev'   => 'EX',
                    'cri'  => '1175'
                ),
                array(
                    'ao'   => 'AND',  // Saying that 12.EX.1175 AND 8.EX.$Child_ID
                    'fid'  => '8',
                    'ev'   => 'EX',
                    'cri'  => $Child_ID
                )
);

My PHP skills aren't particularly good, but you'll need to do a bit more to get the record ID#. In the original code, I think that $record->f[3] will just return to SimpleXMLObject for the third field in the array. That won't necessarily be field ID 3 (the record ID). You can either choose to not use the structured format (and change all of your code to match) or add a loop that goes through all the fields and deletes when it reaches the field with id 3:

foreach($results->table->records->record as $record){ //getting results of delete query
    //Loop through all fields to find id 3
    foreach($record->f as $field){
        if($field['id'] == "3"){
            $Deletevar = $field['id'];
            $deleted = $qb->delete_record($Deletevar); //deleting result
            print_r($deleted);
            break;  //Stops this loop since FID 3 was found
        }
    }
}

Some performance notes: If you aren't using any other fields, you can set your query to only return record IDs by passing a clist that is just '3'. Also, each delete_record() is a separate API call. There is another method purge_records that allows you pass a query and Quickbase deletes all of the records that match your query. You may want to look into using purge_records() because you can you use it in place of your do_query() and avoid all the loops. It uses fewer of your resources and Quickbase's to process as well.

Upvotes: 1

Related Questions