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
Rajat MahajanRajat Mahajan 

Reg: Have a small code, but need to bulkify it

Hi All,

 

I would really appreciate it if someone can please help me bulkify the following code:

 

/*Trigger name : InsertOppTeam
*
* Author : Rajat Mahajan
*
* Description : This is to insert Opportunity Team while inserting or updating Opportunitys.
*/

trigger InsertOppTeam on Opportunity (after insert, after update) {

Set<Id> idOpp = new Set<Id>();
Set<Id> OppEndCustomerId = new Set<Id>();
List<OpportunityTeamMember> lstOppTeam = new List<OpportunityTeamMember>();

//Declare Variables to query Account Directory and then the Account Owner from User Object
User userRec = new User();
Account_Info__c accountRec = new Account_Info__c();

System.debug('Testing out Trigger.New Content Debug 1 : ' + Trigger.New);

for(Opportunity newOpp : trigger.new) {
if(newOpp.End_Customer__c != null) {
idOpp.add(newOpp.Id);
OppEndCustomerId.add(newOpp.End_Customer__c);
accountRec = [select id, Account_Manager__c from Account_Info__c where id in:OppEndCustomerId limit 1];
userRec = [select Id from User where isActive = true and name = :accountRec.Account_Manager__c];

OpportunityTeamMember tm = new OpportunityTeamMember();
tm.UserId = userRec.Id;
tm.OpportunityId = newOpp .Id;
insert tm;
}
}

}

 

It would be great if you can help me out please.

 

Regards

Rajat

Best Answer chosen by Admin (Salesforce Developers) 
JBabuJBabu

Hey Rajat try the below code:

 

trigger InsertOppTeam on Opportunity (after insert, after update) {

Set<Id> idOpp = new Set<Id>();
Set<Id> OppEndCustomerId = new Set<Id>();
List<OpportunityTeamMember> lstOppTeam = new List<OpportunityTeamMember>();
Map<Id, String> actMap = new Map<Id, String>();
Map<String, Id> usrMap = new Map<String,Id>();
private string accountMgr;
private Id usrId;
private List<OpportunityTeamMember> OpportunityTeamMemberList = new List<OpportunityTeamMember>();

//Declare Variables to query Account Directory and then the Account Owner from User Object
User userRec = new User();
Account_Info__c accountRec = new Account_Info__c();

for(Account_Info__c acinfo :[select id, End_Customer__c, Account_Manager__c from Account_Info__c]) {
    actMap.put(acinfo.End_Customer__c, acinfo.Account_Manager__c);
}

for(User usr : [select Id, Name from User where isActive = true and name in :actMap.values()]) {
    usrMap.put(usr.name,usr.Id);

}

for(Opportunity newOpp : trigger.new) {
  accountMgr = actMap.get(End_Customer__c);
  usrId = usrMap.get(accountMgr);
 
  OpportunityTeamMemberList.add(new OpportunityTeamMeber(UserId = usrId, OpportunityId = newOpp.Id));

}


if(OpportunityTeamMemberList.size() > 0){
 insert OpportunityTeamMemberList;
}

}

 

Thanks,

JBabu.

All Answers

JBabuJBabu

Hey Rajat try the below code:

 

trigger InsertOppTeam on Opportunity (after insert, after update) {

Set<Id> idOpp = new Set<Id>();
Set<Id> OppEndCustomerId = new Set<Id>();
List<OpportunityTeamMember> lstOppTeam = new List<OpportunityTeamMember>();
Map<Id, String> actMap = new Map<Id, String>();
Map<String, Id> usrMap = new Map<String,Id>();
private string accountMgr;
private Id usrId;
private List<OpportunityTeamMember> OpportunityTeamMemberList = new List<OpportunityTeamMember>();

//Declare Variables to query Account Directory and then the Account Owner from User Object
User userRec = new User();
Account_Info__c accountRec = new Account_Info__c();

for(Account_Info__c acinfo :[select id, End_Customer__c, Account_Manager__c from Account_Info__c]) {
    actMap.put(acinfo.End_Customer__c, acinfo.Account_Manager__c);
}

for(User usr : [select Id, Name from User where isActive = true and name in :actMap.values()]) {
    usrMap.put(usr.name,usr.Id);

}

for(Opportunity newOpp : trigger.new) {
  accountMgr = actMap.get(End_Customer__c);
  usrId = usrMap.get(accountMgr);
 
  OpportunityTeamMemberList.add(new OpportunityTeamMeber(UserId = usrId, OpportunityId = newOpp.Id));

}


if(OpportunityTeamMemberList.size() > 0){
 insert OpportunityTeamMemberList;
}

}

 

Thanks,

JBabu.

This was selected as the best answer
Rajat MahajanRajat Mahajan

Cheers!!!! Thank you so much for your help.

 

I just had to make small variable variations to get it to work. But i understood how you got it done.

 

Thanks once again,

Rajat