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 

Help with Logic to Update Account Custom Feild

Hi,

 

I need to Update my custom fields in Account Object on Reports.

 

I have the query retrieving my data. After i check the condition i need to update my fields.

for one of the custom field it should be updated with the Opportunity ID. 

How should i retrieve it from the query and update the field.

 

And also it should Update as a 15 character ID. 

 

 

 

global class HF_Survey_OneMonth Implements Schedulable 
{
    
    global void execute(SchedulableContext sc)
    {
        sendSurvey();
    }
    
    
    
    public void SendSurvey()
    {
        List<Account> accToUpdate = [Select Survey_Opp_Id__c, Survey_Dt__c, Survey_Code__c, Name, Id
                                           From Account 
                                           where Id IN (Select AccountId 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)];                               
                    
          for(Account a : accToUpdate)
          {
             if(a.Survey_Code__c != 'HF1MoArv')
              {
                a.Survey_Code__c = 'HF1MoArv';
                a.Survey_Dt__c = Date.today().addDays(1);
              }
          }
                
    }
}   

 

 

Please Help.

Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
samarsamar

Hi Brad,

you can try out the following code:

 

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>();
    Set<Id> dupCheck = Set<Id>();
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= opp.Id;
if(dupCheck.contains(opp.Id)){
 accList.add(acc); dupCheck.add(opp.Id);
}
  } } update accList; }

 I am considering the Survey_Opp_Id__c is a look up field.

 

 

All Answers

Pradeep_NavatarPradeep_Navatar

You need to use DML statement to update your account. First access the record which  you need to update and made changes to record and update using DML statement.

 

for(Account a : accToUpdate)

          {

             if(a.Survey_Code__c != 'HF1MoArv')

              {

                a.Survey_Code__c = 'HF1MoArv';

                a.Survey_Dt__c = Date.today().addDays(1);

              }

          }

       Update accToUpdate;

samarsamar

Hi Brad,

you can try out the following code:

 

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>();
    Set<Id> dupCheck = Set<Id>();
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= opp.Id;
if(dupCheck.contains(opp.Id)){
 accList.add(acc); dupCheck.add(opp.Id);
}
  } } update accList; }

 I am considering the Survey_Opp_Id__c is a look up field.

 

 

This was selected as the best answer
Brad007Brad007

Hi Samar,

 

Thank you so much for your help. I really appreciate for the time you have taken to response.

I am so very much new to Apex programming. i have changed my query accordingly.

But here Survey_Opp_Id__c custom field in Account object is not the look Up field.

 

This field in Opportunity is the Look up field related with Account.

Field Label Field Name Data Type Controlling Field Track HistoryAccount Name

EditAccountLookup(Account)

 

The survey class should update my same  custom fields in 2 cases

Case1:

 

      if(acc.Survey_Code__C != 'HF1MoArv')

{

  custom field(Survey_Code__c )='HF1MoArv';

  custom field(Survey_Dt__c) = Present date+ 1 Day;

  custom field(Survey_Opp_Id__c) = Opportunity ID(Trimmed as 15 character);

}

       if(custom field(Survey_Opp_Id__c) != Opportunity ID)

{

  custom field(Survey_Code__c )='HF1MoArv';

  custom field(Survey_Dt__c) = Present date+ 1 Day;

  custom field(Survey_Opp_Id__c) = Opportunity ID(Trimmed as 15 character);

}

 

Thank you,

Brad.

Brad007Brad007

Hi Pradeep,

 

Thank you for you response and time taken.

 

i added the DML statement and still it seems not to update .Can you please suggest from my reply in the forum.

 

Thank you,

Brad

Brad007Brad007

Hi Samar,

 

I have 2 of my custom fields being updated.  

 

  acc.Survey_Code__c 
acc.Survey_Dt__c
but i am still unable to update my Survey_Opp_Id__c custom field from Account. Can you Help me with that please.
Thank you,
Brad.

 

samarsamar

Hi Brad,

Try the following code and let me know if it is working or not

 

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>();
Set<Id> dupCheck = Set<Id>();
for(Opportunity opp:oppList){
if(opp.AccountId!= null){
Account acc = opp.Account;
String oppId = (opp.Id).substring(3,18);
if(acc.Survey_Code__c != 'HF1MoArv' || acc.Survey_Opp_Id__c != oppId)
{
acc.Survey_Code__c = 'HF1MoArv';
acc.Survey_Dt__c = Date.today().addDays(1);
acc.Survey_Opp_Id = oppId;
if(dupCheck.contains(opp.Id)){
     accList.add(acc);
        dupCheck.add(opp.Id);
}

}
}
}
update accList;
}
Brad007Brad007

Thank you so much Samar. It actually Worked...thank you so much for your Help.

 

Brad007Brad007

Hi,

 

Can you please help build a test class for the above scheduler class. I tried to follow the syntax from the apex guide, but i was unsuccessful with the approach ..Please help

 

Thank you.