2

My request is to update the account field based on opportunity both has same fields name i.e segment1 which is multi select pick list values an account has multiple opp right. when i update the opp1 for segment1 its effetcing on account but when i update the opp2 its got refreshing and not adding the new one. i has to add both opp1 and opp2 on account and also if the same value is updated it should not be repeated.

I have tried. with my code if any ideas plz suggest me

 trigger Opptyclosedwon on Opportunity (after update) {
  list<account> aa= new list<account>();
  for(opportunity c:trigger.new)
  {
    if(c.stagename=='closed won')
    {
      account a=[select marketsegment1__c,marketsegment2__c,marketsegment3__c from    account where id=:c.accountid limit 1];
      a.marketsegment1__c=c.marketsegment1__c;
      a.marketsegment2__c=c.marketsegment2__c;
      a.marketsegment3__c=c.marketsegment3__c;
      aa.add(a);
    }
  }
  update aa;
 }
  • Just to clarify: You are saying that an account may have multiple opportunities and the fields on the account should be an aggregate of the values across all the closed won opps? – Eric Apr 21 '14 at 14:55
1

Given that there are possible many Opportunity objects related to each Account, you have the problem that the market segment values from the last Opportunity updated will be set on the Account. Presumably that is what you want.

I am not sure why your code is not updating Account always as it looks like it should. But your code does suffer from a potential governor limit issue of having a SOQL query inside a loop. In this case that query is not actually needed as you are overriding the values queried. For that case you can create an object of the right ID without querying and avoid the governor limit issue.

Your update is being done whatever value changes on Opportunity; you may prefer only do the update when the stagename moves to the right value.

Code that addresses these issues would look something like this:

trigger Opptyclosedwon on Opportunity (after update) {
    Map<Id, Account> accounts = new Map<Id, Account>;
    for (Opportunity opp : Trigger.new) {
        if (opp.stagename == 'closed won') {
            Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
            if (oldOpp.stagename != 'closed won') {
                Account a = accounts.get(opp.AccountId);
                if (a == null) {
                    a = new Account(Id = opp.AccountId);
                    accounts.put(opp.AccountId, a)
                }
                a.marketsegment1__c = opp.marketsegment1__c;
                a.marketsegment2__c = opp.marketsegment2__c;
                a.marketsegment3__c = opp.marketsegment3__c;
            }
        }
    }
    if (!accounts.isEmpty()) {
        update accounts.value():
    }
}
0

You will have to rebuild it each time it is updated. This should do it. May need editing as I quickly threw it together. This will rebuild the strings on the account based on the values of all related won opp fields

trigger Opptyclosedwon on Opportunity (after update) {
  Set<ID> accIDs = New Set<ID>();
  Map<ID,Account> accStrings = New Map<ID,Account>();

  for(opportunity c:trigger.new)
  {
    if(c.stagename=='closed won')
    {
      //Get list of account ids in the trigger
      accIDs.add(c.AccountID);
    }
  }

  for(Opporutnity o : [Select marketsegment1__c, marketsegment2__c, marketsegment3__c, AccountID From Opportunity Where AccountID IN :accIDs and isWon = true]){
    Account a = accStrings.get(o.AccountID);

    if(a != null){
      //Update the segment based on the current opportunity and the potential strings already present from other opps in the loop  
      if(a.marketsegment1__c == null){
        a.marketsegment1__c = o.marketsegment1__c;
      }else{
        a.marketsegment1__c = a.marketsegment1__c.contains(o.marketsegment1__c) == true ? a.marketsegment1__c : (a.marketsegment1__c + ';' + o.marketsegment1__c);
      }

      if(a.marketsegment2__c == null){
        a.marketsegment2__c = o.marketsegment2__c;
      }else{
        a.marketsegment2__c = a.marketsegment2__c.contains(o.marketsegment2__c) == true ? a.marketsegment2__c : (a.marketsegment2__c + ';' + o.marketsegment2__c);
      }

      if(a.marketsegment3__c == null){
        a.marketsegment3__c = o.marketsegment3__c;
      }else{
        a.marketsegment3__c = a.marketsegment3__c.contains(o.marketsegment3__c) == true ? a.marketsegment3__c : (a.marketsegment3__c + ';' + o.marketsegment3__c);
      }
    }else{
      a = new Account(id=o.accountid, marketsegment1__c = o.marketsegment1__c, marketsegment2__c = o.marketsegment2__c, marketsegment3__c = o.marketsegment1__c);
      accStrings.put(a.id,a);
    }
  }

  if(!accStrings.isEmpty())
    update accStrings.values();

 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.