Search code examples
apexsoql

SOQL help: How to get the correct contactID in apex SOQL, I want assign Contact ID into field whoid in the Task creation


This SOQL give me the Account.Maintenance_Contact__r.id(003C000002M6kiDIAR)

Select ID, owner.id, owner.isactive, 
                                 Master_Opportunity__c, Internal_Start_cycle_days_ROPP__c,Invoice_cycle_days_ROPP__c,
                                        **Account.Maintenance_Contact__r.id** 
                                 from Opportunity 
                                 where Master_Opportunity__c = null 
                                 and Account_Record_Type__c != 'Personal Use Accounts'
                                 and Renewal_Stage__c in ('Pending PO','Pending SLSA','Pending OEM')      
                                 and Invoice_cycle_days_ROPP__c = NEXT_MONTH ]

but when i added into Apex it give me this Account.Maintenance_Contact__r.id is "001C0000013yu9QIAQ" and not the contact ID.

public class Oppjob_Task_PORequired_cls {
    public void createTaskforOpp() {
          List<Opportunity> oppList = [Select ID, owner.id, owner.isactive, 
                                 Master_Opportunity__c, Internal_Start_cycle_days_ROPP__c,Invoice_cycle_days_ROPP__c,
                                        Account.Maintenance_Contact__r.id 
                                 from Opportunity 
                                 where Master_Opportunity__c = null 
                                 and Account_Record_Type__c != 'Personal Use Accounts'
                                 and Renewal_Stage__c in ('Pending PO','Pending SLSA','Pending OEM')      
                                 and Invoice_cycle_days_ROPP__c = NEXT_MONTH 
                                 and id ='006C000001CSbQIIA1'];
    List<Task> taskList = new List<Task>();
        system.debug('oppList' + opplist);

     For(Opportunity opp : oppList) {
      if(opp.Owner.isActive == true) {
                   Task newTask = new Task(
                         WhatId = opp.Id,
                        OwnerId = opp.OwnerId,
                        Team__c = 'PreSales Support',
                    // Task_Type__c = 'RMT Follow Up',
                          WhoID = opp.Account.Maintenance_Contact__r.id,
                        //WhoID = '003C000002M6kiDIAR',
                   ActivityDate = system.today().adddays(3) ,
                        Subject = 'PO Required Follow-up',
                    Description = 'Follow up for customer PO');
         taskList.add(newTask);
         }
        }
     system.debug('TaskList:' + tasklist.size());
    if (tasklist.size()>0){
        insert taskList;
    }

    }
}

Solution

  • I got the answer to my question.

    **List<Opportunity> oppList = [Select ID, owner.id, owner.isactive, 
                                     Master_Opportunity__c, Account.id, Internal_Start_cycle_days_ROPP__c, Invoice_cycle_days_ROPP__c
                                           ,**Account.Maintenance_Contact__c**
                                     from Opportunity 
                                     where Master_Opportunity__c = null 
                                     and Account_Record_Type__c != 'Personal Use Accounts'
                                     and Renewal_Stage__c in ('Pending PO','Pending SLSA','Pending OEM')      
                                     and Invoice_cycle_days_ROPP__c = NEXT_MONTH 
                                     and id ='006C000001CC3AYIA1'];
    
        List<Task> taskList = new List<Task>();
         For(Opportunity opp : oppList) {
          if(opp.Owner.isActive == true) {
                       Task newTask = new Task(
                            WhatId = opp.Id,
                            OwnerId = opp.OwnerId,
                            Team__c = 'Renewal Management',
                       Task_Type__c = 'RMT Follow Up',
                              WhoID = **opp.Account.Maintenance_Contact__c,**
                       ActivityDate = system.today().adddays(5) ,
                            Subject = 'PO Required Follow-up',
                        Description = 'Follow up for customer PO');
             taskList.add(newTask);
             }
            }**