How to fetch all records from Account Module and also updating a field value in all record

Hi Folks,

I want to get All records from Accounts Module and want to do some manipulation on that data.

Once it is done then i will update the data to same record.

How to do this bulk operation read and update the all records.

Thanks in Advance

Regards

John

Parents
  • Hi Follow John

    You can do it by both ways SQLQuery or SugarQuery.

    Using SQLQuery to do so:

    follow this link.

    Using sugarQuery to do so:

    $bean = BeanFactory::getBean('Accounts');
    $query = new SugarQuery();
    $query->from($bean, array('team_security' => false));
    $query->where()->equals('deleted', '0');
    $account_list = $query->execute();     
    if ( $account_list != null ) {
       foreach ($account_list as $acct) {               
         $accounts = new Account();
         $accounts->retrieve($acct->id);
         $accounts->name = 'Updating by Ramana';
         $accounts->save();
       }
    }

    Hope this Helps..!!

    Best Regards

    S Ramana Raju

  • Hi Ramana! Sorry for the dumb question but I'm new to sugar. Can you tell me where to put this code? If I have to go through customization. 

  • Hi Hijab Fatima,

    Would you please explain your use case? Are you trying to fetch the data in logic hook or some custom endpoint or custom script? Based on your use case, you can include the above code script anywhere in the files written inside sugar scope. For external files, you cannot use SugarQuery class.

    Please provide us more info and we could help you.

    Regards.

Reply
  • Hi Hijab Fatima,

    Would you please explain your use case? Are you trying to fetch the data in logic hook or some custom endpoint or custom script? Based on your use case, you can include the above code script anywhere in the files written inside sugar scope. For external files, you cannot use SugarQuery class.

    Please provide us more info and we could help you.

    Regards.

Children
  • Hi hats,

    Thanks for the reply. What I'm trynna do is in the campaigns module I have created a new action named Hello World and have displayed all of the campaign's data in that layout. Now what I am doing is I have added a search bar and I wanted to fetch the records of campaigns module on the base of id. 

  • Hi Hijab Fatima,

    What I have understood from your question is that you have a custom layout where you display all the campaign records. On top of that, you have added a search bar where upon search, you would like to list campaign data result.

    This can be done using ajax call (from JS file) and a custom backend script (API or entry point if you are using sugar 6.x). When user enters a query string in search bar, you can pass the request to backend PHP script (API or entry point) and use the sugar query (shown above) to retrieve the data and return it back to your JS file and show it on the UI.

    Please let me know if this helps.

    Regards.

  • hi hat 

    Thanks for all of your support. I have attached the attachment below

    Please let me know Am I on the right track? 

  • Hi Hijab Fatima,

    There are multiple ways to do what you are trying to achieve. You can separate the backend programming from front end by keeping your js code, html template and back-end programming in separate files as sugar does.

    It takes a while to follow that approach but makes it easy to manage if too many things happening around. As long as the above piece of code serves the purpose, you can use it.

    Would be better if you take opinion from other developers as well about the best practices.

    Regards.