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
Ashok RoutAshok Rout 

Write a trigger on opportunity insertion to append the name of all the contacts under the same account on the opportunity description.

Can Anyone, help me with this problem,
I am new to Apex.
Best Answer chosen by Ashok Rout
ravi soniravi soni
Hy Ashok,
try following trigger.
trigger setConNameOnOppDesc on Opportunity (before insert,before update ) {
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)){
        Map<String,string> storeAccIdWiseContsName = new Map<String,string>();
        for(opportunity opp : trigger.new){
            if(opp.AccountId != null){
                storeAccIdWiseContsName.put(opp.AccountId,'');
            }
        }
        
        for(Account acc : [SELECT Id,Name,(SELECT Id,Name From Contacts) From Account 
                           Where Id In : storeAccIdWiseContsName.keySet()]){
                               string conNames = '';
                               for(contact con : acc.Contacts){
                                   //lstContactNames.add(con);  
                                   conNames+= con.Name + ',';
                               }
                               for(string accId : storeAccIdWiseContsName.keySet()){
                                   if(accId == acc.Id){
                                       storeAccIdWiseContsName.put(accId,conNames.removeEnd(',')); 
                                   }
                               }
                               
                           }
        
        for(opportunity opp : trigger.new){
                for(string accId : storeAccIdWiseContsName.keySet()){
                                   if(accId == opp.AccountId){
                                       opp.Description = storeAccIdWiseContsName.get(accId);
                                   }
                               }
           
            
        }
        
    }
    
}

let me know if it helps you and marking it as best answer.
Thank you

All Answers

CharuDuttCharuDutt
Hii Ashok Rout
Try Below Trigger
trigger NumChild3 on Opportunity (before insert){
	String s = '';
    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isBefore){
        for(Opportunity con : Trigger.new){
            if(con.AccountId != null){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
  
    for(Account acc :[Select id,(Select id,name from contacts) from Account where Id in : setAccIds]){
      
      
        for(Contact Con :acc.contacts){
            s+=Con.Name +',';
            system.Debug('s===>'+s);
            
        }
       
    }
   
     for(Opportunity con : Trigger.new){
           Con.Description = s.removeEnd(',');
			}

}
Please Mark It As Best Answer If It Helps
Thank You!

 
Suraj Tripathi 47Suraj Tripathi 47

Hi,
Please find the solution.

global class appendContact{
    Public static void data1(List<Opportunity> opList){
         Set<Id> accountSet=new Set<Id>();
         for(Opportunity op:opList){
            accountSet.add(op.AccountId);
        }
                map<Id,List<String>> mapName=new map<Id,List<String>>();

        List<Contact> contactList=new List<Contact>([Select Id,Name,accountId from Contact where accountId in: accountSet]);
        List<String> contactNameList=new List<String>();
         for(Contact con:contactList){
             
                if(!mapName.containsKey(con.AccountId)){
                    List<String> conList=new List<String>();
                    conList.add(con.Name);
                    mapName.put(con.AccountId,conList);
                }else{
                    List<String> conList=mapName.get(con.AccountId);
                    conList.add(con.Name);
                    mapName.put(con.AccountId,conList);
                }
                
            }
        system.debug('mapName::'+mapName);
       List<Opportunity> oppUpdate=new List<Opportunity>();
        for(Opportunity op:opList){
            Opportunity opp=new Opportunity();
            opp.Id=op.Id;
            opp.Description=String.valueOf(mapName.get(op.AccountId));
            oppUpdate.add(opp);
        }
        update oppUpdate;
    }

Trigger:

trigger OpportunityTrigger on Opportunity (after insert) {
  
    
    if(trigger.isafter && Trigger.isinsert){
           
         appendContact.data1(trigger.new);
        
    }
}
Please mark it as the Best Answer if your queries are solved.
Thank You
Ashok RoutAshok Rout
It would be really helpful if you could maps to bulkify
ravi soniravi soni
Hy Ashok,
try following trigger.
trigger setConNameOnOppDesc on Opportunity (before insert,before update ) {
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)){
        Map<String,string> storeAccIdWiseContsName = new Map<String,string>();
        for(opportunity opp : trigger.new){
            if(opp.AccountId != null){
                storeAccIdWiseContsName.put(opp.AccountId,'');
            }
        }
        
        for(Account acc : [SELECT Id,Name,(SELECT Id,Name From Contacts) From Account 
                           Where Id In : storeAccIdWiseContsName.keySet()]){
                               string conNames = '';
                               for(contact con : acc.Contacts){
                                   //lstContactNames.add(con);  
                                   conNames+= con.Name + ',';
                               }
                               for(string accId : storeAccIdWiseContsName.keySet()){
                                   if(accId == acc.Id){
                                       storeAccIdWiseContsName.put(accId,conNames.removeEnd(',')); 
                                   }
                               }
                               
                           }
        
        for(opportunity opp : trigger.new){
                for(string accId : storeAccIdWiseContsName.keySet()){
                                   if(accId == opp.AccountId){
                                       opp.Description = storeAccIdWiseContsName.get(accId);
                                   }
                               }
           
            
        }
        
    }
    
}

let me know if it helps you and marking it as best answer.
Thank you
This was selected as the best answer