global class PowerBIUpdateBatch implements Database.Batchable<sObject> {

	String query;
	Id targetId;

	global PowerBIUpdateBatch(Id targetid) {
		this.targetId = targetid;
	}

	global Database.QueryLocator start(Database.BatchableContext BC) {
		query  = 'select Id, ShareWithPowerBI__c, ShareWithPowerBIFormula__c, ShareWithPowerBI_NeedUpd__c';
		query += '  from BI_OpportunityLineItem__c';
		query += ' where ShareWithPowerBI_NeedUpd__c = true';
		if (targetId != null) {
			query += ' and OpportunityId__c = \'' + targetId + '\'';
		}
		return Database.getQueryLocator(query);
	}

   	global void execute(Database.BatchableContext BC, List<sObject> scope) {
   		List<BI_OpportunityLineItem__c> updateList = new List<BI_OpportunityLineItem__c>();
   		for (SObject obj: scope) {
   			BI_OpportunityLineItem__c bi_oli = (BI_OpportunityLineItem__c) obj;
   			bi_oli.ShareWithPowerBI__c = bi_oli.ShareWithPowerBIFormula__c;
   			updateList.add(bi_oli);
   		}

   		if (updateList.size() > 0) update updateList;
	}

	global void finish(Database.BatchableContext BC) {

	}

}