+ Start a Discussion
Deepu sfdcDeepu sfdc 

Creating a Visualforce page on opportunities

Hello Guys,

When i change opportunity status to “closed one” for selected opportunities, On click of save it will update opportunity status as well as Account status as well.
Please tell me how to achieve this.

Thanks in Advance,
Deepu
 
Best Answer chosen by Deepu sfdc
Kirti Deshpande 13Kirti Deshpande 13
Hi Deepu,

After updating Opportunity status on the visualforce page controller, use the successfully updated opportunity Id list say selectedOppList.
Now query AccountIds using selectOppList as below -

Set<Id> AccountIds = new Set<Id>();
List<Account> accountsToBeUpdated = new List<Account>();
for(Opportunity opp : selectOppList ){
       AccountIds.add(opp.AccountId);
}
if(AccountIds.Size() > 0){
         accountsToBeUpdated = [Select Id. Status from Account] where Id IN: AccountIds];
}
for(Account acc : accountsToBeUpdated ){
        acc.Status = 'your status';
}
if(accountsToBeUpdated.Size() > 0){
     update accountsToBeUpdated;
}

Another way is to update opportunity inside page controller and update its account in the trigger on update of Opportunity.

Hope this helps.
 

All Answers

GauravendraGauravendra
Hi Deepu,

From the Visualforce page for opportunity, update the opportunity.
And create a trigger on opportunity object that will check for opportunity status update, if status is "closed one" then update the account related to that opportunity.

You can get the related account id of opportunity like:
Select Id,Name,AccountID From Opportunity where Id ='OppId'
Get the account from this AccountID and update the status of account.

Hope this helps.
 
Kirti Deshpande 13Kirti Deshpande 13
Hi Deepu,

After updating Opportunity status on the visualforce page controller, use the successfully updated opportunity Id list say selectedOppList.
Now query AccountIds using selectOppList as below -

Set<Id> AccountIds = new Set<Id>();
List<Account> accountsToBeUpdated = new List<Account>();
for(Opportunity opp : selectOppList ){
       AccountIds.add(opp.AccountId);
}
if(AccountIds.Size() > 0){
         accountsToBeUpdated = [Select Id. Status from Account] where Id IN: AccountIds];
}
for(Account acc : accountsToBeUpdated ){
        acc.Status = 'your status';
}
if(accountsToBeUpdated.Size() > 0){
     update accountsToBeUpdated;
}

Another way is to update opportunity inside page controller and update its account in the trigger on update of Opportunity.

Hope this helps.
 
This was selected as the best answer