+ Start a Discussion
VRKVRK 

Apex logic issue

Hi folks,
Below logic for 
When Deal Team records created, then same records created into Opp Team member records .
But , i required Logic AS :
If already records available in Opp Team Member , Then we can skip and create only new records
can any one helpme how to write logic for this :

global class BatchClass implements Database.Batchable<sobject>{
    
   Set<Id> dealIds = new Set<Id>();
   String sQuery;
   map<id, opportunity> mapOppIdRec = new map<id, opportunity>();
    // Start Method
    global Database.Querylocator start (Database.BatchableContext BC) {
        String sQuery;
       List<Deal_Team__c> listDealIds = [SELECT Name, Description, Deal__c FROM Deal_Team__c WHERE createdDate = yesterday];
       if (listDealIds.size()>0){
            sQuery='SELECT Name, Description, Deal__c FROM Deal_Team__c'; 
        }else {
            sQuery='SELECT Name, Description, Deal__c FROM Deal_Team__c where id=null';
        }
        return Database.getQueryLocator(sQuery);
    }
    
    // Execute method
    global void execute (Database.BatchableContext bc , List<Deal_Team__c> records){
        if (records.size()>0){
            List<OpportunityTeamMember> listToBeInserted = new List<OpportunityTeamMember>();           
            for (Deal_Team__c dealTeams : records){ 
            OpportunityTeamMember newOtm = new OpportunityTeamMember();
            newOtm.OpportunityId = dealTeams.Deal__c;
            //newOtm.CreatedBy = 'OpportunityTeamMember is created by' +userInfo.getUserId();
            listToBeInserted.add(newOtm);
            }         
            upsert listToBeInserted;     
        }
    }
 
    // Finish Method
    global void finish(Database.BatchableContext BC) {}
}
Best Answer chosen by VRK
GauravendraGauravendra
Hi VRK,
I believe both the objects are related to opportunity. One way is to get all the opportunityId from Deal Team object and using this oppIds fetch all the opportunity team member. Using the map you can compare the both of them and create new record if no match found.
List<Id> oppId = new List<Id>();
Map<Id, Deal_Team__c> oppIdDealTeam = new Map<Id, Deal_Team__c>();
Map<Id, OpportunityTeamMember > oppIdOTM = new Map<Id, OpportunityTeamMember >();
for (Deal_Team__c dealTeams : records){ 
           oppId.add(dealTeams.Opportunity__c);
           oppIdDealTeam.put(dealTeams.Opportunity__c, dealTeams);
}
for(OpportunityTeamMember otm : [Select Id From OpportunityTeamMember WHERE Opportunity__c IN: oppId]) {
    oppIdOTM.put(otm. Opportunity__c,otm);
}

for(Id opId : oppIdDealTeam.keySet()) {
if(oppIdOTM.containsKey(opId) {
    // do nothing
} else {
   // create OTM record
}
}

Hope this helps!

All Answers

GauravendraGauravendra
Hi VRK,
I believe both the objects are related to opportunity. One way is to get all the opportunityId from Deal Team object and using this oppIds fetch all the opportunity team member. Using the map you can compare the both of them and create new record if no match found.
List<Id> oppId = new List<Id>();
Map<Id, Deal_Team__c> oppIdDealTeam = new Map<Id, Deal_Team__c>();
Map<Id, OpportunityTeamMember > oppIdOTM = new Map<Id, OpportunityTeamMember >();
for (Deal_Team__c dealTeams : records){ 
           oppId.add(dealTeams.Opportunity__c);
           oppIdDealTeam.put(dealTeams.Opportunity__c, dealTeams);
}
for(OpportunityTeamMember otm : [Select Id From OpportunityTeamMember WHERE Opportunity__c IN: oppId]) {
    oppIdOTM.put(otm. Opportunity__c,otm);
}

for(Id opId : oppIdDealTeam.keySet()) {
if(oppIdOTM.containsKey(opId) {
    // do nothing
} else {
   // create OTM record
}
}

Hope this helps!
This was selected as the best answer
VRKVRK
Thanks for your prompt response.
Yes, both are related to Oppertunity object 
where can we write this logic ? is this under Execute method () right?
Thanks
VRK
GauravendraGauravendra
Yes.
GauravendraGauravendra
Hi VRK,

You have to create OppotunityTeamMember record each time and add to the list.The else method will look something like this:
else {
            OpportunityTeamMember newOtm = new OpportunityTeamMember();
              newOtm.OpportunityId = oppIdDealTeam.get(opId).Deal__c;
              newOtm.description__c = oppIdDealTeam.get(opId).description__c;
            listToBeInserted.add(newOtm);
        }

Hope this helps!
VRKVRK
Thank you Gauravendra for your prompt response ...