• Admin_dev.ax1029
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 9
    Replies

Hello Devs,

    Not sure what i have to modify in test class to make it 75% coverage , but right now it's only giving me 22%, any help is appreciated, this code can be implemented in any org since no custom fields are involved.

 

global class UpdPrimaryCampaignOnOpty implements Database.Batchable<sObject>{

    global String Query;
    global UpdPrimaryCampaignOnOpty(){
        query = 'Select Id, contactId, opportunityId,isPrimary from OpportunityContactRole where (createddate = today or lastmodifieddate = today) and  opportunity.CampaignId = null';
    }
   
   
     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){
      
   }
        static testMethod void myUnitTest() {
        Test.StartTest();
        UpdPrimaryCampaignOnOpty upc =  new UpdPrimaryCampaignOnOpty();       
        upc.query = upc.query +'Limit 200' ;
        ID batchprocessid = Database.executeBatch(upc);
        Test.StopTest();    
   
  } 
}

Hello,

 

Salesforce doesn't allow to write the triggers on Opportunity contact role and iam not sure how to implement this .

 

I am looking if it is possible to get the most recent campaign from the primary contact role  and assign this campaign to the primary campaign source field in Oppty if this field is null.?

 

There is no way to implement this ? Plz Help

 

Thanks

SF is setup so that when you convert a lead to an opportunity, the opportunity will automatically get a primary campaign source based on the most recent campaign that lead was associated with. SF also populates a PCS when creating an opportunity from a contact.


I am looking if it is possible to get the most recent campaign from the primary contact role from opp and assign this campaign to the primary campaign source field in Oppty if this field is null.

 

Is this something that could be done with APEX triggers?

Hi Folks,

Iam kinda hard in figuring out the formula field for this since i don't see Is changed in formula.

 

Is it possible to calculate how many times the case status has been changed from "Developed" to "Working","New","Assigned", "Under review","Acknowledge" i couldn't figure out this one since i can't find Ischanged in formula .

 

TIA!

 



Hello Devs,

    Not sure what i have to modify in test class to make it 75% coverage , but right now it's only giving me 22%, any help is appreciated, this code can be implemented in any org since no custom fields are involved.

 

global class UpdPrimaryCampaignOnOpty implements Database.Batchable<sObject>{

    global String Query;
    global UpdPrimaryCampaignOnOpty(){
        query = 'Select Id, contactId, opportunityId,isPrimary from OpportunityContactRole where (createddate = today or lastmodifieddate = today) and  opportunity.CampaignId = null';
    }
   
   
     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){
      
   }
        static testMethod void myUnitTest() {
        Test.StartTest();
        UpdPrimaryCampaignOnOpty upc =  new UpdPrimaryCampaignOnOpty();       
        upc.query = upc.query +'Limit 200' ;
        ID batchprocessid = Database.executeBatch(upc);
        Test.StopTest();    
   
  } 
}

Hello,

 

Salesforce doesn't allow to write the triggers on Opportunity contact role and iam not sure how to implement this .

 

I am looking if it is possible to get the most recent campaign from the primary contact role  and assign this campaign to the primary campaign source field in Oppty if this field is null.?

 

There is no way to implement this ? Plz Help

 

Thanks

Hi Folks,

Iam kinda hard in figuring out the formula field for this since i don't see Is changed in formula.

 

Is it possible to calculate how many times the case status has been changed from "Developed" to "Working","New","Assigned", "Under review","Acknowledge" i couldn't figure out this one since i can't find Ischanged in formula .

 

TIA!