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
elkiehoundelkiehound 

Update Account Type from Opportunity Layout

Looking for code to be able to "click" on a button in the Opportunity layout that will update the value of the account_type field in the account object.

 

Currently, after an opportunity is closed, the user has to remember to go back to the account tab and update the account type.

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
clark211clark211

You need this trigger.  This isn't based on a button, but will happen after Opportunity = Closed Won, it happens after update.

 

trigger OpportunityWon on Opportunity (after update) {
  List<Id> acctIds = new List<Id>();
  for(Opportunity opp: Trigger.new) {
    if (opp.StageName == 'Closed Won') {
      acctIds.add(opp.AccountId);
    }
  }
  
  if (!acctIds.isEmpty()) {
    RecordType rt = [SELECT r.Id FROM RecordType r WHERE r.Name = 'Customer Record Type' LIMIT 1];
    List<Account> accounts = [SELECT a.Id, a.RecordTypeId FROM Account a
        WHERE a.Id IN :acctIds];
    for (Account a: accounts) {
      a.RecordTypeId = rt.Id;
    }
     
    update accounts; 
  }
}

All Answers

clark211clark211
Do you mean Account Record Type?  For instance, the Account is a "Prospect" before a Closed Won Oppty and once the Oppty = Closed Won, change Account.Record Type = "Customer"?
elkiehoundelkiehound

Let me give you some specifics, perhaps it will help you help me.

 

We are in the insurance business.

 

When a lead is converted, the Account Type is Prospect.

 

If we win the business, the Account Type needs to be changed to Insured.

 

Currently, to change the value, the Underwriter needs to go from the Opportunity Page to the Account Page to update the Account type.

 

We are looking for some method to update the Account Type from the Opportunity Page, I have seen a "link" that does the update in the background, just looking for the code that does it.

 

Thanks

clark211clark211

You need this trigger.  This isn't based on a button, but will happen after Opportunity = Closed Won, it happens after update.

 

trigger OpportunityWon on Opportunity (after update) {
  List<Id> acctIds = new List<Id>();
  for(Opportunity opp: Trigger.new) {
    if (opp.StageName == 'Closed Won') {
      acctIds.add(opp.AccountId);
    }
  }
  
  if (!acctIds.isEmpty()) {
    RecordType rt = [SELECT r.Id FROM RecordType r WHERE r.Name = 'Customer Record Type' LIMIT 1];
    List<Account> accounts = [SELECT a.Id, a.RecordTypeId FROM Account a
        WHERE a.Id IN :acctIds];
    for (Account a: accounts) {
      a.RecordTypeId = rt.Id;
    }
     
    update accounts; 
  }
}

This was selected as the best answer