• SaiVineeth Maddula
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
Hi, I have written a trigger for the requirement which is after inserting a record in the Campaign Account(Custom Object lookup with Account) it should check the Campaign Account Name with opportunities related to the Account. I have wrote a test class for it but it was not running.
//Trigger
public class CreateOppAfterCAInsertionHelper {
    public Boolean stopRecursive = TRUE;
    public void insertOpp(List<Campaign_Account__c> campaignAccList) {
        Map<Id,Map<String,List<Opportunity>>> existingOpportunities = new Map<Id,Map<String,List<Opportunity>>>();
        List<Opportunity> oppsToUpdate = new List<Opportunity>();
        Set<Id> accIds = new Set<Id>();
        Set<String> campaignNames = new Set<String>();
        for(Campaign_Account__c campAcc : campaignAccList) {
            if(campAcc.Account__c != NULL) {
                accIds.add(campAcc.Account__c);
                campaignNames.add(campAcc.Name);
                existingOpportunities.put(campAcc.Account__c,new Map<String,List<Opportunity>>());
            }
        }
        Map<String,List<Opportunity>> oppMap = new Map<String,List<Opportunity>>();
        for(Opportunity record : [SELECT Name, AccountId, Count__c 
                                  FROM Opportunity 
                                  WHERE AccountId IN: accIds and Name IN: campaignNames]) {
                                      if(oppMap.containsKey(record.Name)) {
                                          oppMap.get(record.Name).add(record);
                                      }
                                      else {
                                          oppMap.put(record.Name, new List<Opportunity>{record});
                                      }
                                      existingOpportunities.put(record.AccountId, oppMap);
                                  }
        for(Campaign_Account__c campAcc : campaignAccList) {
            if(campAcc.Account__c == NULL) {
                continue;
            }
            for(Integer i=0; i<existingOpportunities.get(campAcc.Account__c).get(campAcc.Name).size(); i++) {
                Opportunity existingOpp = existingOpportunities.get(campAcc.Account__c).get(campAcc.Name)[i];
                if(existingOpp.Name != campAcc.Name && existingOpp == NULL) {
                    Opportunity newOpp = new Opportunity(Name=campAcc.Name, AccountId=campAcc.Account__c, StageName='Closed Won', CloseDate=date.today(), Count__c=0);
                    insert newOpp;
                }
                else {
                    if(existingOpp.Count__c == NULL) {
                        existingOpp.Count__c = 1;
                    }
                    else {
                        existingOpp.Count__c = existingOpp.Count__c + 1;
                    }
                    oppsToUpdate.add(existingOpp);
                }
            }
        }
        Map<Id,Opportunity> newMap = new Map<Id,Opportunity>();
        newMap.putAll(oppsToUpdate);
        if(newMap.size() > 0) {
            update newMap.values();
        }
    }
}
//Test Class
@isTest
public class CreateOppAfterCAInsertionHelperTest {
    @testSetup
    static void recordsInsertion() {
        List<Opportunity> newOppsList = new List<Opportunity>();
        List<Campaign_Account__c> newCampaignAccs = new List<Campaign_Account__c>();
        Account newAcc = new Account(Name='Test Acc');
        insert newAcc;
        
        newOppsList.add(new Opportunity(Name='TestOpp', StageName='Closed Won', CloseDate=date.today(), AccountId=newAcc.Id));
        newOppsList.add(new Opportunity(Name='CampaignOpp', StageName='Prospecting', CloseDate=date.today(), AccountId=newAcc.Id));
        insert newOppsList;
        
        newCampaignAccs.add(new Campaign_Account__c(Name='Test Record', Account__c=newAcc.Id));
        newCampaignAccs.add(new Campaign_Account__c(Name='CampaignOpp', Account__c=newAcc.Id));
        insert newCampaignAccs;
    }
    @isTest
    static void method1() {
        Test.startTest();
        CreateOppAfterCAInsertionHelper campAccHelper = new CreateOppAfterCAInsertionHelper();
        Test.stopTest();
        
        for(Opportunity opp : [SELECT Name, Count__c FROM Opportunity WHERE Name = 'CampaignOpp']) {
            system.assertEquals(1, opp.Count__c);
        }
    }
}
Test class is throwing error at the highlighted line. Can someone help me with how to resolve it...
 
Hi,
I have  a requirement where I would like to add contacts to campaign record when status(Custom Field) of Contact is Closed Lost. I was stuck at adding contacts to campaign. Can someone help me acheive this since I am new to salesforce..

//Batch Class

global class AddContactsToCampaignBatch implements Database.Batchable<SObject>{
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator([SELECT Id, Name 
                                         FROM Campaign 
                                         WHERE Name = 'Pipeline Campaign']);
    }
    global void execute(Database.BatchableContext BC, List<Contact> conList) {
        for(Contact con : [SELECT Id, Status__c 
                           FROM Contact]) {
                               if(con.Status__c == 'Closed Lost') {
                                  conList.add(con); 
                               }
                           }
        update conList;
    }
    global void finish(Database.BatchableContext BC) {
        
    }
}

Thanks in Advane....
Hi, I am trying to write a batch class which will delete the opportunities whose CloseDate is last month and update checkbox in related account.

//Batch Class

global class DeleteOppsBatch implements Database.Batchable<SObject> {
    List<Account> newList = new List<Account>();
    global Database.QueryLocator start (Database.BatchableContext BC) {
        return Database.getQueryLocator([Select Id, DeletedOpp__c, (SELECT Id, CloseDate FROM Opportunities WHERE CloseDate = LAST_N_DAYS:30) FROM Account]);
    }
    global void execute(Database.BatchableContext BC, List<Account> accList) {
        for(Account acc : accList) {
            acc.DeletedOpp__c = TRUE;
            newList.add(acc);
        }
        //update newList;
    }
    global void finish(Database.BatchableContext BC) {
        System.debug('listsize:'+newList.size());
        delete newList;
    }
}

I didn't understand why this class is not working. Can someone help me achieve this functionality.

Thanks in advance.
Hi,
I have  a requirement where I would like to add contacts to campaign record when status(Custom Field) of Contact is Closed Lost. I was stuck at adding contacts to campaign. Can someone help me acheive this since I am new to salesforce..

//Batch Class

global class AddContactsToCampaignBatch implements Database.Batchable<SObject>{
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator([SELECT Id, Name 
                                         FROM Campaign 
                                         WHERE Name = 'Pipeline Campaign']);
    }
    global void execute(Database.BatchableContext BC, List<Contact> conList) {
        for(Contact con : [SELECT Id, Status__c 
                           FROM Contact]) {
                               if(con.Status__c == 'Closed Lost') {
                                  conList.add(con); 
                               }
                           }
        update conList;
    }
    global void finish(Database.BatchableContext BC) {
        
    }
}

Thanks in Advane....
Hi, I am trying to write a batch class which will delete the opportunities whose CloseDate is last month and update checkbox in related account.

//Batch Class

global class DeleteOppsBatch implements Database.Batchable<SObject> {
    List<Account> newList = new List<Account>();
    global Database.QueryLocator start (Database.BatchableContext BC) {
        return Database.getQueryLocator([Select Id, DeletedOpp__c, (SELECT Id, CloseDate FROM Opportunities WHERE CloseDate = LAST_N_DAYS:30) FROM Account]);
    }
    global void execute(Database.BatchableContext BC, List<Account> accList) {
        for(Account acc : accList) {
            acc.DeletedOpp__c = TRUE;
            newList.add(acc);
        }
        //update newList;
    }
    global void finish(Database.BatchableContext BC) {
        System.debug('listsize:'+newList.size());
        delete newList;
    }
}

I didn't understand why this class is not working. Can someone help me achieve this functionality.

Thanks in advance.