You need to sign in to do that
Don't have an account?
Trying to Add Opportunity field update to Opportunity Sales Team Batch
I built a trigger that we now use in production that runs nightly and iterates through all opportunities looking for where one of our partners is listed as either the account or the opportunity partner. Where it finds one of the two options it adds everything with that partner's role (we are using partner portal) to the sales team for that opportunity.
I am wanting to add a statement to the code that, after the partner users are added to the sales team, updates the Date_Partner_Added__c field (date field) with today's date.
For the life of me I've tried several different ways and it's not working. Here's the code as it sits today. Any help is appreciated.
global class UniplanBatchOpportunityPartners implements Database.Batchable<SObject>{ global String query; global String email; global Id toUserId; global Id fromUserId; global database.querylocator start(Database.BatchableContext BC){ system.debug('@@@BC=' + BC); system.debug('@@@query=' + query); return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, Sobject[] scope){ //PURPOSE: TO ITERATE THRU ALL THE OPPORTUNTIES WITH THE Uniplan GmbH & Co KG ACCOUNT PARTNER // AND UPSERT ALL THE ACCOUNT SALES TEAM MEMBERS INTO THE OPPORTUNITY SALES TEAM MEMBER LIST List<Opportunity> oList = new List<Opportunity>(); List<Account> aList = new List<Account>(); List<User> uList = new List<User>(); List<OpportunityShare> oShareList = new List<OpportunityShare>(); List<OpportunityTeamMember> oTMList = new List<OpportunityTeamMember>(); List<OpportunityTeamMember> opTMList = new List<OpportunityTeamMember>(); List<AccountTeamMember> aTMList = new List<AccountTeamMember>(); Set<Id> oIdSet = new Set<Id>(); Set<Id> accSet = new Set<Id>(); Set<Id> uIdSet = new Set<Id>(); Set<Id> aPIdSet = new Set<Id>(); Set<Id> aTMUserIdSet = new Set<Id>(); Set<Id> oTMUserIdSet = new Set<Id>(); Set<Id> aIdSet = new Set<Id>(); Set<Id> oShareIdSet = new Set<Id>(); Set<Id> opAccIdSet = new Set<Id>(); Set<Id> inactiveOOwnerIdSet = new Set<Id>(); Map<Id,Account> aMap = new Map<Id,Account>(); Map<Id,List<User>> accMap = new Map<Id,List<User>>(); Map<Id,Set<Id>> aTMIdMap = new Map<Id,Set<Id>>(); //<Account Id, Current Set of User Ids> Map<Id,OpportunityShare> oShareMap = new Map<Id,OpportunityShare>(); Map<Id,List<AccountTeamMember>> aTMMap = new Map<Id,List<AccountTeamMember>>(); Map<Id,Set<Id>> aTMUserIdMap = new Map<Id,Set<Id>>(); Map<Id,Set<Id>> opOIdOpIdSetMap = new Map<Id,Set<Id>>(); Map<Id,Set<Id>> oTMIdSetMap = new Map<Id,Set<Id>>(); Id oId; Id aId = '0014000000GvtqD'; String idStr; //THESE ARE THE ACCOUNTS THAT ARE TO BE USED IN THE BATCH accSet.add('0014000000GvtqD'); accSet.add('00130000008w1qI'); accSet.add('0014000000MnJPv'); accSet.add('0014000000MnJAu'); accSet.add('0014000000MnJB5'); accSet.add('0014000000MnJVI'); accSet.add('0014000000KGcAk'); accSet.add('0014000000MnJRz'); accSet.add('0014000000MnJ0e'); //BUILD THE accMap WHICH HOLDS THE LIST OF USERS FOR EACH ACCOUNT in the accSET for (Id accId : accSet) accMap.put (accId,new List<User>()); UserRole ur = [Select u.Name, u.Id From UserRole u where name = 'Uniplan GmbH & Co KG Partner User' limit 1]; uList = [Select u.UserRole.Name, u.UserRoleId, u.Id, u.isActive, u.UserRole.PortalAccountId From User u where userRole.name = 'Uniplan GmbH & Co KG Partner User' and isActive=true limit 1000]; //BUILD A SET OF ACTIVE Uniplan GmbH & Co KG Partner User USERS for (User u : uList) uIdSet.add(u.id); //BUILD A LIST OF ACCOUNTS WITH VALID ACCOUNT TEAM MEMBERS aList = [Select a.Name, a.Id, (Select Id, AccountId, UserId, TeamMemberRole, AccountAccessLevel, IsDeleted From AccountTeamMembers WHERE UserId in :uIdSet and isDeleted = false) From Account a where id in :accSet]; //BUILD A MAP OF ACCOUNTS WITH VALID ACCOUNT TEAM MEMBERS Boolean aTMFlag = false; for (Account a : aList) { aTMList = a.AccountTeamMembers; if (aTMList.size()>0) aTMFlag = true; aTMMap.put(a.id, a.AccountTeamMembers); aTMUserIdSet = new Set<Id>(); for (AccountTeamMember atm :aTMList) aTMUserIdSet.add(atm.UserId); aTMUserIdMap.put(a.id,aTMUserIdSet); } system.debug('@@@uList=' + uList); system.debug('@@@aTMUserIdMap=' + aTMUserIdMap); system.debug('@@@scope.size=' + scope.size()); system.debug('@@@scope=' + scope); //CHECK IF THERE EXISTS ACCOUNT TEAM MEMBERS if (atmFlag==true) { Integer ct = 0; oIdSet = new Set<Id>(); olist = new List<Opportunity>(); //INITIALIZE SETS AND LISTS Id curOpId = null; oTMUserIdSet = new Set<Id>(); oShareIdSet = new Set<Id>(); oTMList = new List<OpportunityTeamMember>(); oShareList = new List<OpportunityShare>(); inactiveOOwnerIdSet = new Set<Id>(); //LOOP THRU ALL THE OPPORTUNITY PARTNERS for(sobject s : scope){ OpportunityPartner op = (OpportunityPartner)s; oIdSet.add(op.opportunityId); //system.debug('@@@op=' + op); } oList = [Select o.Id, o.Owner.IsActive, o.OwnerId, o.IsDeleted, o.Date_Uniplan_Partner_Added__c, (Select OpportunityId, UserId From OpportunityTeamMembers) From Opportunity o where id in :oIdSet]; for (Opportunity oo : oList) { Set<Id> tmpOTMSet = new Set<Id>(); if (oo.Owner.IsActive == false || oo.isDeleted == true) inactiveOOwnerIdSet.add(oo.id); for (OpportunityTeamMember oOTM : oo.OpportunityTeamMembers) tmpOTMSet.add(oOTM.UserId); oTMIdSetMap.put(oo.Id, tmpOTMSet); } system.debug('@@@oTMIdSetMap=' + oTMIdSetMap); for(sobject s : scope){ OpportunityPartner op = (OpportunityPartner)s; //system.debug('@@@op.opportunityId=' + op.opportunityId); //system.debug('@@@inactiveOOwnerIdSet=' + inactiveOOwnerIdSet); if (inactiveOOwnerIdSet.contains(op.opportunityId) == false) { //CHECK IF NEW OPPORTUNITY ID if (curOpId != op.opportunityId) { //FIRST CREATE OTM AND SHARE RECORDS FOR EACH USER IN THE oTMUserIdSet //LOOP THRU ALL THE oTMUserIdSet SET OF USER IDS FOR THE AccountToId for (Id oTMId : oTMUserIdSet) { if (oTMIdSetMap.get(op.opportunityId).contains(oTMId) == false) { //ADD A NEW OTM RECORD TO THE LIST //ADD A NEW SHARE RECORD TO THE LIST oTMList.add(new OpportunityTeamMember(UserId = oTMId, OpportunityId=op.opportunityId, TeamMemberRole='Uniplan')); oShareList.add(new OpportunityShare(UserOrGroupId = oTMId, OpportunityId=op.opportunityId, OpportunityAccessLevel='Edit')); if (oTMList.size()==1000) { insert oTMList; oTMList = new List<OpportunityTeamMember>(); } if (oShareList.size()==1000) { insert oShareList; oShareList = new List<OpportunityShare>(); } } } system.debug('@@@oTMList=' + oTMList); system.debug('@@@op.AccountToId=' + op.AccountToId + ' :aTMUserIdMap.get(op.AccountToId)=' + aTMUserIdMap.get(op.AccountToId)); //RESET THE curOpId AND oTMUserIdSet FOR NEXT OPPORTUNITY ID curOpId = op.opportunityId; oTMUserIdSet = new Set<Id>(); } //ADD ALL THE ACCOUNT TEAM MEMBERS FROM THE OPPORTUNITY PARTNER'S AccountToId ID //TO THE SET OF OPPORTUNITY A for (Id opId : aTMUserIdMap.get(op.AccountToId)) oTMUserIdSet.add(opId); } } system.debug('@@@oTMList=' + oTMList); system.debug('@@@oShareList=' + oShareList); //INSERT THE Opportunity Team Member List //INSERT THE OPPORTUNITY SHARE MEMBER TABLE WITH //EDIT ACCESS. NEW OPPORTUNITY SHARE MEMBER RECORDS //WERE CREATED ON OPPORTUNITY TEAM MEMBER RECORDS CREATED if (oTMList.size()>0)insert oTMList; if (oShareList.size()>0) insert oShareList; } } global void finish(Database.BatchableContext BC){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(new String[] {email}); mail.setSubject('Batch Process Complete-Uniplan Account Sales Teams Added to Opportunities'); mail.setReplyTo(email); mail.setSenderDisplayName('Batch Processing'); mail.setPlainTextBody('Batch Process has completed'); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } }