def retrieveSfdcOppStageHistory(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): #needs to be changed - satya try: #delete later #job_id_new = job_id #job_id = ObjectId("56a690e98afb006883048e7e") #set variables sfdc = Salesforce() #for leads if run_type == 'initial': opps = TempData.objects(Q(record_type='opportunity') & Q(source_system='sfdc') & Q(job_id=job_id)) #Q(job_id=job_id) & else: opps = TempDataDelta.objects(Q(record_type='opportunity') & Q(source_system='sfdc') & Q(job_id=job_id)) #Q(job_id=job_id) & oppListTemp = list(opps) if not oppListTemp: print 'no opps found' return oppList = [i['source_record'] for i in oppListTemp] batch_size = 500 #10 Activity Types at a time activitiesList = [] for i in range(0, len(oppList), batch_size): opp_list = '(' for opp in oppList[i:i+batch_size]: opp_list += '\'' + opp['Id'] + '\'' + ', ' opp_list = opp_list[:-2] opp_list += ')' activitiesList.extend(sfdc.get_stage_history_for_opportunity(user_id, company_id, opp_list, _str_from_date(sinceDateTime))) print 'got back stage history for SFDC opportunities ' + str(len(activitiesList)) #delete later #job_id = job_id_new saveSfdcOppStageHistory(user_id=user_id, company_id=company_id, activityList=activitiesList, job_id=job_id, run_type=run_type) try: message = 'Opportunity stage history retrieved from Salesforce' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Opportunities' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) return opp_list except Exception as e: print 'exception while retrieving SFDC opportunity stage history: ' + str(e) send_notification(dict(type='error', success=False, message=str(e)))