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
wcwill978wcwill978 

Updating a field to an old value after an opportunity is closed

I am trying to figure out what will be the best way to accomplish this task can anyone please suggest any good solutions:

 

I have a trigget that runs on opportunities and updates the "Type" field on Accts. When a new opportunity is opened the Type on the account is set to field engaged, if the deal is closed won then the account type goes to active, or if the deal is lost right now it goes to another value.

 

What I want to do is set the Type back to what it used to be before if the deal is closed lost. So if the acct was Inactive and a new deal is opened it automatically goes to Field Engaged, but if the account is closed lost I want it to go back to inactive. If the account was field engaged then go back to field engaged if the deal is lost? The problem is that as soon as the opportunity is opened the account is then set to field engaged, how can i store the old value so in the future if the account is lost i can set it back to that old value?

 

I though of having a hiden field that stores the value so then I can call that value or set the value of the visible field to that when the deal is closed lost?

 

Any ideas???

grigri9grigri9

Is the account type always determined by the opportunities underneath that account? If so, you can just look at all the opportunities underneath that account to determine the correct type the account should be. I.E. if there is still at least one closed/won opportunity set it to active, if there are open opportunities set to engaged, otherwise set to inactive, etc.

 

If that is not the case I would recommend turning on field history for the type field and looking at the accounthistory record.

wcwill978wcwill978

Hi grigri9,

 

Yes the acct type is detremined by the opportunities underneath, but it depends on how many opportunities, lets say the account has only one related oportunity then at that point if its closed lost then set it back to its old value if there are more open opportunities then it should remain engaged until the last closed opportunity.

 

How can I write this in an if statement to say if there are no other open opportunities related to this account then revert to the old value? This is what I have so far:

 

else if(lostAccountIds.contains(acct.Id))
    {
      if (acct.Type == 'Field Engaged' &&  "Here is where I want to add the other rule to say if there are no other open oppty")
      {
        acct.Type = acct.Previous_Type;
      }
    }

 

can I use ... && acct.Opportunity.r == ????

grigri9grigri9

You would have to query the opportunities on that account. To do this efficiently you want to grab all the accountids that are related to opportunities where the stage is changing and then query the opportunities underneath that account to figure out which type you should set the account to. something like the following:

 

set<ID> accIDs = new set<ID>(); //get this from the opportunities where the stage is changing
list<Account> AccsToUpdate = new list<Account>();
for(Account acc : [select ID, Type, (select stagename from Opportunities where stagename!='closed/lost') from Account where ID in :accids]){
	string newtype = 'Inactive';
    for(Opportunity opp : acc.Opportunities)
	{
        if(opp.stagename=='Closed/Won')
        {
		newtype='active';
	    	break;
        }
        else if(opp.stagename != 'closed/lost') newtype = 'Field Engaged';
    }
    if(acc.Type != newtype)
    {
        acc.Type = newtype;
        AccsToUpdate.add(acc);
    }
}


if(AccsToUpdate.IsEmpty() == false){ update AccsToUpdate;}