Skip to main content
How can i refer to the account.ownerid from the query for the assignment to take place?:

 

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 resposta
  1. 24 de fev. de 2014, 19:18
    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.
0/9000