function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dan HardyDan Hardy 

2 way sync

Hi-
I'd like to develop a 2 way sync with Asana and need to think through how to set it up.  Does anyone have some tips or a guide on how to set up a 2 way sync?  I know there are lots of integration services out there but I'd like to build it from scratch.

For example, when I create a task in Salesforce I'd like to be able to create a corresponding task in Asana and vice versa.  How would I check that I'm not replacing an newer version of the task with an older one?  Would I check the modified date of each?  Stuff like that.

Thanks!
Darley Stephen PMDarley Stephen PM
Dan,
   These are the holistic steps you need to follow

1. Develop a code that is used as a connected app to read / write data in Salesforce, need to do OAuth authentication and authorization (https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com     - use web server flow if possible) 

2. Develop a code that does OAuth and Restful for Asana (https://asana.com/developers/documentation/getting-started/auth) 

Basically, you are creating 2 functions in java / php or others to retrieve Access token from both Salesforce and Asana for access both the environments

3. Retrieve the Tasks from salesforce using SOQL, and insert (POST) the task into Asana by passing the Task details along with the salesforce ID assigned to the  'external' parameter in asana task. 

4. For any updates use the same 'external' {"external":"salesforce_task_id"}  ID, which helps you cron the program for every 10 minutes and you will see only new tasks in Asana or the old tasks getting updated. 
 
sample PHP code for retrieving salesforce tasks. 
                
                $version='38.0'
                $query = "Select Id from Task";
                $qURL = $token_data->instance_url . '/services/data/v'.$version.'/query'.'?q='.urlencode($query);
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$qURL);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $headers = array();
                $headers[] = 'Authorization: OAuth ' .$token_data->access_token ;
                $headers[] = 'Content-Type: application/json';
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                $tskIDs = curl_exec ($ch);
                $tskIDs = json_decode($tskIDs);


 
Dan HardyDan Hardy
Hi Darley- That's incredibly helpful. Thanks! It sounds like I might need a web server to make requests to SF and Asana then. Could I use javascript and node.js ? Dan
Darley Stephen PMDarley Stephen PM
Dan,
     If you want to do this  via node.js you don't need to install a webserver.

you just need to create a serversocket function in node.js and run that on a port for Salesforce and Asana to send the access tokens.
 
var server = connect()
    .use(function (req, res, next) {
      var query;
      var url_parts = url.parse(req.url, true);
      query = url_parts.query;

      if (req.method == 'GET') {
        switch (url_parts.pathname) {
            case '/sf_auth_client_redirection'
                  /*REDIRECT client to do Salesforce Authentication to get Code*/
            case '/sf_oauthstep1':
                 /*GET Code */           
                 res.end();
                 http.request (/*Request Access Token*/ /*Use the Code received*/)
            break;
            case '/sf_oauthstep2':
                 /*GET Access Token*/   /*Save the Access token and Refresh token as well*/        
                 res.end();
            break;


            case '/asana_auth_client_redirection'
                  /*REDIRECT client to do Asana Authentication to get Code*/
            case '/asana_oauthstep1':
                 /*GET Code */           
                 res.end();
                 http.request (/*Request Access Token*/ /*Use the Code received*/)
            break;
            case '/asana_oauthstep2':
                 /*GET Access Token*/   /*Save the Access token and Refresh token as well*/        
                 res.end();
            break;
          }
        }
    })
    .listen(8080);

User-added image

Once the access tokens are retrieved you have to create a separate function that uses both the access tokens to get the task details from both environments and sync em!, you can also cron this function to auto sync.