You need to sign in to do that
Don't have an account?
VRK
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) {}
}
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) {}
}
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.
Hope this helps!
All Answers
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.
Hope this helps!
Yes, both are related to Oppertunity object
where can we write this logic ? is this under Execute method () right?
Thanks
VRK
You have to create OppotunityTeamMember record each time and add to the list.The else method will look something like this:
Hope this helps!