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
Mariappan PerumalMariappan Perumal 

Doubt Scheduled

 

Hi all ,

 

I have written the apex scheduled class and which will fire everyday and it will fetch the records from the custom object of certain criteria and try to update the account to which the record is associated .

 

I just paste my code below is it possible to write like :

 

global class AccountStatusUpdat Implements Schedulable
 { 


     global void execute(SchedulableContext sc)
        {  
       List<Online_Application__c> oa=new List<Online_Application__c>();
       List<Online_Application__c> os=new List<Online_Application__c>();
       oa=[Select id,Account__r.Account_Status__c from Online_Application__c where StatusUpdatedDate__c < Last_N_Days:15  ];
       for(Online_Application__c oo: oa)
       {
       oo.Account__r.Account_Status__c='Disengaged';
       os.add(oo);
       
       }  
     update os;  
  }
  
  }

 

My question is :

I have queried the like

Select id,Account__r.Account_Status__c from Online_Application__c where StatusUpdatedDate__c < Last_N_Days:15 through i get the account status of the application and i try to update the status of the account below

like this

 

 oo.Account__r.Account_Status__c='Disengaged';  Is it right way of doing this ?

oo.Account_Status__c='Disengaged '; is enough .

 

Can anyone help me out of this ..

 

 

Thanks in advance

 

 

 

Avidev9Avidev9

Well you are doing it correctly

 

 oo.Account__r.Account_Status__c='Disengaged';

 

Because Account_Status__c is field of account you have to traverse all the way to account using the relationship field ("Account__c") to access its values

 


Sorry I didnt read the question properly.

  • You can do comparison like this oo.Account__r.Account_Status__c=='Disengaged'
  • You can do comparison in SOQL where statement something like "WHERE Account__r.Account_Status__c='Disengaged'"
  • You cannot do assignment and update the account like this oo.Account__r.Account_Status__c='Disengaged'

To do update

oa=[Select id,Account__r.Account_Status__c from Online_Application__c where StatusUpdatedDate__c < Last_N_Days:15  AND Account__c !=NULL];

List<Account> accList = new List<Account>();
       for(Online_Application__c oo: oa)
       {
       Account acc = new Account(Id=oo.Account__c);
       acc.Account_Status__c='Disengaged';
       accList.add(acc);
       
       }  

update accList;

 

Mariappan PerumalMariappan Perumal

Hi avidev,

 

Nice to hear from you ,

 

Its correct way , thats great to hear ,

 

but i just update the custom object records list i mean os list in my case

 

How come it will update the associated account record for custom object records .

 

List<Online_Application__c> oa=new List<Online_Application__c>();
       List<Online_Application__c> os=new List<Online_Application__c>();
       oa=[Select id,Account__r.Account_Status__c from Online_Application__c where StatusUpdatedDate__c < Last_N_Days:15  ];
       for(Online_Application__c oo: oa)
       {
       oo.Account__r.Account_Status__c='Disengaged';
       os.add(oo);
       
       }  
     update os;  

 

Thanks in advance.

Avidev9Avidev9
Sorry I made a mistake. I have updated the answer have a look
Mariappan PerumalMariappan Perumal

Hi Avidev,

 

I need the same as you specified in the comment and i want to know whether upating account via custom object is possible or not.

 

Anyway thank you so much .

 

 

Avidev9Avidev9
I just gave you an example how to do the update!.
You cannot directly update using the relationship
Mariappan PerumalMariappan Perumal

Hi

 

I need a help on this . I have go through lot of things in the board but couldn't find the solutions and after viewing the solutions for the above question , I can see the light at the head.

 

It will work with less effort and can you take a look on it .

 

http://boards.developerforce.com/t5/Apex-Code-Development/Doubt-Bulkification-of-wonderful-trigger/m-p/664790