What is the right way to Fetch data from DB in Logic Hook for On Premise and OnLine).

Hi Team,

Following is my Use case for Both Version(On Line and On Premise).

------------------------------------------------------------------------------------------

I want to read All accounts(Module) from the DB When user fire an event called Before Save in logic hook, and want to do some manipulation on the data.

Once i am done with my work i want to update the same data in DB. It is kind of bulk operation to read and update the data.

I have tried it  with On Premise system and able to read one account from DB. How can i read all existing Accounts?

Following is the snippet to read one account from DB.

$AccountBean = BeanFactory::getBean('Accounts', '5213003c-3ce3-6630-46f6-5791e45f0832');

echo $AccountBean->name;

Regards,

Deepak

Parents
  • Hi Parag Mittal

    Once check this code:

    $db = DBManagerFactory::getInstance();
      
      $query = "SELECT * FROM accounts WHERE deleted = 0";
      $result = $db->query($query, true,"Error reading number of accounts: ");
      while( $row = $db->fetchByAssoc($result)){
      // Do what ever manupulation you want do it here
      $module = "Accounts";
      $id = $row['id'];
      $bcontact = BeanFactory::newBean($module);
      $bcontact->db->query("UPDATE accounts SET database-fieldname ='".$row['field-name']."' WHERE id='".$id."'");
      }
      $bean->approved_declined_c = $bean->modified_by_name;  
      $bean->save();
    

    you can also get through SugarQuery

    Hope this Helps

    Best Regards

    S Ramana Raju

Reply
  • Hi Parag Mittal

    Once check this code:

    $db = DBManagerFactory::getInstance();
      
      $query = "SELECT * FROM accounts WHERE deleted = 0";
      $result = $db->query($query, true,"Error reading number of accounts: ");
      while( $row = $db->fetchByAssoc($result)){
      // Do what ever manupulation you want do it here
      $module = "Accounts";
      $id = $row['id'];
      $bcontact = BeanFactory::newBean($module);
      $bcontact->db->query("UPDATE accounts SET database-fieldname ='".$row['field-name']."' WHERE id='".$id."'");
      }
      $bean->approved_declined_c = $bean->modified_by_name;  
      $bean->save();
    

    you can also get through SugarQuery

    Hope this Helps

    Best Regards

    S Ramana Raju

Children