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
learncloudlearncloud 

trying to update account object field

Hi,

I am new to sfdc, i would like o update account object field based on opportunity object field values

below is my code what i tried but im getting error. - Error: Compile Error: Variable does not exist: ac at line 20 column 8

trigger Account on Opportunity (after update)
 {

 if (Trigger.isUpdate)
 {

list<Account> acc = new list<Account>();

for(Opportunity opp: trigger.new)
{

Account ac = new Account();
ac.Status__c = opp.StageName;
if(opp.StageName == 'Closed Won')
{
ac.Status__c = 'Customer';
}
}

update ac;

}
}
Ankit Maheshwari 2Ankit Maheshwari 2
Hi,

Could you please try this and let me know if any issue occure?

trigger Account on Opportunity (after update)
 {
list<Account> accLst = new list<Account>();
 if (Trigger.isUpdate)
 {

for(Opportunity opp: trigger.new)
{

    Account ac = new Account();
    ac.Status__c = opp.StageName;
    if(opp.StageName == 'Closed Won')
    {
         ac.Status__c = 'Customer';
        accLst.add(ac);
     }
}
if(accLst!=null&& accLst.size()>0)
update accLst;

}
}
HARSHIL U PARIKHHARSHIL U PARIKH
Hello LearnCloud,

Can you please explain what are you trying to do here and I will see If I can help you out..
It's looks like you have field Status__c on account which needs to be same as opportunity's stage field. If yes, then one account can have many opportunities and which opportunity's stage field should be set as account's Status__c?