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
sai krishna 267sai krishna 267 

I have a question...I have account record in that record there is a three opportunities r there. in Account field there is a one custom field its name is all_oppnames__c in that field that all opportunities names will be displayed comma(,) symbol.by

I have a question...I have account record  in that record there is a three opportunities r there. in Account field there is a one custom field its name is all_oppnames__c in that field that all opportunities names will be displayed comma(,) symbol.by using triggers can any bodygive the code for that......
srlawr uksrlawr uk
it's probably slightly more complicated than this, but here is a freehand (thus: syntax error riddled - this will almost certainly not save)

 
Trigger saiTrigger on Opportunity (after insert, after update, after delete, after undelete) {
    Set<Id> parentIds = new Set<Id>();  

   // get all the accounts then
    for (Opportunity thisOpp : Trigger.new) {
        parentIds.add(thisOpp.Account);
    }

   // load all the opps for those accounts
   List<Opportunity> allOpps = [SELECT Id, Name FROM Account WHERE Account = :parentIds];

    Map<Id, Account> updatedAccounts = new Map<Id, Account>();

    // go through those accounts and blank the field and associate them with their id
   for(Id thisId : parentIds) {
      updatedAccounts.put(thisId, new Account(Id = thisId, all_oppnames__c = '');
   }
 
   // I'm gunna use an extra list here as I'm too tipsy to think about scope in maps etc...
   List<Account> resultAccounts = new List<Account>();


    // now concat all the opp names back into their account
    for(Opportunity thisOpp : allOpps) {
       Account itsAccount = updatedAccounts.get(thisOpp.Account);
       itsAccount.all_oppnames__c =  itsAccount.all_oppnames__c + thisOpp.name;
        resultAccounts.add(itsAccount);
    }
   
   update resultAccounts;

}


I'm pretty proud of that to be fair considering you have literally just asked a forum to do you work for you, I hope you can beat it into a good working shape!
 
srlawr uksrlawr uk
oh, balls. I forgot the most important bit... comma seperating..

change line 26 to
 
itsAccount.all_oppnames__c =  itsAccount.all_oppnames__c + ', ' + thisOpp.name;

and then you'll have to cleverly strip the first leading coma off th result (there's a String method for that)
sai krishna 267sai krishna 267
hi sriak uk but little error in 16 line ---->

 updatedAccounts.put(thisId, new Account(Id = thisId , all_oppnames__c = ' ' );
   

Error: Compile Error: Missing ')' at ';' at line 16 column 84
srlawr uksrlawr uk
oh yeah, obviously! I typed that code straight into the forum box, like I said there is no way it will work straight out the box, you will have to debug that message yourself!!

The clue is in:
 
Missing ')' at ';' at line 16 column 84

Trust me, it's right there!
sai krishna 267sai krishna 267
ok @sriak thank u
sai krishna 267sai krishna 267
@sriak this code is not working how can u link up opportunity to Account how it will add its not working? can u check it once again?
 
sai krishna 267sai krishna 267
Error: Compile Error: Method does not exist or incorrect signature: void add(Account) from the type Set<Id> at line 6 column 19 
this error can u clear me
sai krishna 267sai krishna 267
Error: Compile Error: Method does not exist or incorrect signature: void add(Account) from the type Set<Id> at line 6 column 19 
this error can u clear me @sriawk
srlawr uksrlawr uk
ahh. an obvious typo, sorry!

I have neglected to add the "id" field to the parameter in the Set add method.

The set is defined as being a set of Id values... on line 2, but then on line 6 I am trying to add an Account object. This needs adjusting so line 6 is actually adding the Id of the Account to the instead! Simple enough :)



I should warn you, as per the cavaet in my original answer, I just hand typed that into the comment box, so it may be riddled with little complier errors, which you may have to debug.

 
sai krishna 267sai krishna 267
Error: Compile Error:
SELECT Id, Name FROM Account WHERE Account =:parentIds
^
ERROR at Row:1:Column:36
No such column 'Account' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 10 column 32


Trigger saiTrigger on Opportunity (after insert, after update, after delete, after undelete) {
    Set<Id> parentIds = new Set<Id>();  

   // get all the accounts then
    for (Opportunity thisOpp : Trigger.new) {
        parentIds.add(thisOpp.AccountId);
    }

   // load all the opps for those accounts
   List<Opportunity> allOpps = [SELECT Id, Name FROM Account WHERE Account =:parentIds];

    Map<Id, Account> updatedAccounts = new Map<Id, Account>();

    // go through those accounts and blank the field and associate them with their id
   for(Id thisId : parentIds) {
      updatedAccounts.put(thisId, new Account(Id = thisId, all_oppnames__c = ''));
   }
 
   // I'm gunna use an extra list here as I'm too tipsy to think about scope in maps etc...
   List<Account> resultAccounts = new List<Account>();


    // now concat all the opp names back into their account
    for(Opportunity thisOpp : allOpps) {
       Account itsAccount = updatedAccounts.get(thisOpp.Account);
       itsAccount.all_oppnames__c =  itsAccount.all_oppnames__c + ', ' + thisOpp.name;
        resultAccounts.add(itsAccount);
    }
   
   update resultAccounts;

}



this error clear please if u dont mine@sriawk uk