
global void execute(Database.BatchableContext BC, List<Opportunity> scope){
List<Opportunity> dealsToUpdate = new List<Opportunity>();
for(Opportunity j : [Select Id, OwnerId, Name, CloseDate,Account.OwnerId, Account.Id,Account.Owner.IsActive,Account.Active__c, Account.Name, Account.GMF_ACF__c from Opportunity where stagename != 'DECLINE,WITHDRW' and Account.recordtypeid = :ActRecTypeID and account.owner.House_Account__c = false and account.owner.isActive = true and Account.LastModifiedDate = Last_Month and Account.LastModifiedById = :wendy and Account.Active__c = 'Y' limit 20000])
{
if(j.OwnerID != account.OwnerID){
Opportunity mOpportunity = new Opportunity();
mOpportunity.Id = j.Id;
mOpportunity.OwnerID = account.OwnerID;
dealsToUpdate.add(mOpportunity);
}
/*Account a = (Account)obj;
Opportunity o;
Account a;*/
//for(Opportunity o: j.opportunities){
/*if(o.OwnerID != j.OwnerID){
Opportunity mOpportunity = new Opportunity();
mOpportunity.Id = o.Id;
mOpportunity.OwnerID = j.OwnerID;
dealsToUpdate.add(mOpportunity);
}*/
//}
}
if(dealsToUpdate.size()>0)
{
update dealsToUpdate;
}
}
답변 1개
Jonathan
Within your for loop:
for (Opportunity j : [select ..... account.ownerId, ....]) {
....
}
the statements:
if (j.ownerId != account.ownerId)
should be
if (j.ownerId != j.account.ownerId)
and
mOpportunity.ownerId = j.account.ownerId;
That is, every Opportunity processed within the for loop is referenced by loop variable j; j. fieldName can reference any field on the Select statement - including lookup fields to parent records such as account.ownerId.