MinnPost / object-sync-for-salesforce

WordPress plugin that maps and syncs data between Salesforce objects and WordPress objects.
https://wordpress.org/plugins/object-sync-for-salesforce/
GNU General Public License v2.0
93 stars 48 forks source link

Synchronize/update all accounts and products #515

Open Megabits-es opened 1 year ago

Megabits-es commented 1 year ago

The first thing is that I would like to thank you for the enormous work you are doing, thank you very much.

I've been studying your code and I've developed a few lines of code that allow you to sync or update all accounts and/or products from Salesforce to Wordpress, a Salesforce bulk sync.

Megabits-es commented 1 year ago
function mgb_salesforce_query($mgb_query) {
    $option_token = get_option('object_sync_for_salesforce_access_token');
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => $mgb_query,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'GET',
      CURLOPT_HTTPHEADER => array('Authorization: Bearer '.$option_token),
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    $response = json_decode($response, true);
    return $response;
}
add_options_page( 'Salesforce bulk', 'Salesforce bulk', 'manage_options', 'salesforce_bulk', 'mgb_salesforce_bulk_admin_page' );
function mgb_salesforce_bulk_admin_page() {
    ?>
    <div class="wrap">
    <h1><?php echo esc_html( get_admin_page_title() );?></h1>
    <h3>Carga/Actualiza todos los usuarios o productos de Salesforce.</h3></br>
    <form action="" method="post">
        <input type="submit" name="bulk_users" value="Cargar/Actualizar todos los usuarios">
        <input type="submit" name="bulk_products" value="Cargar/Actualizar todos los productos">
    </form>
    </div>
    <?php   
    if ( ($_POST['bulk_users']) || ($_POST['bulk_products']) ) {
        if ($_POST['bulk_users']) $object_type = 'Account';
        if ($_POST['bulk_products']) $object_type = 'Product2';
        $fields_query = "Id,Name";
                $your_salesforce= "your_salesforce";
        $mgb_query = "https://".$your_salesforce.".my.salesforce.com/services/data/v56.0/query?q=SELECT+".$fields_query."+FROM+".$object_type;
        $response = mgb_salesforce_query($mgb_query);

        header("X-Accel-Buffering: no");
        ob_end_flush();
        foreach ($response['records'] as $key => $value) {
            echo esc_html($value['Name']).' - '.esc_html($value['Id']).'<br/>';
            ob_flush();
            flush();
            sleep(2);
            if ( class_exists( 'Object_Sync_Sf_Salesforce_Pull' ) ) {
            $mgb_pull_from_salesforce = new Object_Sync_Sf_Salesforce_Pull;
            $mgb_pull_from_salesforce->manual_pull($object_type, $value['Id']);
            }
        }
        ob_clean();
    }
}