You need to sign in to do that
Don't have an account?
sam_Admin
Schedule Apex is not updating records
Description: When the Primary campaign field from the Opp is Null then that field is updated by the most recent campaign from the primary contact role
Apex class:
global class UpdPrimaryCampaignOnOpty implements Database.Batchable<sObject>,Schedulable{
global String Query;
global UpdPrimaryCampaignOnOpty(){
query = 'Select Id, contactId, opportunityId,isPrimary from OpportunityContactRole where (createddate = today or lastmodifieddate = today) and opportunity.CampaignId = null';
}
global void execute(SchedulableContext SC){
UpdPrimaryCampaignOnOpty upc = new UpdPrimaryCampaignOnOpty();
ID batchprocessid = Database.executeBatch(upc);
}
global DataBase.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
System.debug('scope size = '+scope.size());
set<Id> conIds = new Set<Id>();
Set<Id> optyIds = new Set<Id>();
Map<Id,Id> optyConMap = new Map<Id,Id>();
for(sObject s : scope){
OpportunityContactRole ocr = (OpportunityContactRole)s;
if(ocr.isPrimary){
conIds.add(ocr.contactId);
optyIds.add(ocr.opportunityId);
optyConMap.put(ocr.OpportunityId, ocr.ContactId);
}
}
System.debug('conIds size = '+conIds.size());
System.debug('optyIds size = '+optyIds.size());
System.debug('optyConMap size = '+optyConMap.size());
List<CampaignMember> cmList = [Select id, CampaignId, ContactId, Campaign.createddate from CampaignMember where ContactId IN :conIds];
System.debug('cmList size = '+cmList.size());
Map<Id,CampaignMember> contactCampaignMap = new Map<Id,CampaignMember>();
for(CampaignMember cm : cmList){
if(contactCampaignMap.containsKey(cm.contactId) && cm.Campaign.createddate > contactCampaignMap.get(cm.contactId).Campaign.CreatedDate ){
contactCampaignMap.put(cm.contactId, cm);
}
else{
contactCampaignMap.put(cm.contactId, cm);
}
}
List<Opportunity> optyList = [Select Id, CampaignId from Opportunity where Id IN :optyIds];
for(Opportunity opty : optyList){
if(optyConMap.containsKey(opty.id) && contactCampaignMap.containsKey(optyConMap.get(opty.Id))){
opty.CampaignId = contactCampaignMap.get(optyConMap.get(opty.Id)).CampaignId;
}
}
update optyList;
}
global void finish(Database.BatchableContext BC){
}
}
Apex class:
global class UpdPrimaryCampaignOnOpty implements Database.Batchable<sObject>,Schedulable{
global String Query;
global UpdPrimaryCampaignOnOpty(){
query = 'Select Id, contactId, opportunityId,isPrimary from OpportunityContactRole where (createddate = today or lastmodifieddate = today) and opportunity.CampaignId = null';
}
global void execute(SchedulableContext SC){
UpdPrimaryCampaignOnOpty upc = new UpdPrimaryCampaignOnOpty();
ID batchprocessid = Database.executeBatch(upc);
}
global DataBase.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
System.debug('scope size = '+scope.size());
set<Id> conIds = new Set<Id>();
Set<Id> optyIds = new Set<Id>();
Map<Id,Id> optyConMap = new Map<Id,Id>();
for(sObject s : scope){
OpportunityContactRole ocr = (OpportunityContactRole)s;
if(ocr.isPrimary){
conIds.add(ocr.contactId);
optyIds.add(ocr.opportunityId);
optyConMap.put(ocr.OpportunityId, ocr.ContactId);
}
}
System.debug('conIds size = '+conIds.size());
System.debug('optyIds size = '+optyIds.size());
System.debug('optyConMap size = '+optyConMap.size());
List<CampaignMember> cmList = [Select id, CampaignId, ContactId, Campaign.createddate from CampaignMember where ContactId IN :conIds];
System.debug('cmList size = '+cmList.size());
Map<Id,CampaignMember> contactCampaignMap = new Map<Id,CampaignMember>();
for(CampaignMember cm : cmList){
if(contactCampaignMap.containsKey(cm.contactId) && cm.Campaign.createddate > contactCampaignMap.get(cm.contactId).Campaign.CreatedDate ){
contactCampaignMap.put(cm.contactId, cm);
}
else{
contactCampaignMap.put(cm.contactId, cm);
}
}
List<Opportunity> optyList = [Select Id, CampaignId from Opportunity where Id IN :optyIds];
for(Opportunity opty : optyList){
if(optyConMap.containsKey(opty.id) && contactCampaignMap.containsKey(optyConMap.get(opty.Id))){
opty.CampaignId = contactCampaignMap.get(optyConMap.get(opty.Id)).CampaignId;
}
}
update optyList;
}
global void finish(Database.BatchableContext BC){
}
}
There are a couple of things that you should check for.
All Answers
There are a couple of things that you should check for.
I tried your code and executed batch apex through console but records didn't get updated
I just want to ensure that you are executing this class in the expected way. If you want to execute the above stated Batch Job code you will need to use the following code to invoke it. You can then check the status of the executing job under Setup > Scheduled Apex Jobs within your Salesforce instance.
If you are using Database.schedule() to execute the above code from your Developer Console, you need to be aware that Salesforce does not necessarily execute the job at the specified time but just queues it for processing. As and when resources become available, the job is considered for processing. Hence, the Scheduled Apex Jobs console comes is handy to monitor the status.
I removed (createddate =today or lastmodifieddate = today) from below query and it worked, not sure why i had that at in my code but thanks for the help
Select Id, contactId, opportunityId,isPrimary from OpportunityContactRole where (createddate =today or lastmodifieddate = today) and opportunity.CampaignId = null