-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
49Questions
-
62Replies
Trigger to Update Opportunity Price Book
Hello,
I created the class below to fire on an Insert/Update trigger on the Opportunity. It is meant to change the Opportunity price book based upon the value entered in a custom picklist on the Opp. Everything saves and runs without error, but nothing gets changed. Am I missing something that would make the update? Thanks,
I created the class below to fire on an Insert/Update trigger on the Opportunity. It is meant to change the Opportunity price book based upon the value entered in a custom picklist on the Opp. Everything saves and runs without error, but nothing gets changed. Am I missing something that would make the update? Thanks,
Public Class ClassSetPriceBook { public void pbSet(List<Opportunity> SetPB){ ID pb1 = '01sK00000000y6x'; ID pb2 = '01sK00000000y72'; ID pb3 = '01sK00000000y77'; ID pb4 = '01sK00000000y7C'; String Edition1 = 'Edition-1'; String Edition2 = 'Edition-2'; String Edition3 = 'Edition-3'; String Edition4 = 'Edition-4'; for(Opportunity opp : SetPB) { if (opp.Edition__c == Edition1) { opp.Pricebook2Id = pb1; } else if (opp.Type == Edition2) { opp.Pricebook2Id = pb2; } else if (opp.Type == Edition3) { opp.Pricebook2Id = pb3; } else if (opp.Type == Edition4) { opp.Pricebook2Id = pb4; } } } }
- John Neilan
- April 24, 2015
- Like
- 0
Add Products to a New Opportunity
Hello,
I have a trigger that is meant to fire when certain conditions are met on an edited Opportunity. The trigger is designed to create a new Opportunity using some values from the previous Opportunity. I also want to pull through the OpportunityLineItems associated with the old Opportunity. I tried cloning, but the issue was that there are a large number of fields I don't want to clone and I need to set them to blank individually. Essentially, I am just trying to clone an Opps products, nothing else. I have the code below to create the Opp, but I'm stuck on how to clone the products. Can anyone help? Thanks,
I have a trigger that is meant to fire when certain conditions are met on an edited Opportunity. The trigger is designed to create a new Opportunity using some values from the previous Opportunity. I also want to pull through the OpportunityLineItems associated with the old Opportunity. I tried cloning, but the issue was that there are a large number of fields I don't want to clone and I need to set them to blank individually. Essentially, I am just trying to clone an Opps products, nothing else. I have the code below to create the Opp, but I'm stuck on how to clone the products. Can anyone help? Thanks,
public class ClassAccountManagerHandoffNotes { public void addNotes(List<Account_Manager_Handoff__c> accountManagerHandoffs){ Set<Id> accountsInTrigger = new Set<Id>(); Map<Id,String> accountMap = new Map<Id,String>(); Map<Id,Id>accountMap2 = new Map<Id,Id>(); List<Account> accountsToUpdate = new List<Account>(); List<Opportunity> createNewOpp = new List<Opportunity>; FOR(Account_Manager_Handoff__c amh : accountManagerHandoffs){ IF(amh.AccountLookup__c != NULL){ accountsInTrigger.add(amh.AccountLookup__c); String notes = amh.Platform_Experience__c + '\n\n' + amh.Issues_Encountered__c + '\n\n' + amh.Known_Goals__c + '\n\n' + amh.Additional_Notes__c; String OppId = amh.Opportunity__c; Id owner = amh.Assigned_Account_Manager__c; accountMap.put(amh.AccountLookup__c,notes); accountMap2.put(amh.AccountLookup__c,owner); //Clone the Opportunity that is associated with the handoff and all createable fields IF(amh.Handoff_Official__c == TRUE){ Opportunity opp New Opportunity(); opp.accountId = amh.AccountLookup__r.Id; opp.CloseDate = amh.Opportunity__r.Next_Renewal_date__c; opp.Probability = 10; opp.OwnerId = amh.Assigned_Account_Manager__c; opp.Term__c = amh.Opportunity__r.Term__c; opp.Renewal_Base__c = amh.Opportunity__r.Final_Annual_Amount_for_Trigger__c; opp.Effective_Date__c = amh.Opportunity__r.Next_Renewal_Date__c; opp.Renewed_Opportunity__c = amh.Opportunity__r.Id; opp.RP_Contract_Start__c = amh.Opportunity__r.Effective_Date__c; opp.StageName = 'Phase 1: Planning & Training'; createNewOpp.add(opp); } } }
- John Neilan
- April 09, 2015
- Like
- 0
Flow Error From VF Page
Hello,
I have a flow in place that is assigned to a custom List Button on the Opportunity object. It is meant to create an Opportunity when the custom button is pressed the related list on the Account. The flow requires some fields to be completed on the Account before the Opportunity can be created (i.e., Industry, Website). I also want the user to be re-directed to the Opportunity Products page upon creation of the Opportunity. In order to do this, I created a custom VF page and controller and utilized the Flow component in the VF page. However, when I click on the custom button, I get the error:
Error: Unable to Access Page The value of the "id" parameter contains a character that is not allowed or the value exceeds the maximum allowed length. Remove the character from the parameter value or reduce the value length and resubmit. If the error still persists, report it to our Customer Support team. Provide the URL of the page you were requesting as well as any other related information.
Does anyone know I am getting this error?
VF Controller:
I have a flow in place that is assigned to a custom List Button on the Opportunity object. It is meant to create an Opportunity when the custom button is pressed the related list on the Account. The flow requires some fields to be completed on the Account before the Opportunity can be created (i.e., Industry, Website). I also want the user to be re-directed to the Opportunity Products page upon creation of the Opportunity. In order to do this, I created a custom VF page and controller and utilized the Flow component in the VF page. However, when I click on the custom button, I get the error:
Error: Unable to Access Page The value of the "id" parameter contains a character that is not allowed or the value exceeds the maximum allowed length. Remove the character from the parameter value or reduce the value length and resubmit. If the error still persists, report it to our Customer Support team. Provide the URL of the page you were requesting as well as any other related information.
Does anyone know I am getting this error?
VF Controller:
public class OpptyFlowController { public ApexPages.StandardSetController stdControl{get; set;} public OpptyFlowController(ApexPages.StandardSetController controller) { stdControl = controller; } public Flow.Interview.Create_Opportunity_w_Parent_Fixed OppFlow { get; set; } public String getOppID() { if (OppFlow==null) return ''; else return (Id)OppFlow.getVariableValue('OpportunityId'); } public PageReference getOID(){ PageReference p = new PageReference('/p/opp/SelectSearch?addTo=' + getOppID() + '&retURL=%2F' + getOppID()); p.setRedirect(true); return p; } }VF Page:
<apex:page standardController="Account" tabStyle="Account" Extensions="OpptyFlowController" recordSetVar="opportunities"> <apex:outputField value="{!Account.Id}"/> <flow:interview name="Create_Opportunity_w_Parent_Fixed" interview="{!OppFlow}" finishLocation="{!OID}">- <apex:param name="AccountId" value="{!Account.Id}"/> <apex:param name="AccountWebsite" value="{!Account.Website}"/> <apex:param name="AccountIndustry" value="{!Account.Industry}"/> <apex:param name="AccountCountry" value="{!Account.BillingCountry}"/> <apex:param name="AccountSubIndustry" value="{!Account.Sub_Industry__c}"/> </flow:interview> </apex:page>
- John Neilan
- March 27, 2015
- Like
- 0
List Button to VF Page
Hello,
I've created a custom List button on the Opportunity object that sits on the related list of the Account object. Users go to an Account, and when they want to create a new Opportunity they click this button, which overrides the standard New button on the Opportunity related list. When clicking the button, the user is redirected to a VF page (below), which then invokes a custom flow from a custom VF controller. The flow works properly on its own, however, when I try to save the VF page, I get the error:
Error: No variable named "AccountWebsite" in flow
Can anyone help me figure out why I am getting this error and how to fix it?
VF Page:
I've created a custom List button on the Opportunity object that sits on the related list of the Account object. Users go to an Account, and when they want to create a new Opportunity they click this button, which overrides the standard New button on the Opportunity related list. When clicking the button, the user is redirected to a VF page (below), which then invokes a custom flow from a custom VF controller. The flow works properly on its own, however, when I try to save the VF page, I get the error:
Error: No variable named "AccountWebsite" in flow
Can anyone help me figure out why I am getting this error and how to fix it?
VF Page:
<apex:page standardController="Account" tabStyle="Account" Extensions="OpptyFlowController" recordSetVar="opportunities"> <flow:interview name="Create_Opportunity_w_Parent_Fixed" finishLocation="{!OID}"> <apex:param name="AccountId" value="{!Account.Id}"/> <apex:param name="AccountWebsite" value="{!Account.Website}"/> <apex:param name="AccountIndustry" value="{!Account.Industry}"/> <apex:param name="AccountCountry" value="{!Account.BillingCountry}"/> <apex:param name="AccountSubIndustry" value="{!Account.Sub_Industry__c}"/> </flow:interview> </apex:page>VF Controller:
public class OpptyFlowController { public ApexPages.StandardSetController stdControl{get; set;} public OpptyFlowController(ApexPages.StandardSetController controller) { stdControl = controller; } public Flow.Interview.Create_Opportunity_w_Parent_Fixed OppFlow { get; set; } public String getOppID() { if (OppFlow==null) return ''; else return OppFlow.OpportunityId; } public PageReference getOID(){ PageReference p = new PageReference('/p/opp/SelectSearch?addTo=' + getOppID() + '&retURL=%2F' + getOppID()); p.setRedirect(true); return p; } }
- John Neilan
- March 21, 2015
- Like
- 0
Add VF Page to Custom List Button
Hello,
I've created a custom VF Page and a custom controller. I want to add a custom List button to the Opportunity object that will invoke this VF page when pressed. However, when I try to create the button, my VF page does not appear as an option. Does anyone know how I can get it to appear? Thanks.
VF Page:
VF Controller:
I've created a custom VF Page and a custom controller. I want to add a custom List button to the Opportunity object that will invoke this VF page when pressed. However, when I try to create the button, my VF page does not appear as an option. Does anyone know how I can get it to appear? Thanks.
VF Page:
<apex:page standardController="Opportunity" tabStyle="Opportunity"> <flow:interview name="Create_Opportunity_w_Parent_Fixed" finishLocation="/p/opp/SelectSearch?addTo={!opportunity.Id}&retURL=%2F{!opportunity.Id}"/> </apex:page>
VF Controller:
public class OpptyFlowController { public Flow.Interview.Create_Opportunity_w_Parent_Fixed OppFlow { get; set; } public String getOppID() { if (OppFlow==null) return ''; else return OppFlow.OpptyID; } public PageReference getOID(){ PageReference p = new PageReference('/p/opp/SelectSearch?addTo=' + getOppID() + '&retURL=%2F' + getOppID(); p.setRedirect(true); return p; } }
- John Neilan
- March 18, 2015
- Like
- 0
Test Class for Opportunity Update
I have a test class where I am trying to cover code on an update trigger when the Effective Date or Stage is changed. However, when I run the test, none of the Opportunity data gets changed. Do I have the proper syntax in my test to test for a change to the Opportunity?
static testmethod void testAcctData(){ Account acct5 = TestCreateRecords.createAcct(0); insert acct5; Opportunity opp1 = TestCreateRecords.createOppNew(acct5.Id); insert opp1; Test.startTest(); Opportunity opp1a = [SELECT Id,Effective_Date__c,StageName FROM Opportunity WHERE Id =: opp1.Id]; opp1a.Effective_Date__c = Date.TODAY().addDays(5); opp1a.StageName = 'Phase 5: Closed Won'; UPDATE opp1a; Test.stopTest(); }
- John Neilan
- March 05, 2015
- Like
- 0
Record Type Name in Test Class
I have a test class that creates records for use in unit tests. One of the components creates an Opportunity record, but I don't know how to add a Record Type to the Opp. I know I can use RecordTypeID, but since I don't want to hardcode that, I am looking to use the Record Type name. Can anyone help me figure out the syntax I need to use? Thanks,
public static Opportunity createOppNew (Id acctId){ Opportunity opp1 = new Opportunity(); opp1.Name = 'Test Renewal Opp'; opp1.StageName = 'Phase 1'; opp1.CloseDate = Date.today().addYears(1); opp1.Renewal__c = 'Yes'; opp1.Effective_Date__c = Date.today().addYears(1); opp1.RP_Contract_Start__c = Date.today().addDays(1); opp1.Term__c = 12; opp1.AccountId = acctId; opp1.RP_Customer_Engagement__c = 0; return opp1; }
- John Neilan
- March 05, 2015
- Like
- 0
Trigger Issue
Hello,
I have an after trigger that fires when an Opportunity is updated. I'm having an issue because when I look for the Account ID related to the Oppotunity, it is returning as Null even though the Opportunity is absolutely linked to an Account (system debug at line 25). All the other variables in the debug statement return as expected. Does anyone know why the Account field does not? Thanks,
Trigger:
Trigger Class:
I have an after trigger that fires when an Opportunity is updated. I'm having an issue because when I look for the Account ID related to the Oppotunity, it is returning as Null even though the Opportunity is absolutely linked to an Account (system debug at line 25). All the other variables in the debug statement return as expected. Does anyone know why the Account field does not? Thanks,
Trigger:
ClassOpp2Account updater14 = new ClassOpp2Account(); updater14.mgdUpdates(Trigger.new);
Trigger Class:
public class ClassOpp2Account{ public void mgdUpdates(List<Opportunity> MgdAcctUpdates){ Map<Id,Account> acctMap = new Map<Id,Account>(); Set<id> Ids = new Set<id>(); String recordTypeName = 'Renewals'; Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Opportunity.getRecordTypeInfosByName(); Schema.RecordTypeInfo rtInfo = rtMapByName.get(recordTypeName); id recType = rtInfo.getRecordTypeId(); List<Account> accountsToUpdate = new List<Account>(); for (Opportunity opp : MgdAcctUpdates) { Ids.add(opp.Account.Id); } Map<Id,Account> acctMap2 = new Map<Id,Account>([SELECT Id,Name,Managed_Renewal_Date__c,Managed_Term__c,Managed_Value__c FROM Account WHERE Id in :Ids]); for (Opportunity opp2 : MgdAcctUpdates){ system.debug('***#*** IsClosed: '+opp2.IsClosed+' Stage: '+opp2.StageName+' Opp Rec Type: '+opp2.RecordTypeId+' Rec Type: '+recType+' Acct Name: '+opp2.Account); if(opp2.RecordTypeId == recType && opp2.IsClosed == true){ if(acctMap2.containsKey(opp2.Account.Id)){ Account acct = acctMap2.get(opp2.Account.Id); if(acctMap2.get(opp2.Account.Id).Managed_Renewal_Date__c < opp2.Renewal_Date_Next__c || acctMap2.get(opp2.Account.Id).Managed_Renewal_Date__c == null) { acct.Managed_Renewal_Date__c = opp2.Renewal_Date_Next__c; acct.Managed_Term__c = opp2.Term__c; acct.Managed_Value__c = opp2.Final_Annualized_Amount__c; } acctMap.put(acct.id,acct); } } } update acctMap.values(); } }
- John Neilan
- March 03, 2015
- Like
- 0
Help Bulkifying Trigger
Hello,
I have a fairly complex trigger that I've been told is not done according to best practice. I understand that I have SOQL and DML statements in FOR loops, but quite frankly I'm not sure how to remove them based on the complexity of my trigger. I've looked at the documentation, which does well explaining the typical use cases, but I'm not sure how to apply it to my trigger. Can anyone give me some guidance? Thanks,
Trigger:
Trigger Clone Class:
I have a fairly complex trigger that I've been told is not done according to best practice. I understand that I have SOQL and DML statements in FOR loops, but quite frankly I'm not sure how to remove them based on the complexity of my trigger. I've looked at the documentation, which does well explaining the typical use cases, but I'm not sure how to apply it to my trigger. Can anyone give me some guidance? Thanks,
Trigger:
trigger MainTriggerOpportunity on Opportunity (after update) { ClassRenewalOppClone updater13 = new ClassRenewalOppClone(); updater13.cloneOpp(Trigger.new); }Trigger Handler Class:
public class ClassRenewalOppClone { public void cloneOpp(List<Opportunity> cloneOpp) { String recordTypeName = 'Renewals'; Map<String, Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Opportunity.getRecordTypeInfosByName(); Schema.RecordTypeInfo rtInfo = rtMapByName.get(recordTypeName); id recType = rtInfo.getRecordTypeId(); FOR(Opportunity opp1 : cloneOpp) { IF(opp1.StageName.contains('Closed Won') && trigger.OldMap.get(opp1.Id).isclosed == false && opp1.RecordTypeId == recType) { String OppId = opp1.Id; //Clone the Opportunity that is associated with the handoff and all createable fields /* query Opportunity and then clone it */ String soql = RecClone.getCreatableFieldsSOQL('Opportunity', 'Id =: OppId'); Opportunity opp = (Opportunity)Database.query(soql); Opportunity opp2 = opp.clone(false, true); insert opp2; List<OpportunityLineItem> itemList = (List<OpportunityLineItem>)Database.query(RecClone.getCreatableFieldsSOQL('OpportunityLineItem', 'OpportunityId =: OppId')); List<OpportunityLineItem> newItemList = new List<OpportunityLineItem>(); for (OpportunityLineItem item : itemList) { OpportunityLineItem ol = item.clone(false, true); ol.totalprice = null; ol.opportunityid = opp2.id; newItemList.add(ol); } insert newItemList; } } } }
Trigger Clone Class:
//This class is used to clone all the creatable fields on objects for use with Apex cloning. public with sharing class RecClone{ // Returns a dynamic SOQL statement for the whole object, includes only creatable fields since we will be inserting a cloned result of this query public static string getCreatableFieldsSOQL(String objectName, String whereClause){ String selects = ''; if (whereClause == null || whereClause == ''){ return null; } // Get a map of field name and field token Map<String, Schema.SObjectField> fMap = Schema.getGlobalDescribe().get(objectName.toLowerCase()).getDescribe().Fields.getMap(); list<string> selectFields = new list<string>(); if (fMap != null){ for (Schema.SObjectField ft : fMap.values()){ // loop through all field tokens (ft) Schema.DescribeFieldResult fd = ft.getDescribe(); // describe each field (fd) // if (fd.isAccessible()){ // field is Accessible if (fd.isCreateable()){ // field is creatable selectFields.add(fd.getName()); } } } if (!selectFields.isEmpty()){ for (string s:selectFields){ selects += s + ','; } if (selects.endsWith(',')){selects = selects.substring(0,selects.lastIndexOf(','));} } return 'SELECT ' + selects + ' FROM ' + objectName + ' WHERE ' + whereClause; } }
- John Neilan
- February 19, 2015
- Like
- 0
Fire Trigger Only When Stage Moves to Closed Won
I have the trigger class below and I would like it to fire nly when an Opportunity Stage is changed to Closed Won. Can anyone helpme figure out how to do this? I have an If statement, but it fires any time an Opportunity that is Closed Won is edited, and when I tried to add another condition it gives me the error message: Error: Compile Error: Field expression not allowed for generic SObject at line 12 column 85. I only want it to fire when the Stage is initially changed. Thanks.
Trigger:
Trigger Class:
Trigger:
trigger MainTriggerOpportunity on Opportunity (after update) { ClassRenewalOppClone updater13 = new ClassRenewalOppClone(); updater13.cloneOpp(Trigger.new); }
Trigger Class:
public class ClassRenewalOppClone { public void cloneOpp(List<Opportunity> cloneOpp){ String recordTypeName = 'Renewals'; Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Opportunity.getRecordTypeInfosByName(); Schema.RecordTypeInfo rtInfo = rtMapByName.get(recordTypeName); id recType = rtInfo.getRecordTypeId(); FOR(Opportunity opp1 : cloneOpp){ IF(opp1.StageName.contains('Closed Won') && trigger.OldMap.get(opp1.Id).isclosed == false && opp1.RecordTypeId == recType){ String OppId = opp1.Id; //Clone the Opportunity that is associated with the handoff and all createable fields /* query Opportunity and then clone it */ String soql = RecClone.getCreatableFieldsSOQL('Opportunity','Id =: OppId'); Opportunity opp = (Opportunity)Database.query(soql); Opportunity opp2 = opp.clone(false, true); insert opp2; List<OpportunityLineItem> itemList = (List<OpportunityLineItem>)Database.query(RecClone.getCreatableFieldsSOQL('OpportunityLineItem','OpportunityId =: OppId')); List<OpportunityLineItem> newItemList = new List<OpportunityLineItem>(); for (OpportunityLineItem item : itemList) { OpportunityLineItem ol = item.clone(false, true); ol.totalprice = null; ol.opportunityid = opp2.id; newItemList.add(ol); } insert newItemList; } } } }
- John Neilan
- February 19, 2015
- Like
- 0
Trigger Error - Attempt to De-Reference Null Object
Hello,
I'm trying to create a trigger class that will clone an Opportunity that is of a certain record type and reaches a Stage that contains Close Won. Everything saves fine, but when I try to close out an Opp I get the error message:
System.NullPointerException: Attempt to de-reference a null object: Class.ClassRenewalOppClone.cloneOpp: line 8, column 1
Does anyone know why I might be getting this?
Trigger Class:
I'm trying to create a trigger class that will clone an Opportunity that is of a certain record type and reaches a Stage that contains Close Won. Everything saves fine, but when I try to close out an Opp I get the error message:
System.NullPointerException: Attempt to de-reference a null object: Class.ClassRenewalOppClone.cloneOpp: line 8, column 1
Does anyone know why I might be getting this?
Trigger Class:
public class ClassRenewalOppClone { public void cloneOpp(List<Opportunity> cloneOpp){ String recordTypeName = 'Renewals'; Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Campaign.getRecordTypeInfosByName(); Schema.RecordTypeInfo rtInfo = rtMapByName.get(recordTypeName); id recType = rtInfo.getRecordTypeId(); FOR(Opportunity opp1 : cloneOpp){ IF(opp1.StageName.contains('Closed Won') && opp1.RecordTypeId == recType){Before Update Trigger:
ClassRenewalOppClone updater4 = new ClassRenewalOppClone(); updater4.cloneOpp(Trigger.new);
- John Neilan
- February 12, 2015
- Like
- 0
Test to Cover Page Redirect in VF Controller
Hello,
I have a custom VF controller and I'm trying to write some line in my test that will cover the Save and page redirect sections of the controller (lines 21-24 below). Can anyone help me figure out the test code that will cover the page redirect? Thanks,
Controller
Test Class
I have a custom VF controller and I'm trying to write some line in my test that will cover the Save and page redirect sections of the controller (lines 21-24 below). Can anyone help me figure out the test code that will cover the page redirect? Thanks,
Controller
public class VF_CQController{ public List<Client_Questionnaire__c> cq {get; set;} private final Account acct; public VF_CQController(ApexPages.StandardController myController){ cq = new List<Client_Questionnaire__c>(); acct=(Account)myController.getrecord(); } public Client_Questionnaire__c cq2 = new Client_Questionnaire__c(); public void clientQuest(){ cq2.Account_Name__c = acct.id; cq.add(cq2); } public PageReference save() { insert cq; { PageReference RetPage = new PageReference('/apex/ClientQuestInitVerify?id=' + cq[0].id); RetPage.setRedirect(true); return RetPage; } } }
Test Class
@IsTest public class TestVFControllers { static testMethod void Test_CQ_Overall() { User user1 = TestCreateRecords.createAMUser(); insert user1; Account acct1 = TestCreateRecords.createAcct(0); insert acct1; Client_Questionnaire__c cq1 = TestCreateRecords.createCQNull(acct1.Id); ApexPages.StandardController sc1 = new ApexPages.standardController(acct1); VF_CQController cqCont1 = new VF_CQController(sc1); cqCont1.cq.add(cq1); Test.StartTest(); cqCont1.clientQuest(); Test.StopTest(); }
- John Neilan
- February 09, 2015
- Like
- 0
Exclude Class From Code Coverage
Hello,
I wrote a Class to use in my test Classes. The purpose of this class is to create varius records that I can reference in test classes. However, when I try to deploy it to production, it shows as 0% coverage and will not let me deploy. Is there a way I can exclude this class from the code coverage calculation?
Class:
I wrote a Class to use in my test Classes. The purpose of this class is to create varius records that I can reference in test classes. However, when I try to deploy it to production, it shows as 0% coverage and will not let me deploy. Is there a way I can exclude this class from the code coverage calculation?
Class:
public class TestCreateRecords { // create and insert a new Account record. public static Account createAcct(Integer i){ Profile ProSys = [SELECT Id FROM Profile WHERE Name='Client Services (Admin)']; User U1 = new User(Alias = 'User1',Country='United States',Email='User1@testing.com',EmailEncodingKey='ISO-8859-1', LastName='User1', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = ProSys.Id,TimeZoneSidKey='America/New_York', UserName='User1@testing.com'); insert U1; Account acct = new Account(); acct.Name = 'Test'; acct.Region__c = 'USA'; acct.BillingCountry = 'USA'; acct.Industry = 'Consumer Goods'; acct.Status__c = 'Customer'; acct.Website = 'www.test.com'; acct.OwnerId = U1.Id; return acct; } }
- John Neilan
- February 06, 2015
- Like
- 0
Test Custom Controller
Hello,
I'm having trouble getting adequate test coverage for a custom VF controller I created. My test and controller are below. When I run the test, I am getting no coverage for lines 13, 16-17, and 26-46. Can anyone point me to what I need to do to get these lines covered? The VF page works properly when tested. Thanks
Test Class:
Controller:
I'm having trouble getting adequate test coverage for a custom VF controller I created. My test and controller are below. When I run the test, I am getting no coverage for lines 13, 16-17, and 26-46. Can anyone point me to what I need to do to get these lines covered? The VF page works properly when tested. Thanks
Test Class:
@Istest(SeeAllData=true) public class TestVFControllers { public static testMethod void testCQ1() { User user1 = TestCreateRecords.createAMUser(); insert user1; Account acct1 = TestCreateRecords.createAcct(0); insert acct1; Client_Questionnaire__c cq1 = TestCreateRecords.createCQ(acct1.Id); ApexPages.StandardController sc1 = new ApexPages.standardController(acct1); VF_CQController cqCont = new VF_CQController(sc1); cqCont.cq.add(cq1); cqCont.save(); } }
Controller:
public class VF_CQController{ public List<Client_Questionnaire__c> cq {get; set;} private final Account acct; public VF_CQController(ApexPages.StandardController myController){ cq = new List<Client_Questionnaire__c>(); acct=(Account)myController.getrecord(); system.debug('*##***** acct: ' + acct); } public Client_Questionnaire__c cq2 = new Client_Questionnaire__c(); public void clientQuest(){ system.debug('*##*****:'+cq2); cq2.Account_Name__c = acct.id; cq.add(cq2); system.debug('*##***** Account Name: '+cq2.Account_Name__c); } public PageReference save() { IF(cq2.Initial_Meeting_Date__c == null){ cq2.Initial_Meeting_Date__c.addError('Please enter the date of the initial meeting you have with this client'); return null; } IF(cq2.Primary_Goal__c == null){ cq2.Primary_Goal__c.addError('Please enter the Primar Goal for this client'); return null; } IF(cq2.Training__c == null){ cq2.Training__c.addError('Please enter the initial training you have set up for this client'); return null; } IF(cq2.Training_Availability__c == null){ cq2.Training_Availability__c.addError('Please enter the client’s availability for training'); return null; } IF(cq2.Recurring_Meeting__c == null){ cq2.Recurring_Meeting__c.addError('Please enter the recurring meeting schedule set up for this client'); return null; } insert cq; { PageReference RetPage = new PageReference('/apex/ClientQuestInitVerify?id=' + cq[0].id); RetPage.setRedirect(true); return RetPage; } } }
- John Neilan
- February 05, 2015
- Like
- 0
Help To Increase Test Coverage
Hello,
I have created a trigger that fires when a Task is Inserted, Updated, or Deleted. When it fires, it updates custom checkboxes on open Opportunities based upon the Subject of the Task and checks to see that the ActivityDate is within the effective and renewal dates of the Opp. All works fine, however, I am not able to get my test to cover the part of the code that works on checking one of the boxes after an update. Can anyone advise on how I can get the code covered below (Specifically line 14 and lines 19-37 of the Trigger Class?
Trigger:
Trigger Class:
Test Method:
I have created a trigger that fires when a Task is Inserted, Updated, or Deleted. When it fires, it updates custom checkboxes on open Opportunities based upon the Subject of the Task and checks to see that the ActivityDate is within the effective and renewal dates of the Opp. All works fine, however, I am not able to get my test to cover the part of the code that works on checking one of the boxes after an update. Can anyone advise on how I can get the code covered below (Specifically line 14 and lines 19-37 of the Trigger Class?
Trigger:
IF(Trigger.IsUpdate){ ClassRenewalTaskCount updater4 = new ClassRenewalTaskCount(); updater4.taskCountUpdate(Trigger.new,Trigger.old); }
Trigger Class:
public class ClassRenewalTaskCount { Set<ID> OppIds = new Set<ID>(); Set<ID> TaskIdsRem = new Set<ID>(); String acctPrefix = Account.SObjectType.getDescribe().getKeyPrefix(); public void taskCountUpdate(List<Task> checkTasks, List<Task> oldTasks) { for (Task t : checkTasks) { for (Task prev : oldTasks){ //Adds a task that is updated to completed and current if (t.WhatId != null && t.Status == 'Completed' && t.RP_Prior_60__c <= t.ActivityDate && string.valueof(t.WhatId).startsWith(acctPrefix) ) { OppIds.add(t.WhatId); } } } if (OppIds.size() > 0){ List<Account> acctsWithTasks = [SELECT Id,(SELECT Id FROM Tasks) FROM Account WHERE Id IN : OppIds]; List<Opportunity> oppsUpdatable = new List<Opportunity>(); for(Account acct : acctsWithTasks){ for(Opportunity L : [SELECT Id,Account.Id,RP_Customer_Engagement__c,IsWon FROM Opportunity WHERE Account.Id =: acct.Id AND IsWon=FALSE]){ L.RP_Customer_Engagement__c = acct.Tasks.size(); oppsUpdatable.add(L); } } if(oppsUpdatable.size()>0){ update oppsUpdatable; } } } }
Test Method:
static testmethod void testRenewalTasks3(){ Account acct1 = TestCreateRecords.createAcct(0); insert acct1; Opportunity opp1 = TestCreateRecords.createOppNew(acct1.Id); insert opp1; opp1.RP_Customer_Engagement__c = 0; UPDATE opp1; Task task1 = TestCreateRecords.createTaskInitProg(acct1.Id); INSERT task1; Task task2 = new task(); task2.WhatId = acct1.Id; task2.ActivityDate = Date.today().addDays(10); task2.Subject = 'User Training Complete'; task2.Status = 'In Progress'; task2.Priority = 'Normal'; INSERT task2; Test.startTest(); Task tsk1 = [SELECT Id,ActivityDate,Status FROM Task WHERE Id =: task1.Id]; tsk1.Status = 'In Progress'; UPDATE tsk1; Task tsk2 = [SELECT Id,ActivityDate,Status FROM Task WHERE Id =: tsk1.Id]; tsk2.Status = 'Completed'; UPDATE tsk2; Test.stopTest(); }
- John Neilan
- January 29, 2015
- Like
- 0
Increase Test Coverage
Hello,
I have a trigger handler class below that fires on a Task event and updates custom checkbox fields on the Opportunity object based on a few variables in the Task record. The trigger fires as appropriate, however, I am having difficulty getting coverage on lines 39 - 50 with the test class I have below (specifically lines 58 - 75 to cover that portion of the trigger). Can anyone give me guidance on how I can cover these lines?
Handler Class:
Test Class:
I have a trigger handler class below that fires on a Task event and updates custom checkbox fields on the Opportunity object based on a few variables in the Task record. The trigger fires as appropriate, however, I am having difficulty getting coverage on lines 39 - 50 with the test class I have below (specifically lines 58 - 75 to cover that portion of the trigger). Can anyone give me guidance on how I can cover these lines?
Handler Class:
public void renewalTasks(List<Task> checkTasks) { List<Opportunity> linkedOpps = new List<Opportunity>(); Map<Id, Task> taskMap = new Map<Id, Task>(); for (Task t: checkTasks) { if (t.WhatId != null && (t.Subject == 'User Training Complete' || t.Subject == 'Initial Program Confirmation' || t.Subject == 'Renewal Package Review')){ taskMap.put(t.WhatId, t); } } if (taskMap.size() > 0) { linkedOpps = [SELECT Account.Id, Effective_Date__c, Renewal_Date_Next__c, RP_User_Training__c, RP_Initial_Program_Developed__c, RP_Recommendation_Review_Call__c FROM Opportunity WHERE Account.Id IN: taskMap.keySet() AND IsClosed =: FALSE]; for (Opportunity c: linkedOpps){ for (Task taskAdd:[SELECT WhatId, Subject, Status, ActivityDate FROM Task WHERE WhatId =: c.Account.Id]){ IF(taskAdd.Status == 'Completed' && taskAdd.ActivityDate >= c.Effective_Date__c && taskAdd.ActivityDate <= c.Renewal_Date_Next__c){ IF(taskAdd.Subject == 'User Training Complete' && taskAdd.Status == 'Completed'){ c.RP_User_Training__c = TRUE; } IF(taskAdd.Subject == 'Initial Program Confirmation'){ c.RP_Initial_Program_Developed__c = TRUE; } IF(taskAdd.Subject == 'Renewal Package Review'){ c.RP_Recommendation_Review_Call__c = TRUE; } } ELSE IF((taskAdd.Status != 'Completed' && taskAdd.ActivityDate >= c.Effective_Date__c && taskAdd.ActivityDate <= c.Renewal_Date_Next__c) || (taskAdd.Status == 'Completed' && (taskAdd.ActivityDate < c.Effective_Date__c || taskAdd.ActivityDate > c.Renewal_Date_Next__c)) || (taskAdd.Status != 'Completed' && (taskAdd.ActivityDate < c.Effective_Date__c || taskAdd.ActivityDate > c.Renewal_Date_Next__c))){ IF(taskAdd.Subject == 'User Training Complete'){ c.RP_User_Training__c = FALSE; } IF(taskAdd.Subject == 'Initial Program Confirmation'){ c.RP_Initial_Program_Developed__c = FALSE; } IF(taskAdd.Subject == 'Renewal Package Review'){ c.RP_Recommendation_Review_Call__c = FALSE; } } } } if (linkedOpps.size() > 0) { update linkedOpps; } } }
Test Class:
@isTest public class TestRenewalTasks{ static testmethod void testRenewalTasks(){ Profile ProSys = [SELECT Id FROM Profile WHERE Name='Client Services (Admin)']; User U1 = new User(Alias = 'User1',Country='United States',Email='User1@testing.com',EmailEncodingKey='ISO-8859-1', LastName='User1', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = ProSys.Id,TimeZoneSidKey='America/New_York', UserName='User1@testing.com'); insert U1; Account acct1 = new Account(); acct1.Name = 'Test-JN'; acct1.Region__c = 'USA'; acct1.BillingCountry = 'USA'; acct1.Industry = 'Consumer Goods'; acct1.Status__c = 'Customer'; acct1.Website = 'www.test.com'; acct1.FB_Page_1_Fans__c = 500; acct1.FB_Page_1_Link__c = 'www.facebook.com/test'; acct1.OwnerId = U1.Id; INSERT acct1; Opportunity opp1 = new Opportunity(); opp1.Name = 'Test-JN Renewal Opp'; opp1.StageName = 'Phase 1: Planning & Training'; opp1.CloseDate = date.parse('1/1/15'); opp1.Renewal__c = 'Yes'; opp1.Effective_Date__c = date.parse('1/1/15'); opp1.Term__c = 12; opp1.AccountId = acct1.Id; INSERT opp1; Test.startTest(); List<Task> tasklist = new List<Task>(); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Initial Program Confirmation', Status = 'Completed', Priority = 'Normal')); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop User Training Complete', Status = 'Completed', Priority = 'Normal')); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Primary Goal Training Confirmation', Status = 'Completed', Priority = 'Normal')); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Initial Campaign Results', Status = 'Completed', Priority = 'Normal')); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Business Review', Status = 'Completed', Priority = 'Normal')); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Roadmap Review', Status = 'Completed', Priority = 'Normal')); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Manager Introduction', Status = 'Completed', Priority = 'Normal')); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Renewal Package Review', Status = 'Completed', Priority = 'Normal')); INSERT taskList; DELETE taskList; List<Task> tasklist2 = new List<Task>(); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Initial Program Confirmation', Status = 'In Progress', Priority = 'Normal')); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop User Training Complete', Status = 'In Progress', Priority = 'Normal')); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Primary Goal Training Confirmation', Status = 'In Progress', Priority = 'Normal')); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Initial Campaign Results', Status = 'In Progress', Priority = 'Normal')); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Business Review', Status = 'In Progress', Priority = 'Normal')); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Roadmap Review', Status = 'In Progress', Priority = 'Normal')); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Manager Introduction', Status = 'In Progress', Priority = 'Normal')); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Renewal Package Review', Status = 'In Progress', Priority = 'Normal')); INSERT taskList2; Test.stopTest(); } }
- John Neilan
- January 27, 2015
- Like
- 0
Increase Test Coverage
I've created a trigger and handler class on Tasks that fires when tasks are created, edited, and deleted. Custom checkboxes are either checked or unchecked on the Opportunity, based upon changes to the subject of the task, the ActivityDate, and the Status. The trigger works fine. I now created the test below to test all the potential iterations and I am only geting 63% coverage. Can anyone help me figure out how to get the bolded lines covered in my handler class?
Handler Class:
public class ClassRenewalTasks {
public void deleteTasks(List<Task> delTasks) {
List<Opportunity> linkedOpps = new List<Opportunity>();
Map<Id, Task> taskMap = new Map<Id, Task>();
Map<Id, Task> taskMapIds = new Map<Id, Task>();
for (Task t: delTasks) {
taskMap.put(t.WhatId, t);
}
for (Task t2: delTasks) {
taskMapIds.put(t2.Id, t2);
}
if (taskMap.size() > 0)
{
linkedOpps = [SELECT Id,Account.Id, Effective_Date__c, Renewal_Date_Next__c, RP_User_Training__c,
RP_Initial_Program_Developed__c,
RP_Training_Primary_Goal__c,
RP_Review_Initial_Campaign__c,
RP_Business_Review_Complete__c,
RP_Roadmap_Review_Complete__c,
RP_Vision_Meeting__c,
RP_Recommendation_Review_Call__c
FROM Opportunity
WHERE Account.Id IN: taskMap.keySet() AND IsClosed =: FALSE];
for (Opportunity opp1: linkedOpps){
for (Task delTask:[SELECT Id, WhatId, Subject, Status, ActivityDate, IsDeleted
FROM Task
WHERE Id IN: taskMapIds.keySet() AND WhatId =: opp1.Account.Id AND Status =: 'Completed' AND ActivityDate >=: opp1.Effective_Date__c AND ActivityDate <=: opp1.Renewal_Date_Next__c]){
system.debug('******** delTask ' + delTask);
IF(delTask.Subject == 'User Training Complete'){
opp1.RP_User_Training__c = FALSE;
}
ELSE IF(delTask.Subject == 'Initial Program Confirmation'){
opp1.RP_Initial_Program_Developed__c = FALSE;
}
ELSE IF(delTask.Subject == 'Primary Goal Training Confirmation'){
opp1.RP_Training_Primary_Goal__c = FALSE;
}
ELSE IF(delTask.Subject == 'Initial Campaign Results'){
opp1.RP_Review_Initial_Campaign__c = FALSE;
}
ELSE IF(delTask.Subject == 'Business Review'){
opp1.RP_Business_Review_Complete__c = FALSE;
}
ELSE IF(delTask.Subject == 'Roadmap Review'){
opp1.RP_Roadmap_Review_Complete__c = FALSE;
}
ELSE IF(delTask.Subject == 'Manager Introduction'){
opp1.RP_Vision_Meeting__c = FALSE;
}
ELSE IF(delTask.Subject == 'Renewal Package Review'){
opp1.RP_Recommendation_Review_Call__c = FALSE;
}
}
}
if (linkedOpps.size() > 0)
{
update linkedOpps;
}
}
}
public void renewalTasks(List<Task> checkTasks) {
List<Opportunity> linkedOpps = new List<Opportunity>();
Map<Id, Task> taskMap = new Map<Id, Task>();
for (Task t: checkTasks) {
if (t.WhatId != null && (t.Subject == 'User Training Complete' ||
t.Subject == 'Initial Program Confirmation' ||
t.Subject == 'Primary Goal Training Confirmation' ||
t.Subject == 'Initial Campaign Results' ||
t.Subject == 'Business Review' ||
t.Subject == 'Roadmap Review' ||
t.Subject == 'Manager Introduction' ||
t.Subject == 'Renewal Package Review')){
taskMap.put(t.WhatId, t);
System.Debug('******** TaskMapEdit ' + taskMap);
}
}
if (taskMap.size() > 0)
{
linkedOpps = [SELECT Account.Id, Effective_Date__c, Renewal_Date_Next__c, RP_User_Training__c,
RP_Initial_Program_Developed__c,
RP_Training_Primary_Goal__c,
RP_Review_Initial_Campaign__c,
RP_Business_Review_Complete__c,
RP_Roadmap_Review_Complete__c,
RP_Vision_Meeting__c,
RP_Recommendation_Review_Call__c
FROM Opportunity
WHERE Account.Id IN: taskMap.keySet() AND IsClosed =: FALSE];
for (Opportunity c: linkedOpps){
for (Task taskAdd:[SELECT WhatId, Subject, Status, ActivityDate
FROM Task
WHERE WhatId =: c.Account.Id AND Status =: 'Completed' AND ActivityDate >=: c.Effective_Date__c AND ActivityDate <=: c.Renewal_Date_Next__c]){
IF(taskAdd.Subject == 'User Training Complete' && taskAdd.Status == 'Completed'){
c.RP_User_Training__c = TRUE;
}
IF(taskAdd.Subject == 'Initial Program Confirmation'){
c.RP_Initial_Program_Developed__c = TRUE;
}
IF(taskAdd.Subject == 'Primary Goal Training Confirmation'){
c.RP_Training_Primary_Goal__c = TRUE;
}
IF(taskAdd.Subject == 'Initial Campaign Results'){
c.RP_Review_Initial_Campaign__c = TRUE;
}
IF(taskAdd.Subject == 'Business Review'){
c.RP_Business_Review_Complete__c = TRUE;
}
IF(taskAdd.Subject == 'Roadmap Review'){
c.RP_Roadmap_Review_Complete__c = TRUE;
}
IF(taskAdd.Subject == 'Manager Introduction'){
c.RP_Vision_Meeting__c = TRUE;
}
IF(taskAdd.Subject == 'Renewal Package Review'){
c.RP_Recommendation_Review_Call__c = TRUE;
}
}
for (Task taskRem:[SELECT WhatId, Subject, Status, ActivityDate
FROM Task
WHERE WhatId =: c.Account.Id AND ((Status !=: 'Completed' AND ActivityDate >=: c.Effective_Date__c AND ActivityDate <=: c.Renewal_Date_Next__c) OR
(Status =: 'Completed' AND (ActivityDate <: c.Effective_Date__c OR ActivityDate >: c.Renewal_Date_Next__c)) OR
(Status !=: 'Completed' AND (ActivityDate <: c.Effective_Date__c OR ActivityDate >: c.Renewal_Date_Next__c)))]){
IF(taskRem.Subject == 'User Training Complete'){
c.RP_User_Training__c = FALSE;
}
IF(taskRem.Subject == 'Initial Program Confirmation'){
c.RP_Initial_Program_Developed__c = FALSE;
}
IF(taskRem.Subject == 'Primary Goal Training Confirmation'){
c.RP_Training_Primary_Goal__c = FALSE;
}
IF(taskRem.Subject == 'Initial Campaign Results'){
c.RP_Review_Initial_Campaign__c = FALSE;
}
IF(taskRem.Subject == 'Business Review'){
c.RP_Business_Review_Complete__c = FALSE;
}
IF(taskRem.Subject == 'Roadmap Review'){
c.RP_Roadmap_Review_Complete__c = FALSE;
}
IF(taskRem.Subject == 'Manager Introduction'){
c.RP_Vision_Meeting__c = FALSE;
}
IF(taskRem.Subject == 'Renewal Package Review'){
c.RP_Recommendation_Review_Call__c = FALSE;
}
}
}
// if the list of cons isnt empty, update them
system.debug('linkedOpps = '+linkedOpps);
if (linkedOpps.size() > 0)
{
System.Debug('******** linkedOpps Edit ' + linkedOpps);
update linkedOpps;
}
}
}
}
Test Class:
Handler Class:
public class ClassRenewalTasks {
public void deleteTasks(List<Task> delTasks) {
List<Opportunity> linkedOpps = new List<Opportunity>();
Map<Id, Task> taskMap = new Map<Id, Task>();
Map<Id, Task> taskMapIds = new Map<Id, Task>();
for (Task t: delTasks) {
taskMap.put(t.WhatId, t);
}
for (Task t2: delTasks) {
taskMapIds.put(t2.Id, t2);
}
if (taskMap.size() > 0)
{
linkedOpps = [SELECT Id,Account.Id, Effective_Date__c, Renewal_Date_Next__c, RP_User_Training__c,
RP_Initial_Program_Developed__c,
RP_Training_Primary_Goal__c,
RP_Review_Initial_Campaign__c,
RP_Business_Review_Complete__c,
RP_Roadmap_Review_Complete__c,
RP_Vision_Meeting__c,
RP_Recommendation_Review_Call__c
FROM Opportunity
WHERE Account.Id IN: taskMap.keySet() AND IsClosed =: FALSE];
for (Opportunity opp1: linkedOpps){
for (Task delTask:[SELECT Id, WhatId, Subject, Status, ActivityDate, IsDeleted
FROM Task
WHERE Id IN: taskMapIds.keySet() AND WhatId =: opp1.Account.Id AND Status =: 'Completed' AND ActivityDate >=: opp1.Effective_Date__c AND ActivityDate <=: opp1.Renewal_Date_Next__c]){
system.debug('******** delTask ' + delTask);
IF(delTask.Subject == 'User Training Complete'){
opp1.RP_User_Training__c = FALSE;
}
ELSE IF(delTask.Subject == 'Initial Program Confirmation'){
opp1.RP_Initial_Program_Developed__c = FALSE;
}
ELSE IF(delTask.Subject == 'Primary Goal Training Confirmation'){
opp1.RP_Training_Primary_Goal__c = FALSE;
}
ELSE IF(delTask.Subject == 'Initial Campaign Results'){
opp1.RP_Review_Initial_Campaign__c = FALSE;
}
ELSE IF(delTask.Subject == 'Business Review'){
opp1.RP_Business_Review_Complete__c = FALSE;
}
ELSE IF(delTask.Subject == 'Roadmap Review'){
opp1.RP_Roadmap_Review_Complete__c = FALSE;
}
ELSE IF(delTask.Subject == 'Manager Introduction'){
opp1.RP_Vision_Meeting__c = FALSE;
}
ELSE IF(delTask.Subject == 'Renewal Package Review'){
opp1.RP_Recommendation_Review_Call__c = FALSE;
}
}
}
if (linkedOpps.size() > 0)
{
update linkedOpps;
}
}
}
public void renewalTasks(List<Task> checkTasks) {
List<Opportunity> linkedOpps = new List<Opportunity>();
Map<Id, Task> taskMap = new Map<Id, Task>();
for (Task t: checkTasks) {
if (t.WhatId != null && (t.Subject == 'User Training Complete' ||
t.Subject == 'Initial Program Confirmation' ||
t.Subject == 'Primary Goal Training Confirmation' ||
t.Subject == 'Initial Campaign Results' ||
t.Subject == 'Business Review' ||
t.Subject == 'Roadmap Review' ||
t.Subject == 'Manager Introduction' ||
t.Subject == 'Renewal Package Review')){
taskMap.put(t.WhatId, t);
System.Debug('******** TaskMapEdit ' + taskMap);
}
}
if (taskMap.size() > 0)
{
linkedOpps = [SELECT Account.Id, Effective_Date__c, Renewal_Date_Next__c, RP_User_Training__c,
RP_Initial_Program_Developed__c,
RP_Training_Primary_Goal__c,
RP_Review_Initial_Campaign__c,
RP_Business_Review_Complete__c,
RP_Roadmap_Review_Complete__c,
RP_Vision_Meeting__c,
RP_Recommendation_Review_Call__c
FROM Opportunity
WHERE Account.Id IN: taskMap.keySet() AND IsClosed =: FALSE];
for (Opportunity c: linkedOpps){
for (Task taskAdd:[SELECT WhatId, Subject, Status, ActivityDate
FROM Task
WHERE WhatId =: c.Account.Id AND Status =: 'Completed' AND ActivityDate >=: c.Effective_Date__c AND ActivityDate <=: c.Renewal_Date_Next__c]){
IF(taskAdd.Subject == 'User Training Complete' && taskAdd.Status == 'Completed'){
c.RP_User_Training__c = TRUE;
}
IF(taskAdd.Subject == 'Initial Program Confirmation'){
c.RP_Initial_Program_Developed__c = TRUE;
}
IF(taskAdd.Subject == 'Primary Goal Training Confirmation'){
c.RP_Training_Primary_Goal__c = TRUE;
}
IF(taskAdd.Subject == 'Initial Campaign Results'){
c.RP_Review_Initial_Campaign__c = TRUE;
}
IF(taskAdd.Subject == 'Business Review'){
c.RP_Business_Review_Complete__c = TRUE;
}
IF(taskAdd.Subject == 'Roadmap Review'){
c.RP_Roadmap_Review_Complete__c = TRUE;
}
IF(taskAdd.Subject == 'Manager Introduction'){
c.RP_Vision_Meeting__c = TRUE;
}
IF(taskAdd.Subject == 'Renewal Package Review'){
c.RP_Recommendation_Review_Call__c = TRUE;
}
}
for (Task taskRem:[SELECT WhatId, Subject, Status, ActivityDate
FROM Task
WHERE WhatId =: c.Account.Id AND ((Status !=: 'Completed' AND ActivityDate >=: c.Effective_Date__c AND ActivityDate <=: c.Renewal_Date_Next__c) OR
(Status =: 'Completed' AND (ActivityDate <: c.Effective_Date__c OR ActivityDate >: c.Renewal_Date_Next__c)) OR
(Status !=: 'Completed' AND (ActivityDate <: c.Effective_Date__c OR ActivityDate >: c.Renewal_Date_Next__c)))]){
IF(taskRem.Subject == 'User Training Complete'){
c.RP_User_Training__c = FALSE;
}
IF(taskRem.Subject == 'Initial Program Confirmation'){
c.RP_Initial_Program_Developed__c = FALSE;
}
IF(taskRem.Subject == 'Primary Goal Training Confirmation'){
c.RP_Training_Primary_Goal__c = FALSE;
}
IF(taskRem.Subject == 'Initial Campaign Results'){
c.RP_Review_Initial_Campaign__c = FALSE;
}
IF(taskRem.Subject == 'Business Review'){
c.RP_Business_Review_Complete__c = FALSE;
}
IF(taskRem.Subject == 'Roadmap Review'){
c.RP_Roadmap_Review_Complete__c = FALSE;
}
IF(taskRem.Subject == 'Manager Introduction'){
c.RP_Vision_Meeting__c = FALSE;
}
IF(taskRem.Subject == 'Renewal Package Review'){
c.RP_Recommendation_Review_Call__c = FALSE;
}
}
}
// if the list of cons isnt empty, update them
system.debug('linkedOpps = '+linkedOpps);
if (linkedOpps.size() > 0)
{
System.Debug('******** linkedOpps Edit ' + linkedOpps);
update linkedOpps;
}
}
}
}
Test Class:
@isTest public class TestRenewalTasks{ static testmethod void testRenewalTasks(){ Profile ProSys = [SELECT Id FROM Profile WHERE Name='Client Services (Admin)']; User U1 = new User(Alias = 'User1',Country='United States',Email='User1@testing.com',EmailEncodingKey='ISO-8859-1', LastName='User1', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = ProSys.Id,TimeZoneSidKey='America/New_York', UserName='User1@testing.com'); insert U1; Account acct1 = new Account(); acct1.Name = 'Test-JN'; acct1.Region__c = 'USA'; acct1.BillingCountry = 'USA'; acct1.Industry = 'Consumer Goods'; acct1.Status__c = 'Customer'; acct1.Website = 'www.test.com'; acct1.FB_Page_1_Fans__c = 500; acct1.FB_Page_1_Link__c = 'www.facebook.com/test'; acct1.OwnerId = U1.Id; INSERT acct1; Opportunity opp1 = new Opportunity(); opp1.Name = 'Test Opp'; opp1.StageName = 'Phase 1'; opp1.CloseDate = date.parse('1/1/15'); opp1.Renewal__c = 'Yes'; opp1.Effective_Date__c = date.parse('1/1/15'); opp1.Term__c = 12; opp1.AccountId = acct1.Id; INSERT opp1; Task tsk1 = new task(); tsk1.WhatId = acct1.Id; tsk1.ActivityDate = date.parse('1/5/15'); tsk1.Subject = 'Initial Program Confirmation'; tsk1.Status = 'Completed'; tsk1.Priority = 'Normal'; INSERT tsk1; Task tsk1a = [SELECT Id,ActivityDate,Status FROM Task WHERE Id =: tsk1.Id]; tsk1a.Status = 'In Progress'; UPDATE tsk1a; tsk1a.Status = 'Completed'; tsk1a.ActivityDate = date.parse('1/5/14'); UPDATE tsk1a; tsk1a.Status = 'In Progress'; UPDATE tsk1a; Task tsk2 = new task(); tsk2.WhatId = acct1.Id; tsk2.ActivityDate = date.parse('1/5/15'); tsk2.Subject = 'User Training Complete'; tsk2.Status = 'Completed'; tsk2.Priority = 'Normal'; INSERT tsk2; Task tsk2a = [SELECT Id,ActivityDate,Status FROM Task WHERE Id =: tsk2.Id]; tsk2a.Status = 'In Progress'; UPDATE tsk2a; tsk2a.Status = 'Completed'; tsk2a.ActivityDate = date.parse('1/5/14'); UPDATE tsk2a; tsk2a.Status = 'In Progress'; UPDATE tsk2a; Task tsk3 = new task(); tsk3.WhatId = acct1.Id; tsk3.ActivityDate = date.parse('1/5/15'); tsk3.Subject = 'Primary Goal Training Confirmation'; tsk3.Status = 'Completed'; tsk3.Priority = 'Normal'; INSERT tsk3; Task tsk3a = [SELECT Id,ActivityDate,Status FROM Task WHERE Id =: tsk3.Id]; tsk3a.Status = 'In Progress'; UPDATE tsk3a; tsk3a.Status = 'Completed'; tsk3a.ActivityDate = date.parse('1/5/14'); UPDATE tsk3a; tsk3a.Status = 'In Progress'; UPDATE tsk3a; Task tsk4 = new task(); tsk4.WhatId = acct1.Id; tsk4.ActivityDate = date.parse('1/5/15'); tsk4.Subject = 'Initial Campaign Results'; tsk4.Status = 'Completed'; tsk4.Priority = 'Normal'; INSERT tsk4; Task tsk4a = [SELECT Id,ActivityDate,Status FROM Task WHERE Id =: tsk4.Id]; tsk4a.Status = 'In Progress'; UPDATE tsk4a; tsk4a.Status = 'Completed'; tsk4a.ActivityDate = date.parse('1/5/14'); UPDATE tsk4a; tsk4a.Status = 'In Progress'; UPDATE tsk4a; Task tsk5 = new task(); tsk5.WhatId = acct1.Id; tsk5.ActivityDate = date.parse('1/5/15'); tsk5.Subject = 'Business Review'; tsk5.Status = 'Completed'; tsk5.Priority = 'Normal'; INSERT tsk5; Task tsk5a = [SELECT Id,ActivityDate,Status FROM Task WHERE Id =: tsk5.Id]; tsk5a.Status = 'In Progress'; UPDATE tsk5a; tsk5a.Status = 'Completed'; tsk5a.ActivityDate = date.parse('1/5/14'); UPDATE tsk5a; tsk5a.Status = 'In Progress'; UPDATE tsk5a; Task tsk6 = new task(); tsk6.WhatId = acct1.Id; tsk6.ActivityDate = date.parse('1/5/15'); tsk6.Subject = 'Roadmap Review'; tsk6.Status = 'Completed'; tsk6.Priority = 'Normal'; INSERT tsk6; Task tsk6a = [SELECT Id,ActivityDate,Status FROM Task WHERE Id =: tsk6.Id]; tsk6a.Status = 'In Progress'; UPDATE tsk6a; tsk6a.Status = 'Completed'; tsk6a.ActivityDate = date.parse('1/5/14'); UPDATE tsk6a; tsk6a.Status = 'In Progress'; UPDATE tsk6a; Task tsk7 = new task(); tsk7.WhatId = acct1.Id; tsk7.ActivityDate = date.parse('1/5/15'); tsk7.Subject = 'Manager Introduction'; tsk7.Status = 'Completed'; tsk7.Priority = 'Normal'; INSERT tsk7; Task tsk7a = [SELECT Id,ActivityDate,Status FROM Task WHERE Id =: tsk7.Id]; tsk7a.Status = 'In Progress'; UPDATE tsk7a; tsk7a.Status = 'Completed'; tsk7a.ActivityDate = date.parse('1/5/14'); UPDATE tsk7a; tsk7a.Status = 'In Progress'; UPDATE tsk7a; Task tsk8 = new task(); tsk8.WhatId = acct1.Id; tsk8.ActivityDate = date.parse('1/5/15'); tsk8.Subject = 'Renewal Package Review'; tsk8.Status = 'Completed'; tsk8.Priority = 'Normal'; INSERT tsk8; Task tsk8a = [SELECT Id,ActivityDate,Status FROM Task WHERE Id =: tsk8.Id]; tsk8a.Status = 'In Progress'; UPDATE tsk8a; tsk8a.Status = 'Completed'; tsk8a.ActivityDate = date.parse('1/5/14'); UPDATE tsk8a; tsk8a.Status = 'In Progress'; UPDATE tsk8a; Task tsk9 = new task(); tsk9.WhatId = acct1.Id; tsk9.ActivityDate = date.parse('1/5/15'); tsk9.Subject = 'Test Task 1'; tsk9.Status = 'Completed'; tsk9.Priority = 'Normal'; INSERT tsk9; Task tsk9a = [SELECT Id,ActivityDate,Status FROM Task WHERE Id =: tsk9.Id]; tsk9a.Status = 'In Progress'; UPDATE tsk9a; tsk9a.Status = 'Completed'; tsk9a.ActivityDate = date.parse('1/5/14'); UPDATE tsk9a; tsk9a.Status = 'In Progress'; UPDATE tsk9a; Test.startTest(); DELETE tsk1; DELETE tsk2; DELETE tsk3; DELETE tsk4; DELETE tsk5; DELETE tsk6; DELETE tsk7; DELETE tsk8; DELETE tsk9; Test.stopTest(); } }
- John Neilan
- January 22, 2015
- Like
- 0
Dataloader Insert Not Firing Trigger
Hello,
I have an after trigger that fires when a task is inserted, updated, deleted, or undeleted. I added a number of tasks via the dataloader and the trigger did not fire. When I went into one of the tasks and just clicked edit/save, the trigger fired. Does anyone know how I can get it to fire when the dataloader is used?
Trigger:
Class:
I have an after trigger that fires when a task is inserted, updated, deleted, or undeleted. I added a number of tasks via the dataloader and the trigger did not fire. When I went into one of the tasks and just clicked edit/save, the trigger fired. Does anyone know how I can get it to fire when the dataloader is used?
Trigger:
trigger MainTriggerTask on Task (after insert, after update, after delete, before delete, after undelete) { IF(Trigger.IsAfter){ IF(Trigger.IsInsert){ ClassRenewalTasks updater6 = new ClassRenewalTasks(); updater6.renewalTasks(Trigger.new); } IF(Trigger.IsUpdate){ ClassRenewalTasks updater3 = new ClassRenewalTasks(); updater3.renewalTasks(Trigger.new); } } ELSE IF(Trigger.IsBefore){ IF(Trigger.IsDelete){ ClassRenewalTasks deleteTsk = new ClassRenewalTasks(); deleteTsk.deleteTasks(Trigger.old); } } }
Class:
public class ClassRenewalTasks { public void deleteTasks(List<Task> delTasks) { List<Opportunity> linkedOpps = new List<Opportunity>(); Map<Id, Task> taskMap = new Map<Id, Task>(); for (Task t: delTasks) { taskMap.put(t.WhatId, t); System.Debug('******** TaskMapDel ' + TaskMap); } if (taskMap.size() > 0) { linkedOpps = [SELECT Id,Account.Id, Effective_Date__c, Renewal_Date_Next__c, RP_User_Training__c, RP_Initial_Program_Developed__c, RP_Recommendation_Review_Call__c FROM Opportunity WHERE Account.Id IN: taskMap.keySet() AND IsClosed =: FALSE]; for (Opportunity opp1: linkedOpps){ for (Task delTask:[SELECT WhatId, Subject, Status, ActivityDate FROM Task WHERE WhatId =: opp1.Account.Id AND Status =: 'Completed' AND ActivityDate >=: opp1.Effective_Date__c AND ActivityDate <=: opp1.Renewal_Date_Next__c]){ IF(delTask.Subject == 'Initial Program Confirmation'){ opp1.RP_Initial_Program_Developed__c = FALSE; } IF(delTask.Subject == 'Renewal Package Review'){ opp1.RP_Recommendation_Review_Call__c = FALSE; } } } } // if the list of cons isnt empty, update them system.debug('linkedOpps = '+linkedOpps); if (linkedOpps.size() > 0) { update linkedOpps; } } public void renewalTasks(List<Task> checkTasks) { // set up lists you will need List<Opportunity> linkedOpps = new List<Opportunity>(); Map<Id, Task> taskMap = new Map<Id, Task>(); // go through the list of tasks that were inserted for (Task t: checkTasks) { // if they are related to a contact, add the contact id (whoID) and their values to a map if (t.WhatId != null && (t.Subject == 'Initial Program Confirmation' || t.Subject == 'Renewal Package Review')){ taskMap.put(t.WhatId, t); } } system.debug('taskMap = '+taskMap); if (taskMap.size() > 0) { // get all of the contacts related to the tasks linkedOpps = [SELECT Account.Id, Effective_Date__c, Renewal_Date_Next__c, RP_User_Training__c, RP_Initial_Program_Developed__c, RP_Recommendation_Review_Call__c FROM Opportunity WHERE Account.Id IN: taskMap.keySet() AND IsClosed =: FALSE]; // go through the list for each contact for (Opportunity c: linkedOpps){ for (Task task2:[SELECT WhatId, Subject, Status, ActivityDate FROM Task WHERE WhatId =: c.Account.Id AND ActivityDate >=: c.Effective_Date__c AND ActivityDate <=: c.Renewal_Date_Next__c]){ IF(task2.Subject == 'Initial Program Confirmation' && task2.Status == 'Completed'){ c.RP_Initial_Program_Developed__c = TRUE; } IF(task2.Subject == 'Renewal Package Review' && task2.Status == 'Completed'){ c.RP_Recommendation_Review_Call__c = TRUE; } IF(task2.Subject == 'Initial Program Confirmation' && task2.Status <> 'Completed'){ c.RP_Initial_Program_Developed__c = FALSE; } IF(task2.Subject == 'Renewal Package Review' && task2.Status <> 'Completed'){ c.RP_Recommendation_Review_Call__c = FALSE; } } } // if the list of cons isnt empty, update them system.debug('linkedOpps = '+linkedOpps); if (linkedOpps.size() > 0) { update linkedOpps; } } } }
- John Neilan
- January 13, 2015
- Like
- 0
Activity Count on Opportunity
Hello,
The trigger below fires when a Task is Inserted, Updated, or Deleted. It looks at the Account the Task is associated with and matches it to any Open Opportunities associated to the same Account. It then takes the count of tasks and writes it to a custom field on the Opportunity. All works fine when tasks are inserted, deleted, or undeleted. However, when I move a task from a Status of Closed to something else, the count should decrease, however, it does not. I don't see any reason why it would not decrease. Can anyone help me figure it out? Thanks.
Trigger:
Class:
The trigger below fires when a Task is Inserted, Updated, or Deleted. It looks at the Account the Task is associated with and matches it to any Open Opportunities associated to the same Account. It then takes the count of tasks and writes it to a custom field on the Opportunity. All works fine when tasks are inserted, deleted, or undeleted. However, when I move a task from a Status of Closed to something else, the count should decrease, however, it does not. I don't see any reason why it would not decrease. Can anyone help me figure it out? Thanks.
Trigger:
trigger MainTriggerTask on Task (after insert, after update, after delete, before delete, after undelete) { IF(Trigger.IsAfter){ IF(Trigger.IsInsert){ ClassRenewalTaskCount updater7 = new ClassRenewalTaskCount(); updater7.taskCountInsert(Trigger.new); } } IF(Trigger.IsUpdate){ ClassRenewalTaskCount updater4 = new ClassRenewalTaskCount(); updater4.taskCountUpdate(Trigger.new,Trigger.old); } IF(Trigger.IsDelete){ ClassRenewalTaskCount updater2 = new ClassRenewalTaskCount(); updater2.taskCountDel(Trigger.old); } IF(Trigger.IsUndelete){ ClassRenewalTaskCount updater2 = new ClassRenewalTaskCount(); updater2.taskCountInsert(Trigger.new); } } }
Class:
public class ClassRenewalTaskCount { Set<ID> OppIds = new Set<ID>(); String acctPrefix = Account.SObjectType.getDescribe().getKeyPrefix(); public void taskCountInsert(List<Task> checkTasks) { for (Task t : checkTasks) { if (t.WhatId != null && t.Status == 'Completed' && t.RP_Prior_60__c <= t.ActivityDate && string.valueof(t.WhatId).startsWith(acctPrefix) ) { OppIds.add(t.WhatId); } } if (OppIds.size() > 0){ List<Account> acctsWithTasks = [SELECT Id,(SELECT Id FROM Tasks) FROM Account WHERE Id IN : OppIds]; List<Opportunity> oppsUpdatable = new List<Opportunity>(); for(Account acct : acctsWithTasks){ for(Opportunity L : [SELECT Id,Account.Id,RP_Customer_Engagement__c,IsWon FROM Opportunity WHERE Account.Id =: acct.Id AND IsWon=FALSE]){ L.RP_Customer_Engagement__c = acct.Tasks.size(); oppsUpdatable.add(L); } } if(oppsUpdatable.size()>0){ update oppsUpdatable; } } } public void taskCountUpdate(List<Task> checkTasks, List<Task> oldTasks) { for (Task t : checkTasks) { for (Task prev : oldTasks){ if (t.WhatId != null && t.Status == 'Completed' && t.RP_Prior_60__c <= t.ActivityDate && string.valueof(t.WhatId).startsWith(acctPrefix) ) { OppIds.add(t.WhatId); } } } if (OppIds.size() > 0){ List<Account> acctsWithTasks = [SELECT Id,(SELECT Id FROM Tasks) FROM Account WHERE Id IN : OppIds]; List<Opportunity> oppsUpdatable = new List<Opportunity>(); for(Account acct : acctsWithTasks){ for(Opportunity L : [SELECT Id,Account.Id,RP_Customer_Engagement__c,IsWon FROM Opportunity WHERE Account.Id =: acct.Id AND IsWon=FALSE]){ L.RP_Customer_Engagement__c = acct.Tasks.size(); oppsUpdatable.add(L); } } if(oppsUpdatable.size()>0){ update oppsUpdatable; } } } public void taskCountDel(List<Task> delTasks) { for (Task t2 : delTasks) { if (t2.WhatId!= null && t2.Status == 'Completed' && t2.RP_Prior_60__c <= t2.ActivityDate && string.valueof(t2.WhatId).startsWith(acctPrefix)){ OppIds.add(t2.WhatId); } } if (OppIds.size() > 0){ List<Account> acctsWithTasks = [SELECT Id,(SELECT Id FROM Tasks) FROM Account WHERE Id IN : OppIds]; List<Opportunity> oppsUpdatable = new List<Opportunity>(); for(Account acct : acctsWithTasks){ for(Opportunity L : [SELECT Id,Account.Id,RP_Customer_Engagement__c,IsWon FROM Opportunity WHERE Account.Id =: acct.Id AND IsWon=FALSE]){ L.RP_Customer_Engagement__c = acct.Tasks.size(); oppsUpdatable.add(L); } } if(oppsUpdatable.size()>0){ update oppsUpdatable; } } } }
- John Neilan
- January 12, 2015
- Like
- 0
Update Opportunity When Task Created on Account
Hello,
I have a trigger that is meant to update custom field on an Opportunity when specific tasks are created on an Account that is also linked to the Opportunity. The trigger below saves and does not give any errors, however, nothing happens when a task is created. Can anyone help me figure out why? Thanks.
Trigger:
Class:
I have a trigger that is meant to update custom field on an Opportunity when specific tasks are created on an Account that is also linked to the Opportunity. The trigger below saves and does not give any errors, however, nothing happens when a task is created. Can anyone help me figure out why? Thanks.
Trigger:
trigger MainTriggerTask on Task (after insert, after update, before delete) { IF(Trigger.IsAfter){ IF(Trigger.IsInsert){ ClassRenewalTasks updater6 = new ClassRenewalTasks(); updater6.renewalTasks(Trigger.new); } IF(Trigger.IsUpdate){ ClassRenewalTasks updater3 = new ClassRenewalTasks(); updater3.renewalTasks(Trigger.new); } } ELSE IF(Trigger.IsBefore){ IF(Trigger.IsDelete){ ClassRenewalTasks deleteTsk = new ClassRenewalTasks(); deleteTsk.deleteTasks(Trigger.old); } } }
Class:
public class ClassRenewalTasks { public void deleteTasks(List<Task> delTasks) { List<Opportunity> linkedOpps = new List<Opportunity>(); Map<Id, Task> taskMap = new Map<Id, Task>(); for (Task t: delTasks) { taskMap.put(t.WhatId, t); } if (taskMap.size() > 0) { linkedOpps = [SELECT Id,Account.Id, Effective_Date__c, Next_Renewal_Date__c, RP_User_Training__c, RP_Initial_Program_Developed__c FROM Opportunity WHERE Account.Id IN: taskMap.keySet()]; for (Opportunity opp1: linkedOpps){ for (Task delTask:[SELECT WhatId, Subject, Status, ActivityDate FROM Task WHERE WhatId =: opp1.Account.Id AND Subject =: 'Completed' AND ActivityDate >=: opp1.Effective_Date__c AND ActivityDate <=: opp1.Next_Renewal_Date__c]){ IF(delTask.Subject == 'User Training Complete'){ opp1.RP_User_Training__c = FALSE; } IF(delTask.Subject == 'Initial Program Confirmation'){ opp1.RP_Initial_Program_Developed__c = FALSE; } } } } // if the list of cons isnt empty, update them system.debug('linkedOpps = '+linkedOpps); if (linkedOpps.size() > 0) { update linkedOpps; } } public void renewalTasks(List<Task> checkTasks) { // set up lists you will need List<Opportunity> linkedOpps = new List<Opportunity>(); Map<Id, Task> taskMap = new Map<Id, Task>(); // go through the list of tasks that were inserted for (Task t: checkTasks) { // if they are related to a contact, add the contact id (whoID) and their values to a map if (t.WhatId != null && (t.Subject == 'User Training Complete' || t.Subject == 'Initial Program Confirmation')){ taskMap.put(t.WhatId, t); } } // if the map isnt empty // *** saying !taskMap.isEmpty() costs much less than using taskMap.size()>0 *** system.debug('taskMap = '+taskMap); if (taskMap.size() > 0) { // get all of the contacts related to the tasks linkedOpps = [SELECT Account.Id, Effective_Date__c, Next_Renewal_Date__c, RP_User_Training__c, RP_Initial_Program_Developed__c FROM Opportunity WHERE Account.Id IN: taskMap.keySet()]; // go through the list for each contact for (Opportunity c: linkedOpps){ for (Task task2:[SELECT WhatId, Subject, Status, ActivityDate FROM Task WHERE WhatId =: c.Account.Id AND ActivityDate >=: c.Effective_Date__c AND ActivityDate <=: c.Next_Renewal_Date__c]){ IF(task2.Subject == 'User Training Complete' && task2.Status == 'Completed'){ c.RP_User_Training__c = TRUE; } IF(task2.Subject == 'Initial Program Confirmation' && task2.Status == 'Completed'){ c.RP_Initial_Program_Developed__c = TRUE; } IF(task2.Subject == 'User Training Complete' && task2.Status <> 'Completed'){ c.RP_User_Training__c = FALSE; } IF(task2.Subject == 'Initial Program Confirmation' && task2.Status <> 'Completed'){ c.RP_Initial_Program_Developed__c = FALSE; } } } // if the list of cons isnt empty, update them system.debug('linkedOpps = '+linkedOpps); if (linkedOpps.size() > 0) { update linkedOpps; } } } }
- John Neilan
- January 08, 2015
- Like
- 0
Trigger to Update Opportunity Price Book
Hello,
I created the class below to fire on an Insert/Update trigger on the Opportunity. It is meant to change the Opportunity price book based upon the value entered in a custom picklist on the Opp. Everything saves and runs without error, but nothing gets changed. Am I missing something that would make the update? Thanks,
I created the class below to fire on an Insert/Update trigger on the Opportunity. It is meant to change the Opportunity price book based upon the value entered in a custom picklist on the Opp. Everything saves and runs without error, but nothing gets changed. Am I missing something that would make the update? Thanks,
Public Class ClassSetPriceBook { public void pbSet(List<Opportunity> SetPB){ ID pb1 = '01sK00000000y6x'; ID pb2 = '01sK00000000y72'; ID pb3 = '01sK00000000y77'; ID pb4 = '01sK00000000y7C'; String Edition1 = 'Edition-1'; String Edition2 = 'Edition-2'; String Edition3 = 'Edition-3'; String Edition4 = 'Edition-4'; for(Opportunity opp : SetPB) { if (opp.Edition__c == Edition1) { opp.Pricebook2Id = pb1; } else if (opp.Type == Edition2) { opp.Pricebook2Id = pb2; } else if (opp.Type == Edition3) { opp.Pricebook2Id = pb3; } else if (opp.Type == Edition4) { opp.Pricebook2Id = pb4; } } } }
- John Neilan
- April 24, 2015
- Like
- 0
List Button to VF Page
Hello,
I've created a custom List button on the Opportunity object that sits on the related list of the Account object. Users go to an Account, and when they want to create a new Opportunity they click this button, which overrides the standard New button on the Opportunity related list. When clicking the button, the user is redirected to a VF page (below), which then invokes a custom flow from a custom VF controller. The flow works properly on its own, however, when I try to save the VF page, I get the error:
Error: No variable named "AccountWebsite" in flow
Can anyone help me figure out why I am getting this error and how to fix it?
VF Page:
I've created a custom List button on the Opportunity object that sits on the related list of the Account object. Users go to an Account, and when they want to create a new Opportunity they click this button, which overrides the standard New button on the Opportunity related list. When clicking the button, the user is redirected to a VF page (below), which then invokes a custom flow from a custom VF controller. The flow works properly on its own, however, when I try to save the VF page, I get the error:
Error: No variable named "AccountWebsite" in flow
Can anyone help me figure out why I am getting this error and how to fix it?
VF Page:
<apex:page standardController="Account" tabStyle="Account" Extensions="OpptyFlowController" recordSetVar="opportunities"> <flow:interview name="Create_Opportunity_w_Parent_Fixed" finishLocation="{!OID}"> <apex:param name="AccountId" value="{!Account.Id}"/> <apex:param name="AccountWebsite" value="{!Account.Website}"/> <apex:param name="AccountIndustry" value="{!Account.Industry}"/> <apex:param name="AccountCountry" value="{!Account.BillingCountry}"/> <apex:param name="AccountSubIndustry" value="{!Account.Sub_Industry__c}"/> </flow:interview> </apex:page>VF Controller:
public class OpptyFlowController { public ApexPages.StandardSetController stdControl{get; set;} public OpptyFlowController(ApexPages.StandardSetController controller) { stdControl = controller; } public Flow.Interview.Create_Opportunity_w_Parent_Fixed OppFlow { get; set; } public String getOppID() { if (OppFlow==null) return ''; else return OppFlow.OpportunityId; } public PageReference getOID(){ PageReference p = new PageReference('/p/opp/SelectSearch?addTo=' + getOppID() + '&retURL=%2F' + getOppID()); p.setRedirect(true); return p; } }
- John Neilan
- March 21, 2015
- Like
- 0
Add VF Page to Custom List Button
Hello,
I've created a custom VF Page and a custom controller. I want to add a custom List button to the Opportunity object that will invoke this VF page when pressed. However, when I try to create the button, my VF page does not appear as an option. Does anyone know how I can get it to appear? Thanks.
VF Page:
VF Controller:
I've created a custom VF Page and a custom controller. I want to add a custom List button to the Opportunity object that will invoke this VF page when pressed. However, when I try to create the button, my VF page does not appear as an option. Does anyone know how I can get it to appear? Thanks.
VF Page:
<apex:page standardController="Opportunity" tabStyle="Opportunity"> <flow:interview name="Create_Opportunity_w_Parent_Fixed" finishLocation="/p/opp/SelectSearch?addTo={!opportunity.Id}&retURL=%2F{!opportunity.Id}"/> </apex:page>
VF Controller:
public class OpptyFlowController { public Flow.Interview.Create_Opportunity_w_Parent_Fixed OppFlow { get; set; } public String getOppID() { if (OppFlow==null) return ''; else return OppFlow.OpptyID; } public PageReference getOID(){ PageReference p = new PageReference('/p/opp/SelectSearch?addTo=' + getOppID() + '&retURL=%2F' + getOppID(); p.setRedirect(true); return p; } }
- John Neilan
- March 18, 2015
- Like
- 0
Test Class for Opportunity Update
I have a test class where I am trying to cover code on an update trigger when the Effective Date or Stage is changed. However, when I run the test, none of the Opportunity data gets changed. Do I have the proper syntax in my test to test for a change to the Opportunity?
static testmethod void testAcctData(){ Account acct5 = TestCreateRecords.createAcct(0); insert acct5; Opportunity opp1 = TestCreateRecords.createOppNew(acct5.Id); insert opp1; Test.startTest(); Opportunity opp1a = [SELECT Id,Effective_Date__c,StageName FROM Opportunity WHERE Id =: opp1.Id]; opp1a.Effective_Date__c = Date.TODAY().addDays(5); opp1a.StageName = 'Phase 5: Closed Won'; UPDATE opp1a; Test.stopTest(); }
- John Neilan
- March 05, 2015
- Like
- 0
Record Type Name in Test Class
I have a test class that creates records for use in unit tests. One of the components creates an Opportunity record, but I don't know how to add a Record Type to the Opp. I know I can use RecordTypeID, but since I don't want to hardcode that, I am looking to use the Record Type name. Can anyone help me figure out the syntax I need to use? Thanks,
public static Opportunity createOppNew (Id acctId){ Opportunity opp1 = new Opportunity(); opp1.Name = 'Test Renewal Opp'; opp1.StageName = 'Phase 1'; opp1.CloseDate = Date.today().addYears(1); opp1.Renewal__c = 'Yes'; opp1.Effective_Date__c = Date.today().addYears(1); opp1.RP_Contract_Start__c = Date.today().addDays(1); opp1.Term__c = 12; opp1.AccountId = acctId; opp1.RP_Customer_Engagement__c = 0; return opp1; }
- John Neilan
- March 05, 2015
- Like
- 0
Trigger Issue
Hello,
I have an after trigger that fires when an Opportunity is updated. I'm having an issue because when I look for the Account ID related to the Oppotunity, it is returning as Null even though the Opportunity is absolutely linked to an Account (system debug at line 25). All the other variables in the debug statement return as expected. Does anyone know why the Account field does not? Thanks,
Trigger:
Trigger Class:
I have an after trigger that fires when an Opportunity is updated. I'm having an issue because when I look for the Account ID related to the Oppotunity, it is returning as Null even though the Opportunity is absolutely linked to an Account (system debug at line 25). All the other variables in the debug statement return as expected. Does anyone know why the Account field does not? Thanks,
Trigger:
ClassOpp2Account updater14 = new ClassOpp2Account(); updater14.mgdUpdates(Trigger.new);
Trigger Class:
public class ClassOpp2Account{ public void mgdUpdates(List<Opportunity> MgdAcctUpdates){ Map<Id,Account> acctMap = new Map<Id,Account>(); Set<id> Ids = new Set<id>(); String recordTypeName = 'Renewals'; Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Opportunity.getRecordTypeInfosByName(); Schema.RecordTypeInfo rtInfo = rtMapByName.get(recordTypeName); id recType = rtInfo.getRecordTypeId(); List<Account> accountsToUpdate = new List<Account>(); for (Opportunity opp : MgdAcctUpdates) { Ids.add(opp.Account.Id); } Map<Id,Account> acctMap2 = new Map<Id,Account>([SELECT Id,Name,Managed_Renewal_Date__c,Managed_Term__c,Managed_Value__c FROM Account WHERE Id in :Ids]); for (Opportunity opp2 : MgdAcctUpdates){ system.debug('***#*** IsClosed: '+opp2.IsClosed+' Stage: '+opp2.StageName+' Opp Rec Type: '+opp2.RecordTypeId+' Rec Type: '+recType+' Acct Name: '+opp2.Account); if(opp2.RecordTypeId == recType && opp2.IsClosed == true){ if(acctMap2.containsKey(opp2.Account.Id)){ Account acct = acctMap2.get(opp2.Account.Id); if(acctMap2.get(opp2.Account.Id).Managed_Renewal_Date__c < opp2.Renewal_Date_Next__c || acctMap2.get(opp2.Account.Id).Managed_Renewal_Date__c == null) { acct.Managed_Renewal_Date__c = opp2.Renewal_Date_Next__c; acct.Managed_Term__c = opp2.Term__c; acct.Managed_Value__c = opp2.Final_Annualized_Amount__c; } acctMap.put(acct.id,acct); } } } update acctMap.values(); } }
- John Neilan
- March 03, 2015
- Like
- 0
Fire Trigger Only When Stage Moves to Closed Won
I have the trigger class below and I would like it to fire nly when an Opportunity Stage is changed to Closed Won. Can anyone helpme figure out how to do this? I have an If statement, but it fires any time an Opportunity that is Closed Won is edited, and when I tried to add another condition it gives me the error message: Error: Compile Error: Field expression not allowed for generic SObject at line 12 column 85. I only want it to fire when the Stage is initially changed. Thanks.
Trigger:
Trigger Class:
Trigger:
trigger MainTriggerOpportunity on Opportunity (after update) { ClassRenewalOppClone updater13 = new ClassRenewalOppClone(); updater13.cloneOpp(Trigger.new); }
Trigger Class:
public class ClassRenewalOppClone { public void cloneOpp(List<Opportunity> cloneOpp){ String recordTypeName = 'Renewals'; Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Opportunity.getRecordTypeInfosByName(); Schema.RecordTypeInfo rtInfo = rtMapByName.get(recordTypeName); id recType = rtInfo.getRecordTypeId(); FOR(Opportunity opp1 : cloneOpp){ IF(opp1.StageName.contains('Closed Won') && trigger.OldMap.get(opp1.Id).isclosed == false && opp1.RecordTypeId == recType){ String OppId = opp1.Id; //Clone the Opportunity that is associated with the handoff and all createable fields /* query Opportunity and then clone it */ String soql = RecClone.getCreatableFieldsSOQL('Opportunity','Id =: OppId'); Opportunity opp = (Opportunity)Database.query(soql); Opportunity opp2 = opp.clone(false, true); insert opp2; List<OpportunityLineItem> itemList = (List<OpportunityLineItem>)Database.query(RecClone.getCreatableFieldsSOQL('OpportunityLineItem','OpportunityId =: OppId')); List<OpportunityLineItem> newItemList = new List<OpportunityLineItem>(); for (OpportunityLineItem item : itemList) { OpportunityLineItem ol = item.clone(false, true); ol.totalprice = null; ol.opportunityid = opp2.id; newItemList.add(ol); } insert newItemList; } } } }
- John Neilan
- February 19, 2015
- Like
- 0
Trigger Error - Attempt to De-Reference Null Object
Hello,
I'm trying to create a trigger class that will clone an Opportunity that is of a certain record type and reaches a Stage that contains Close Won. Everything saves fine, but when I try to close out an Opp I get the error message:
System.NullPointerException: Attempt to de-reference a null object: Class.ClassRenewalOppClone.cloneOpp: line 8, column 1
Does anyone know why I might be getting this?
Trigger Class:
I'm trying to create a trigger class that will clone an Opportunity that is of a certain record type and reaches a Stage that contains Close Won. Everything saves fine, but when I try to close out an Opp I get the error message:
System.NullPointerException: Attempt to de-reference a null object: Class.ClassRenewalOppClone.cloneOpp: line 8, column 1
Does anyone know why I might be getting this?
Trigger Class:
public class ClassRenewalOppClone { public void cloneOpp(List<Opportunity> cloneOpp){ String recordTypeName = 'Renewals'; Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Campaign.getRecordTypeInfosByName(); Schema.RecordTypeInfo rtInfo = rtMapByName.get(recordTypeName); id recType = rtInfo.getRecordTypeId(); FOR(Opportunity opp1 : cloneOpp){ IF(opp1.StageName.contains('Closed Won') && opp1.RecordTypeId == recType){Before Update Trigger:
ClassRenewalOppClone updater4 = new ClassRenewalOppClone(); updater4.cloneOpp(Trigger.new);
- John Neilan
- February 12, 2015
- Like
- 0
Test to Cover Page Redirect in VF Controller
Hello,
I have a custom VF controller and I'm trying to write some line in my test that will cover the Save and page redirect sections of the controller (lines 21-24 below). Can anyone help me figure out the test code that will cover the page redirect? Thanks,
Controller
Test Class
I have a custom VF controller and I'm trying to write some line in my test that will cover the Save and page redirect sections of the controller (lines 21-24 below). Can anyone help me figure out the test code that will cover the page redirect? Thanks,
Controller
public class VF_CQController{ public List<Client_Questionnaire__c> cq {get; set;} private final Account acct; public VF_CQController(ApexPages.StandardController myController){ cq = new List<Client_Questionnaire__c>(); acct=(Account)myController.getrecord(); } public Client_Questionnaire__c cq2 = new Client_Questionnaire__c(); public void clientQuest(){ cq2.Account_Name__c = acct.id; cq.add(cq2); } public PageReference save() { insert cq; { PageReference RetPage = new PageReference('/apex/ClientQuestInitVerify?id=' + cq[0].id); RetPage.setRedirect(true); return RetPage; } } }
Test Class
@IsTest public class TestVFControllers { static testMethod void Test_CQ_Overall() { User user1 = TestCreateRecords.createAMUser(); insert user1; Account acct1 = TestCreateRecords.createAcct(0); insert acct1; Client_Questionnaire__c cq1 = TestCreateRecords.createCQNull(acct1.Id); ApexPages.StandardController sc1 = new ApexPages.standardController(acct1); VF_CQController cqCont1 = new VF_CQController(sc1); cqCont1.cq.add(cq1); Test.StartTest(); cqCont1.clientQuest(); Test.StopTest(); }
- John Neilan
- February 09, 2015
- Like
- 0
Exclude Class From Code Coverage
Hello,
I wrote a Class to use in my test Classes. The purpose of this class is to create varius records that I can reference in test classes. However, when I try to deploy it to production, it shows as 0% coverage and will not let me deploy. Is there a way I can exclude this class from the code coverage calculation?
Class:
I wrote a Class to use in my test Classes. The purpose of this class is to create varius records that I can reference in test classes. However, when I try to deploy it to production, it shows as 0% coverage and will not let me deploy. Is there a way I can exclude this class from the code coverage calculation?
Class:
public class TestCreateRecords { // create and insert a new Account record. public static Account createAcct(Integer i){ Profile ProSys = [SELECT Id FROM Profile WHERE Name='Client Services (Admin)']; User U1 = new User(Alias = 'User1',Country='United States',Email='User1@testing.com',EmailEncodingKey='ISO-8859-1', LastName='User1', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = ProSys.Id,TimeZoneSidKey='America/New_York', UserName='User1@testing.com'); insert U1; Account acct = new Account(); acct.Name = 'Test'; acct.Region__c = 'USA'; acct.BillingCountry = 'USA'; acct.Industry = 'Consumer Goods'; acct.Status__c = 'Customer'; acct.Website = 'www.test.com'; acct.OwnerId = U1.Id; return acct; } }
- John Neilan
- February 06, 2015
- Like
- 0
Help To Increase Test Coverage
Hello,
I have created a trigger that fires when a Task is Inserted, Updated, or Deleted. When it fires, it updates custom checkboxes on open Opportunities based upon the Subject of the Task and checks to see that the ActivityDate is within the effective and renewal dates of the Opp. All works fine, however, I am not able to get my test to cover the part of the code that works on checking one of the boxes after an update. Can anyone advise on how I can get the code covered below (Specifically line 14 and lines 19-37 of the Trigger Class?
Trigger:
Trigger Class:
Test Method:
I have created a trigger that fires when a Task is Inserted, Updated, or Deleted. When it fires, it updates custom checkboxes on open Opportunities based upon the Subject of the Task and checks to see that the ActivityDate is within the effective and renewal dates of the Opp. All works fine, however, I am not able to get my test to cover the part of the code that works on checking one of the boxes after an update. Can anyone advise on how I can get the code covered below (Specifically line 14 and lines 19-37 of the Trigger Class?
Trigger:
IF(Trigger.IsUpdate){ ClassRenewalTaskCount updater4 = new ClassRenewalTaskCount(); updater4.taskCountUpdate(Trigger.new,Trigger.old); }
Trigger Class:
public class ClassRenewalTaskCount { Set<ID> OppIds = new Set<ID>(); Set<ID> TaskIdsRem = new Set<ID>(); String acctPrefix = Account.SObjectType.getDescribe().getKeyPrefix(); public void taskCountUpdate(List<Task> checkTasks, List<Task> oldTasks) { for (Task t : checkTasks) { for (Task prev : oldTasks){ //Adds a task that is updated to completed and current if (t.WhatId != null && t.Status == 'Completed' && t.RP_Prior_60__c <= t.ActivityDate && string.valueof(t.WhatId).startsWith(acctPrefix) ) { OppIds.add(t.WhatId); } } } if (OppIds.size() > 0){ List<Account> acctsWithTasks = [SELECT Id,(SELECT Id FROM Tasks) FROM Account WHERE Id IN : OppIds]; List<Opportunity> oppsUpdatable = new List<Opportunity>(); for(Account acct : acctsWithTasks){ for(Opportunity L : [SELECT Id,Account.Id,RP_Customer_Engagement__c,IsWon FROM Opportunity WHERE Account.Id =: acct.Id AND IsWon=FALSE]){ L.RP_Customer_Engagement__c = acct.Tasks.size(); oppsUpdatable.add(L); } } if(oppsUpdatable.size()>0){ update oppsUpdatable; } } } }
Test Method:
static testmethod void testRenewalTasks3(){ Account acct1 = TestCreateRecords.createAcct(0); insert acct1; Opportunity opp1 = TestCreateRecords.createOppNew(acct1.Id); insert opp1; opp1.RP_Customer_Engagement__c = 0; UPDATE opp1; Task task1 = TestCreateRecords.createTaskInitProg(acct1.Id); INSERT task1; Task task2 = new task(); task2.WhatId = acct1.Id; task2.ActivityDate = Date.today().addDays(10); task2.Subject = 'User Training Complete'; task2.Status = 'In Progress'; task2.Priority = 'Normal'; INSERT task2; Test.startTest(); Task tsk1 = [SELECT Id,ActivityDate,Status FROM Task WHERE Id =: task1.Id]; tsk1.Status = 'In Progress'; UPDATE tsk1; Task tsk2 = [SELECT Id,ActivityDate,Status FROM Task WHERE Id =: tsk1.Id]; tsk2.Status = 'Completed'; UPDATE tsk2; Test.stopTest(); }
- John Neilan
- January 29, 2015
- Like
- 0
Increase Test Coverage
Hello,
I have a trigger handler class below that fires on a Task event and updates custom checkbox fields on the Opportunity object based on a few variables in the Task record. The trigger fires as appropriate, however, I am having difficulty getting coverage on lines 39 - 50 with the test class I have below (specifically lines 58 - 75 to cover that portion of the trigger). Can anyone give me guidance on how I can cover these lines?
Handler Class:
Test Class:
I have a trigger handler class below that fires on a Task event and updates custom checkbox fields on the Opportunity object based on a few variables in the Task record. The trigger fires as appropriate, however, I am having difficulty getting coverage on lines 39 - 50 with the test class I have below (specifically lines 58 - 75 to cover that portion of the trigger). Can anyone give me guidance on how I can cover these lines?
Handler Class:
public void renewalTasks(List<Task> checkTasks) { List<Opportunity> linkedOpps = new List<Opportunity>(); Map<Id, Task> taskMap = new Map<Id, Task>(); for (Task t: checkTasks) { if (t.WhatId != null && (t.Subject == 'User Training Complete' || t.Subject == 'Initial Program Confirmation' || t.Subject == 'Renewal Package Review')){ taskMap.put(t.WhatId, t); } } if (taskMap.size() > 0) { linkedOpps = [SELECT Account.Id, Effective_Date__c, Renewal_Date_Next__c, RP_User_Training__c, RP_Initial_Program_Developed__c, RP_Recommendation_Review_Call__c FROM Opportunity WHERE Account.Id IN: taskMap.keySet() AND IsClosed =: FALSE]; for (Opportunity c: linkedOpps){ for (Task taskAdd:[SELECT WhatId, Subject, Status, ActivityDate FROM Task WHERE WhatId =: c.Account.Id]){ IF(taskAdd.Status == 'Completed' && taskAdd.ActivityDate >= c.Effective_Date__c && taskAdd.ActivityDate <= c.Renewal_Date_Next__c){ IF(taskAdd.Subject == 'User Training Complete' && taskAdd.Status == 'Completed'){ c.RP_User_Training__c = TRUE; } IF(taskAdd.Subject == 'Initial Program Confirmation'){ c.RP_Initial_Program_Developed__c = TRUE; } IF(taskAdd.Subject == 'Renewal Package Review'){ c.RP_Recommendation_Review_Call__c = TRUE; } } ELSE IF((taskAdd.Status != 'Completed' && taskAdd.ActivityDate >= c.Effective_Date__c && taskAdd.ActivityDate <= c.Renewal_Date_Next__c) || (taskAdd.Status == 'Completed' && (taskAdd.ActivityDate < c.Effective_Date__c || taskAdd.ActivityDate > c.Renewal_Date_Next__c)) || (taskAdd.Status != 'Completed' && (taskAdd.ActivityDate < c.Effective_Date__c || taskAdd.ActivityDate > c.Renewal_Date_Next__c))){ IF(taskAdd.Subject == 'User Training Complete'){ c.RP_User_Training__c = FALSE; } IF(taskAdd.Subject == 'Initial Program Confirmation'){ c.RP_Initial_Program_Developed__c = FALSE; } IF(taskAdd.Subject == 'Renewal Package Review'){ c.RP_Recommendation_Review_Call__c = FALSE; } } } } if (linkedOpps.size() > 0) { update linkedOpps; } } }
Test Class:
@isTest public class TestRenewalTasks{ static testmethod void testRenewalTasks(){ Profile ProSys = [SELECT Id FROM Profile WHERE Name='Client Services (Admin)']; User U1 = new User(Alias = 'User1',Country='United States',Email='User1@testing.com',EmailEncodingKey='ISO-8859-1', LastName='User1', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = ProSys.Id,TimeZoneSidKey='America/New_York', UserName='User1@testing.com'); insert U1; Account acct1 = new Account(); acct1.Name = 'Test-JN'; acct1.Region__c = 'USA'; acct1.BillingCountry = 'USA'; acct1.Industry = 'Consumer Goods'; acct1.Status__c = 'Customer'; acct1.Website = 'www.test.com'; acct1.FB_Page_1_Fans__c = 500; acct1.FB_Page_1_Link__c = 'www.facebook.com/test'; acct1.OwnerId = U1.Id; INSERT acct1; Opportunity opp1 = new Opportunity(); opp1.Name = 'Test-JN Renewal Opp'; opp1.StageName = 'Phase 1: Planning & Training'; opp1.CloseDate = date.parse('1/1/15'); opp1.Renewal__c = 'Yes'; opp1.Effective_Date__c = date.parse('1/1/15'); opp1.Term__c = 12; opp1.AccountId = acct1.Id; INSERT opp1; Test.startTest(); List<Task> tasklist = new List<Task>(); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Initial Program Confirmation', Status = 'Completed', Priority = 'Normal')); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop User Training Complete', Status = 'Completed', Priority = 'Normal')); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Primary Goal Training Confirmation', Status = 'Completed', Priority = 'Normal')); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Initial Campaign Results', Status = 'Completed', Priority = 'Normal')); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Business Review', Status = 'Completed', Priority = 'Normal')); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Roadmap Review', Status = 'Completed', Priority = 'Normal')); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Manager Introduction', Status = 'Completed', Priority = 'Normal')); taskList.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Renewal Package Review', Status = 'Completed', Priority = 'Normal')); INSERT taskList; DELETE taskList; List<Task> tasklist2 = new List<Task>(); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Initial Program Confirmation', Status = 'In Progress', Priority = 'Normal')); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop User Training Complete', Status = 'In Progress', Priority = 'Normal')); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Primary Goal Training Confirmation', Status = 'In Progress', Priority = 'Normal')); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Initial Campaign Results', Status = 'In Progress', Priority = 'Normal')); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Business Review', Status = 'In Progress', Priority = 'Normal')); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Roadmap Review', Status = 'In Progress', Priority = 'Normal')); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Manager Introduction', Status = 'In Progress', Priority = 'Normal')); taskList2.add(new Task(WhatId = acct1.Id, ActivityDate = date.parse('1/5/15'), Subject = 'Offerpop Renewal Package Review', Status = 'In Progress', Priority = 'Normal')); INSERT taskList2; Test.stopTest(); } }
- John Neilan
- January 27, 2015
- Like
- 0
Dataloader Insert Not Firing Trigger
Hello,
I have an after trigger that fires when a task is inserted, updated, deleted, or undeleted. I added a number of tasks via the dataloader and the trigger did not fire. When I went into one of the tasks and just clicked edit/save, the trigger fired. Does anyone know how I can get it to fire when the dataloader is used?
Trigger:
Class:
I have an after trigger that fires when a task is inserted, updated, deleted, or undeleted. I added a number of tasks via the dataloader and the trigger did not fire. When I went into one of the tasks and just clicked edit/save, the trigger fired. Does anyone know how I can get it to fire when the dataloader is used?
Trigger:
trigger MainTriggerTask on Task (after insert, after update, after delete, before delete, after undelete) { IF(Trigger.IsAfter){ IF(Trigger.IsInsert){ ClassRenewalTasks updater6 = new ClassRenewalTasks(); updater6.renewalTasks(Trigger.new); } IF(Trigger.IsUpdate){ ClassRenewalTasks updater3 = new ClassRenewalTasks(); updater3.renewalTasks(Trigger.new); } } ELSE IF(Trigger.IsBefore){ IF(Trigger.IsDelete){ ClassRenewalTasks deleteTsk = new ClassRenewalTasks(); deleteTsk.deleteTasks(Trigger.old); } } }
Class:
public class ClassRenewalTasks { public void deleteTasks(List<Task> delTasks) { List<Opportunity> linkedOpps = new List<Opportunity>(); Map<Id, Task> taskMap = new Map<Id, Task>(); for (Task t: delTasks) { taskMap.put(t.WhatId, t); System.Debug('******** TaskMapDel ' + TaskMap); } if (taskMap.size() > 0) { linkedOpps = [SELECT Id,Account.Id, Effective_Date__c, Renewal_Date_Next__c, RP_User_Training__c, RP_Initial_Program_Developed__c, RP_Recommendation_Review_Call__c FROM Opportunity WHERE Account.Id IN: taskMap.keySet() AND IsClosed =: FALSE]; for (Opportunity opp1: linkedOpps){ for (Task delTask:[SELECT WhatId, Subject, Status, ActivityDate FROM Task WHERE WhatId =: opp1.Account.Id AND Status =: 'Completed' AND ActivityDate >=: opp1.Effective_Date__c AND ActivityDate <=: opp1.Renewal_Date_Next__c]){ IF(delTask.Subject == 'Initial Program Confirmation'){ opp1.RP_Initial_Program_Developed__c = FALSE; } IF(delTask.Subject == 'Renewal Package Review'){ opp1.RP_Recommendation_Review_Call__c = FALSE; } } } } // if the list of cons isnt empty, update them system.debug('linkedOpps = '+linkedOpps); if (linkedOpps.size() > 0) { update linkedOpps; } } public void renewalTasks(List<Task> checkTasks) { // set up lists you will need List<Opportunity> linkedOpps = new List<Opportunity>(); Map<Id, Task> taskMap = new Map<Id, Task>(); // go through the list of tasks that were inserted for (Task t: checkTasks) { // if they are related to a contact, add the contact id (whoID) and their values to a map if (t.WhatId != null && (t.Subject == 'Initial Program Confirmation' || t.Subject == 'Renewal Package Review')){ taskMap.put(t.WhatId, t); } } system.debug('taskMap = '+taskMap); if (taskMap.size() > 0) { // get all of the contacts related to the tasks linkedOpps = [SELECT Account.Id, Effective_Date__c, Renewal_Date_Next__c, RP_User_Training__c, RP_Initial_Program_Developed__c, RP_Recommendation_Review_Call__c FROM Opportunity WHERE Account.Id IN: taskMap.keySet() AND IsClosed =: FALSE]; // go through the list for each contact for (Opportunity c: linkedOpps){ for (Task task2:[SELECT WhatId, Subject, Status, ActivityDate FROM Task WHERE WhatId =: c.Account.Id AND ActivityDate >=: c.Effective_Date__c AND ActivityDate <=: c.Renewal_Date_Next__c]){ IF(task2.Subject == 'Initial Program Confirmation' && task2.Status == 'Completed'){ c.RP_Initial_Program_Developed__c = TRUE; } IF(task2.Subject == 'Renewal Package Review' && task2.Status == 'Completed'){ c.RP_Recommendation_Review_Call__c = TRUE; } IF(task2.Subject == 'Initial Program Confirmation' && task2.Status <> 'Completed'){ c.RP_Initial_Program_Developed__c = FALSE; } IF(task2.Subject == 'Renewal Package Review' && task2.Status <> 'Completed'){ c.RP_Recommendation_Review_Call__c = FALSE; } } } // if the list of cons isnt empty, update them system.debug('linkedOpps = '+linkedOpps); if (linkedOpps.size() > 0) { update linkedOpps; } } } }
- John Neilan
- January 13, 2015
- Like
- 0
Activity Count on Opportunity
Hello,
The trigger below fires when a Task is Inserted, Updated, or Deleted. It looks at the Account the Task is associated with and matches it to any Open Opportunities associated to the same Account. It then takes the count of tasks and writes it to a custom field on the Opportunity. All works fine when tasks are inserted, deleted, or undeleted. However, when I move a task from a Status of Closed to something else, the count should decrease, however, it does not. I don't see any reason why it would not decrease. Can anyone help me figure it out? Thanks.
Trigger:
Class:
The trigger below fires when a Task is Inserted, Updated, or Deleted. It looks at the Account the Task is associated with and matches it to any Open Opportunities associated to the same Account. It then takes the count of tasks and writes it to a custom field on the Opportunity. All works fine when tasks are inserted, deleted, or undeleted. However, when I move a task from a Status of Closed to something else, the count should decrease, however, it does not. I don't see any reason why it would not decrease. Can anyone help me figure it out? Thanks.
Trigger:
trigger MainTriggerTask on Task (after insert, after update, after delete, before delete, after undelete) { IF(Trigger.IsAfter){ IF(Trigger.IsInsert){ ClassRenewalTaskCount updater7 = new ClassRenewalTaskCount(); updater7.taskCountInsert(Trigger.new); } } IF(Trigger.IsUpdate){ ClassRenewalTaskCount updater4 = new ClassRenewalTaskCount(); updater4.taskCountUpdate(Trigger.new,Trigger.old); } IF(Trigger.IsDelete){ ClassRenewalTaskCount updater2 = new ClassRenewalTaskCount(); updater2.taskCountDel(Trigger.old); } IF(Trigger.IsUndelete){ ClassRenewalTaskCount updater2 = new ClassRenewalTaskCount(); updater2.taskCountInsert(Trigger.new); } } }
Class:
public class ClassRenewalTaskCount { Set<ID> OppIds = new Set<ID>(); String acctPrefix = Account.SObjectType.getDescribe().getKeyPrefix(); public void taskCountInsert(List<Task> checkTasks) { for (Task t : checkTasks) { if (t.WhatId != null && t.Status == 'Completed' && t.RP_Prior_60__c <= t.ActivityDate && string.valueof(t.WhatId).startsWith(acctPrefix) ) { OppIds.add(t.WhatId); } } if (OppIds.size() > 0){ List<Account> acctsWithTasks = [SELECT Id,(SELECT Id FROM Tasks) FROM Account WHERE Id IN : OppIds]; List<Opportunity> oppsUpdatable = new List<Opportunity>(); for(Account acct : acctsWithTasks){ for(Opportunity L : [SELECT Id,Account.Id,RP_Customer_Engagement__c,IsWon FROM Opportunity WHERE Account.Id =: acct.Id AND IsWon=FALSE]){ L.RP_Customer_Engagement__c = acct.Tasks.size(); oppsUpdatable.add(L); } } if(oppsUpdatable.size()>0){ update oppsUpdatable; } } } public void taskCountUpdate(List<Task> checkTasks, List<Task> oldTasks) { for (Task t : checkTasks) { for (Task prev : oldTasks){ if (t.WhatId != null && t.Status == 'Completed' && t.RP_Prior_60__c <= t.ActivityDate && string.valueof(t.WhatId).startsWith(acctPrefix) ) { OppIds.add(t.WhatId); } } } if (OppIds.size() > 0){ List<Account> acctsWithTasks = [SELECT Id,(SELECT Id FROM Tasks) FROM Account WHERE Id IN : OppIds]; List<Opportunity> oppsUpdatable = new List<Opportunity>(); for(Account acct : acctsWithTasks){ for(Opportunity L : [SELECT Id,Account.Id,RP_Customer_Engagement__c,IsWon FROM Opportunity WHERE Account.Id =: acct.Id AND IsWon=FALSE]){ L.RP_Customer_Engagement__c = acct.Tasks.size(); oppsUpdatable.add(L); } } if(oppsUpdatable.size()>0){ update oppsUpdatable; } } } public void taskCountDel(List<Task> delTasks) { for (Task t2 : delTasks) { if (t2.WhatId!= null && t2.Status == 'Completed' && t2.RP_Prior_60__c <= t2.ActivityDate && string.valueof(t2.WhatId).startsWith(acctPrefix)){ OppIds.add(t2.WhatId); } } if (OppIds.size() > 0){ List<Account> acctsWithTasks = [SELECT Id,(SELECT Id FROM Tasks) FROM Account WHERE Id IN : OppIds]; List<Opportunity> oppsUpdatable = new List<Opportunity>(); for(Account acct : acctsWithTasks){ for(Opportunity L : [SELECT Id,Account.Id,RP_Customer_Engagement__c,IsWon FROM Opportunity WHERE Account.Id =: acct.Id AND IsWon=FALSE]){ L.RP_Customer_Engagement__c = acct.Tasks.size(); oppsUpdatable.add(L); } } if(oppsUpdatable.size()>0){ update oppsUpdatable; } } } }
- John Neilan
- January 12, 2015
- Like
- 0
Update Opportunity When Task Created on Account
Hello,
I have a trigger that is meant to update custom field on an Opportunity when specific tasks are created on an Account that is also linked to the Opportunity. The trigger below saves and does not give any errors, however, nothing happens when a task is created. Can anyone help me figure out why? Thanks.
Trigger:
Class:
I have a trigger that is meant to update custom field on an Opportunity when specific tasks are created on an Account that is also linked to the Opportunity. The trigger below saves and does not give any errors, however, nothing happens when a task is created. Can anyone help me figure out why? Thanks.
Trigger:
trigger MainTriggerTask on Task (after insert, after update, before delete) { IF(Trigger.IsAfter){ IF(Trigger.IsInsert){ ClassRenewalTasks updater6 = new ClassRenewalTasks(); updater6.renewalTasks(Trigger.new); } IF(Trigger.IsUpdate){ ClassRenewalTasks updater3 = new ClassRenewalTasks(); updater3.renewalTasks(Trigger.new); } } ELSE IF(Trigger.IsBefore){ IF(Trigger.IsDelete){ ClassRenewalTasks deleteTsk = new ClassRenewalTasks(); deleteTsk.deleteTasks(Trigger.old); } } }
Class:
public class ClassRenewalTasks { public void deleteTasks(List<Task> delTasks) { List<Opportunity> linkedOpps = new List<Opportunity>(); Map<Id, Task> taskMap = new Map<Id, Task>(); for (Task t: delTasks) { taskMap.put(t.WhatId, t); } if (taskMap.size() > 0) { linkedOpps = [SELECT Id,Account.Id, Effective_Date__c, Next_Renewal_Date__c, RP_User_Training__c, RP_Initial_Program_Developed__c FROM Opportunity WHERE Account.Id IN: taskMap.keySet()]; for (Opportunity opp1: linkedOpps){ for (Task delTask:[SELECT WhatId, Subject, Status, ActivityDate FROM Task WHERE WhatId =: opp1.Account.Id AND Subject =: 'Completed' AND ActivityDate >=: opp1.Effective_Date__c AND ActivityDate <=: opp1.Next_Renewal_Date__c]){ IF(delTask.Subject == 'User Training Complete'){ opp1.RP_User_Training__c = FALSE; } IF(delTask.Subject == 'Initial Program Confirmation'){ opp1.RP_Initial_Program_Developed__c = FALSE; } } } } // if the list of cons isnt empty, update them system.debug('linkedOpps = '+linkedOpps); if (linkedOpps.size() > 0) { update linkedOpps; } } public void renewalTasks(List<Task> checkTasks) { // set up lists you will need List<Opportunity> linkedOpps = new List<Opportunity>(); Map<Id, Task> taskMap = new Map<Id, Task>(); // go through the list of tasks that were inserted for (Task t: checkTasks) { // if they are related to a contact, add the contact id (whoID) and their values to a map if (t.WhatId != null && (t.Subject == 'User Training Complete' || t.Subject == 'Initial Program Confirmation')){ taskMap.put(t.WhatId, t); } } // if the map isnt empty // *** saying !taskMap.isEmpty() costs much less than using taskMap.size()>0 *** system.debug('taskMap = '+taskMap); if (taskMap.size() > 0) { // get all of the contacts related to the tasks linkedOpps = [SELECT Account.Id, Effective_Date__c, Next_Renewal_Date__c, RP_User_Training__c, RP_Initial_Program_Developed__c FROM Opportunity WHERE Account.Id IN: taskMap.keySet()]; // go through the list for each contact for (Opportunity c: linkedOpps){ for (Task task2:[SELECT WhatId, Subject, Status, ActivityDate FROM Task WHERE WhatId =: c.Account.Id AND ActivityDate >=: c.Effective_Date__c AND ActivityDate <=: c.Next_Renewal_Date__c]){ IF(task2.Subject == 'User Training Complete' && task2.Status == 'Completed'){ c.RP_User_Training__c = TRUE; } IF(task2.Subject == 'Initial Program Confirmation' && task2.Status == 'Completed'){ c.RP_Initial_Program_Developed__c = TRUE; } IF(task2.Subject == 'User Training Complete' && task2.Status <> 'Completed'){ c.RP_User_Training__c = FALSE; } IF(task2.Subject == 'Initial Program Confirmation' && task2.Status <> 'Completed'){ c.RP_Initial_Program_Developed__c = FALSE; } } } // if the list of cons isnt empty, update them system.debug('linkedOpps = '+linkedOpps); if (linkedOpps.size() > 0) { update linkedOpps; } } } }
- John Neilan
- January 08, 2015
- Like
- 0
Illegal assignment from SObject to SOBJECT:Task
Hello,
I created a trigger that calls a class to count the number of tasks related to an Opportunity, who's Status is "Completed". The trigger works properly when tasks are inserted, moved to Completed, deleted, or un-deleted. However, nothing happens if a task is moved from Completed to another status and I would like the count to be decreased when this occurs. Can anyone help me figure this out? Thanks,
Trigger:
Class:
I created a trigger that calls a class to count the number of tasks related to an Opportunity, who's Status is "Completed". The trigger works properly when tasks are inserted, moved to Completed, deleted, or un-deleted. However, nothing happens if a task is moved from Completed to another status and I would like the count to be decreased when this occurs. Can anyone help me figure this out? Thanks,
Trigger:
trigger MainTriggerTask on Task (after insert, after update, after delete, after undelete) { IF(Trigger.IsInsert){ ClassRenewalTaskCount updater7 = new ClassRenewalTaskCount(); updater7.taskCountInsert(Trigger.new); } IF(Trigger.IsUpdate){ ClassRenewalTaskCount updater4 = new ClassRenewalTaskCount(); updater4.taskCountUpdate(Trigger.new,Trigger.old); } IF(Trigger.IsDelete){ ClassRenewalTaskCount updater2 = new ClassRenewalTaskCount(); updater2.taskCountDel(Trigger.old); } IF(Trigger.IsUndelete){ ClassRenewalTaskCount updater2 = new ClassRenewalTaskCount(); updater2.taskCountUpdate(Trigger.new, Trigger.old); } }
Class:
public class ClassRenewalTaskCount { Set<ID> OppIds = new Set<ID>(); String oppPrefix = Opportunity.SObjectType.getDescribe().getKeyPrefix(); public void taskCountInsert(List<Task> checkTasks) { for (Task t : checkTasks) { if (t.WhatId != null && t.Status == 'Completed' && t.RP_Prior_60__c <= t.ActivityDate && string.valueof(t.WhatId).startsWith(oppPrefix) ) { OppIds.add(t.WhatId); } } if (OppIds.size() > 0){ List<Opportunity> oppsWithTasks = [SELECT Id,RP_Customer_Engagement__c,(SELECT Id FROM Tasks) FROM Opportunity WHERE Id IN : OppIds]; List<Opportunity> oppsUpdatable = new List<Opportunity>(); for(Opportunity L : oppsWithTasks){ L.RP_Customer_Engagement__c = L.Tasks.size(); oppsUpdatable.add(L); } if(oppsUpdatable.size()>0){ update oppsUpdatable; } } } public void taskCountUpdate(List<Task> checkTasks, List<Task> oldTasks) { for (Task t : checkTasks) { for (Task prev : oldTasks){ if (t.WhatId != null && t.Status == 'Completed' && t.RP_Prior_60__c <= t.ActivityDate && string.valueof(t.WhatId).startsWith(oppPrefix) ) { OppIds.add(t.WhatId); } if (t.WhatId != null && ((prev.Status == 'Completed' && t.Status <> 'Completed') || t.RP_Prior_60__c > t.ActivityDate) && string.valueof(t.WhatId).startsWith(oppPrefix) ) { OppIds.remove(t.WhatId); } } } if (OppIds.size() > 0){ List<Opportunity> oppsWithTasks = [SELECT Id,RP_Customer_Engagement__c,(SELECT Id FROM Tasks) FROM Opportunity WHERE Id IN : OppIds]; List<Opportunity> oppsUpdatable = new List<Opportunity>(); for(Opportunity L : oppsWithTasks){ L.RP_Customer_Engagement__c = L.Tasks.size(); oppsUpdatable.add(L); } if(oppsUpdatable.size()>0){ update oppsUpdatable; } } } public void taskCountDel(List<Task> delTasks) { for (Task t2 : delTasks) { for (Task prev2 : (List<Task>)Trigger.Old){ if (t2.WhatId!= null && (t2.Status == 'Completed' || (prev2.Status == 'Completed' && t2.Status != 'Completed') || t2.RP_Prior_60__c > t2.ActivityDate) && string.valueof(t2.WhatId).startsWith(oppPrefix)){ OppIds.add(t2.WhatId); } } } if (OppIds.size() > 0){ List<Opportunity> oppsWithTasks = [SELECT Id,RP_Customer_Engagement__c,(SELECT Id FROM Tasks) FROM Opportunity WHERE Id IN : OppIds]; List<Opportunity> oppsUpdatable = new List<Opportunity>(); for(Opportunity L : oppsWithTasks){ L.RP_Customer_Engagement__c = L.Tasks.size(); oppsUpdatable.add(L); } if(oppsUpdatable.size()>0){ update oppsUpdatable; //update all the leads with activity count } } } }
- John Neilan
- January 08, 2015
- Like
- 0
VF Controller Error
Hello,
I have a controller below on a VF page off the Opportunity object. All I am trying to do is pre-populate a custom field (RENEW_Date_Updated__c) with today's date. When I try to save my VF page, I get the error below. Does anyone know how I can fix this? Thanks.
System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
Error is in expression '{!save}' in component <apex:commandButton> in page renewprogupdate: Class.RenewalOppProg.save: line 18, column 1
Class.RenewalOppProg.save: line 18, column 1
I have a controller below on a VF page off the Opportunity object. All I am trying to do is pre-populate a custom field (RENEW_Date_Updated__c) with today's date. When I try to save my VF page, I get the error below. Does anyone know how I can fix this? Thanks.
System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
Error is in expression '{!save}' in component <apex:commandButton> in page renewprogupdate: Class.RenewalOppProg.save: line 18, column 1
Class.RenewalOppProg.save: line 18, column 1
public class RenewalOppProg{ public List<Opportunity> opps {get; set;} private final Opportunity oppty; public RenewalOppProg(ApexPages.StandardController myController) { opps = new List<Opportunity>(); oppty = (Opportunity)myController.getrecord(); } public Opportunity opp2 = new Opportunity(); public void OppProg(){ opp2.RENEW_Date_Updated__c = Date.Today(); opps.add(opp2); } public PageReference save() { update opps; { PageReference RetPage = new PageReference('/apex/RenewProgView?id=' + opps[0].id); RetPage.setRedirect(true); return RetPage; } } }
- John Neilan
- December 23, 2014
- Like
- 0