You need to sign in to do that
Don't have an account?

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.
Hi Brad,
you can try out the following code:
I am considering the Survey_Opp_Id__c is a look up field.
All Answers
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;
Hi Brad,
you can try out the following code:
I am considering the Survey_Opp_Id__c is a look up field.
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
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.
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
Hi Samar,
I have 2 of my custom fields being updated.
Hi Brad,
Try the following code and let me know if it is working or not
Thank you so much Samar. It actually Worked...thank you so much for your Help.
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.