function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brad007Brad007 

Update Account object from Opportunity(With Lookup Field Account)

Hi,

 

I need to develop a Scheduler class to generate a report. And my class should update the custom fields in Account Object relating to Opportunity (with look up field as Account in it). 

 

Here is the code.

 

 

global class HF_Survey_OneMonth Implements Schedulable 
{
    
    global void execute(SchedulableContext sc)
    {
        sendSurvey();
    }
      
    
    public void SendSurvey()
    {
         List<Opportunity> oppList = [Select o.Id, o.Account.Id, o.AccountId, o.Account.Survey_Opp_Id__c, o.Account.Survey_Dt__c, o.Account.Survey_Code__c, o.Account.Name 
                                      From Opportunity o 
                                      where o.StageName='Active' 
                                      AND o.Placmnt_Start_Date_Arrive__c < Last_N_days :30 
                                      AND o.Placmnt_Start_Date_Arrive__c = Last_N_days :60];                               
                    
             List<Account> accList = new List<Account>();
             
                   for(Opportunity opp:oppList)
                     {
                        if(opp.AccountId!= null)
                        {
                          Account acc = opp.Account;
                          if(acc.Survey_Code__c != 'HF1MoArv')
                           {
                             acc.Survey_Code__c = 'HF1MoArv';
                             acc.Survey_Dt__c = Date.today().addDays(1);
                             //acc.Survey_Opp_Id__c = acc.Id;
                           }                  
                        
                         else if(acc.Survey_Opp_Id__c != acc.Id)
                          {
                             acc.Survey_Code__c = 'HF1MoArv';
                             acc.Survey_Dt__c = Date.today().addDays(1);
                             //acc.Survey_Opp_Id__c = acc.Id;
                          }
                         accList.add(acc); 
                        }
                     }
                update accList;          
              
     }
  }
In here i am unable to check condition /Update Survey_Opp_Id__c field which is in Account . AccountName(Field Name:Account) is a Lookup field in Opportunity .  Here the Survey_Opp_Id__c should check for condition if it is equal to Opportunity ID if it  is not it should update the  Survey_Opp_Id__c with the Opportunity ID.
Please Help!
Thank you,
Brad.

 

 

dmchengdmcheng

What do you mean that you are "unable to check condition /Update Survey_Opp_Id__c field which is in Account"?  You can include it in the select statement along with the other account fields you are retrieving:

 

select Id, Account.Name, Account.Update Survey_Opp_Id__c from Opportunity where blah blah blah ...

Pradeep_NavatarPradeep_Navatar

Try using the sample code given below :

 

global class HF_Survey_OneMonth Implements Schedulable

{

 

    global void execute(SchedulableContext sc)

    {

        sendSurvey();

    }

 

 

    public void SendSurvey()

    {

         List<Opportunity> oppList = [Select Id,AccountId, Account.Survey_Opp_Id__c, Account.Survey_Dt__c, Account.Survey_Code__c, Account.Name

                                      From Opportunity 

                                      where StageName='Active'

                                      AND Placmnt_Start_Date_Arrive__c < Last_N_days :30

                                      AND Placmnt_Start_Date_Arrive__c = Last_N_days :60];                              

 

 

 

                   for(Opportunity opp:oppList)

                     {

                        if(opp.AccountId!= null)

                        {

 

                          if(opp.Account.Survey_Code__c != 'HF1MoArv')

                           {

                             opp.Account.Survey_Code__c = 'HF1MoArv';

                             opp.Account.Survey_Dt__c = Date.today().addDays(1);

                             opp.Account.Survey_Opp_Id__c = opp.Id;

                           }                 

 

                         else if(opp.Account.Survey_Opp_Id__c != opp.Id)

                          {

                             opp.Account.Survey_Code__c = 'HF1MoArv';

                             opp.Account.Survey_Dt__c = Date.today().addDays(1);

                             opp.Account.Survey_Opp_Id__c = opp.Id;

                          }

 

                        }

                     }

                update oppList;         

 

     }

  }

Brad007Brad007

Hi,

 

Can you please help build a test class for this scheduler. I have seen the syntax in Apex guide but i am quiet confused with the approach.

 

Thanks in advance.