-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
7Questions
-
7Replies
How to take Query out of Loop - particular instance
Hi all-
Beginning developer that has been running my head into the wall with this trigger for a while now. I feel like I am missing something obvious but I don't know where it is.
I have reciently implemented Campaign Influence for my organization. (for those of you who have not used Campaign Influence - it is related to the Opportunity and Contact Roles. When a contact role is related to an opportunity, the Campaign influence will pull in the Campaigns related to the contact so they are automatically associated to the campaign.)
I have a requirement that all Campaigns related to a contact must be split equally. I've tried various ways to pull the Query of related campaign influences, but have found that when I attempt to do a bulk upload, the trigger will combine campaigns two different opps, then divide by the total.
(ie. Opp 1 has 3 related campaign influence and opp 2 has 3 related campaign influence. Instead of attributing 33% influence to each, I end up with 17%).
The below trigger works, but I understand that the trigger should not be in the loop and I would like to improve it.
Thank you!
trigger UpdateOppPrimaryCampaign on Opportunity (after update) { for(Opportunity o : trigger.new) { List<String> oppListId = new List<String>(); system.debug('First for loop runs'); oppListId.add(o.Id); system.debug('oppListId = '+oppListId); List<CampaignInfluence> relatedCampaignInfuence = [SELECT Id, CampaignId, Influence, OpportunityId FROM CampaignInfluence WHERE OpportunityId IN :oppListId]; List<CampaignInfluence> campaignInfluenceListUpdate = new List<CampaignInfluence>(); system.debug('relatedCampaignInfuence = '+relatedCampaignInfuence); for(campaignInfluence c : relatedCampaignInfuence){ Decimal numCampaign = relatedCampaignInfuence.size(); system.debug ('numCampaign = ' + numCampaign); Decimal percentCampaign = 100 / numCampaign; system.debug ('percentCampaign = '+ percentCampaign); c.Influence = percentCampaign; system.debug('c.Influence = '+ c.Influence); c.Influenced__c = percentCampaign; campaignInfluenceListUpdate.add(c); system.debug('campaignInfluenceListUpdate = ' + campaignInfluenceListUpdate); } system.debug('update campaignInfluenceListUpdate EXECUTES'); Database.SaveResult[] results = Database.update(campaignInfluenceListUpdate, false); } }
-
- Upton_X
- April 19, 2021
- Like
- 0
- Continue reading or reply
Which value assigned on Put method with multiple values?
Hi all, I have a question and a followup question.
I have a query that potentially pulls multiple records:
//Line 25, 26, and 27 List<Territory__c> terrList = [SELECT Name, OwnerId FROM Territory__c WHERE Name IN :allZips]; system.debug('terrList = '+terrList)
After gathing the list, I run a loop to assign the Name and OwnerId to a a Map
// Lines 36,37,38,39,40 system.debug('terrList.size()' +terrList.size()); for(Territory__c t : terrList){ system.debug('for(Territory__c t : terrList) runs'); ziptoId.put(t.Name, t.OwnerId);} system.debug ('ziptoId.size() = '+ziptoId.size());It appears that the the code will use the first record in query and assign to the Map. Is this True?
If this is true, is there a way to assign a random value (of the queried values) to the map instead of first?
-
- Upton_X
- March 16, 2021
- Like
- 0
- Continue reading or reply
Writing a Trigger when Lead is Closed
Hello, I am writing a trigger with the following requirements:
Write a trigger that automatically sets Tasks to the “Completed” status whenever their associated Leads are set to any Closed status. Make sure your trigger is bulkified - there should be no SOQL queries inside any loops.
I have written the following trigger - it works well if the task is 'Closed - Not Converted' however when I convert the lead, the task is not closing. Can you provide some guidance please?
trigger SetTaskCompletedOnLeadClosed on Lead (after update) { //Create a set of ideas for no duplicates Set<Id> leadListIds = new Set<Id>(); for(Lead l : Trigger.new){ // Access the "old" record by its ID in Trigger.oldMap if(l.status == 'Closed - Converted' || l.status == 'Closed - Not Converted'){ leadListIds.add(l.id); system.debug('LeadListIds = '+leadListIds); } } List<Task> taskList = [SELECT id, subject, status, whoid FROM Task WHERE whoid in :leadListIds]; system.debug('taskList = '+taskList); List<Task> taskToUpdate = new List<Task>(); for(Task t : taskList){ if(t.status != 'Completed'){ t.status = 'Completed'; } taskToUpdate.add(t); system.debug('taskToUpdate = '+taskToUpdate); } if(taskToUpdate.size()>0){ update taskToUpdate; } }
-
- Upton_X
- February 27, 2021
- Like
- 0
- Continue reading or reply
No such column 'CreatedDated' on entity 'Case'.
I keep getting this error. Why?
List<Case> userCreatedCases = [SELECT Id, CreatedById FROM Case WHERE CreatedDated = 2005-10-08T01:02:03Z];
-
- Upton_X
- February 13, 2021
- Like
- 0
- Continue reading or reply
Last Day of Month for DATETIME method
I need datetime specifically because I can only query Case.CreatedDate with that datatype.
Line 8 Specifically
trigger ScratchTrigger on Case (before insert) { //Write a trigger that will prevent each user from creating more than 99 cases a month //Create values for this month Date dayToday = Date.Today(); DateTime firstDayOfMonth = DateTime.newInstance(daytoday.toStartOfMonth().addMonths(-1), Time.newInstance(0,0,0,0)); DateTime lastDayOfMonth = DateTime.newInstance(dayToday.addMonths(1).toStartofMonth().addDays(-1)), Time.newInstance(0,0,0,0)); system.debug('Todays Date = '+dayToday+ ' firstDayOfMonth = '+firstDayOfMonth+ ' lastday of Month = '+lastDayOfMonth); //When a new case is crated, iterate through Trigger.New and add case to UserCreated Cases if(Trigger.isInsert){ List<Case> UserCreatedCases = [SELECT Id, CreatedById, CreatedDate FROM Case WHERE (CreatedDated >= :firstDayOfMonth) AND (CreatedDate <= :lastDayOfMonth)]; } }
-
- Upton_X
- February 11, 2021
- Like
- 0
- Continue reading or reply
Test Class for VFP Controller
I’ve stumbled through writing the controller and visualforce page.
However, I am having trouble with achieving 75% code coverage with a test .
Here is my class
public class CombineContractAgreement { Id LegalEntityId; public CombineContractAgreement(ApexPages.StandardController controller){ LegalEntityId = ((SB_Legal_Entity__c)controller.getRecord()).Id; } public List<APXT_Redlining__Contract_Agreement__c>GetAllRelatedAgreement() { List <APXT_Redlining__Contract_Agreement__c> AllRelatedAgreements; AllRelatedAgreements = [SELECT Name, Agreement_Type__C, APXT_Redlining__Status__c, APXT_Redlining__Effective_Date__c, Agreement_Name__c FROM APXT_Redlining__Contract_Agreement__c WHERE Customer_Legal_Entity__c = :LegalEntityId OR Customer_Legal_Entity_2__c = :LegalEntityId OR Customer_Legal_Entity_3__c = :LegalEntityId OR Party_4__c = :LegalEntityId OR Party_5__c = :LegalEntityId]; return AllRelatedAgreements; } }
Visualforce
<apex:page StandardController="SB_Legal_Entity__c" extensions="CombineContractAgreement" lightningStylesheets="true"> <apex:pageBlock title="All Related Agreements"> <apex:pageBlockTable value="{!AllRelatedAgreement}" var="item"> <apex:column > <apex:outputLink value="https://hyclonehorizon--sb1.lightning.force.com/{!item.ID}">{!item.Name}</apex:outputLink> </apex:column> <apex:column value="{!item.Agreement_Name__c}" /> <apex:column value="{!item.Agreement_Type__c}" /> <apex:column value="{!item.APXT_Redlining__Status__c}" /> <apex:column value="{!item.APXT_Redlining__Effective_Date__c}" /> </apex:pageBlockTable> </apex:pageBlock> </apex:page>
Test Class
@isTest public class CombineContractAgreementTest { static testMethod void testMethod1(){ //Create required legal alias for Legal Entitiy Object Legal_Alias__c ali = new Legal_Alias__c(); ali.Name = 'NameLegalAlias'; system.debug('The Legal Alias name is '+ali.Name); insert ali; //Create Legal Entity SB_Legal_Entity__c testEntity = new SB_Legal_Entity__c(); testEntity.Legal_Alias__c = ali.Id; testEntity.Name = 'testLegalEntity'; insert testEntity; //Create Contract Agreement APXT_Redlining__Contract_Agreement__c testAgreement = new APXT_Redlining__Contract_Agreement__c(); testAgreement.Customer_Legal_Entity__c = testEntity.id; testAgreement.APXT_Redlining__Status__c = 'inactive'; testAgreement.APXT_Redlining__Effective_Date__c = date.today(); testagreement.Agreement_Name__c = 'TestAgreement'; insert testAgreement; System.debug('The Contract Agreements Legal Entity is' + testAgreement.Customer_Legal_Entity__c); Test.StartTest(); //current page equal the legal entity page ApexPages.StandardController stdctrl = new ApexPages.StandardController(testEntity); ApexPages.currentPage().getParameters().put('id',testEntity.Id); CombineContractAgreement testCombineAgree = new CombineContractAgreement(stdctrl); stdctrl.cancel(); Test.Stoptest(); } }
Result:
-
- Upton_X
- December 14, 2020
- Like
- 0
- Continue reading or reply
Combine Two Related List
Hello everyone,
Through searching various past threads it looks like this questions has been asked a few times, however I am hoping to get some specific help.
I've just last week started dedicating myself to learning to develop (SF99!) So I understand this is beyond my range, but this is a need at my current Org, and I thought it would be fun to actually try to do some development.
Note* I understand that a junction object could be an option here. Given the users that will be using these objects, the fewer clicks the better so I need to stick with this model.
We have a Contract Agreement Object that has 5 lookup fields for parties that can be related to the Contract Agreement. On the Party Object, I have 5 list. I would like to combine them into one list, and display them on the Party object. Here is what I have for code so far, I'm a newbie so I'm sure I am far off, but I'm excited to learn.
public class CombineContractAgreement { public CombineContractAgreement(ApexPages.StandardController controller) { } Public List <Contract_Agreement__C> AllRelatedAgreements; Public List<Contract_Agreement__c>GetAllRelatedAgreement() { AllRelatedAgreements = [SELECT id, RecordTypeId, APXT_Redlining_Status__c, APXT_Redlining__Effective_Date__c, Agreement_Name__c FROM Contract_Agreement__c WHERE //Current record is value on any 1 of 5 lookup fields on the Contract Agreement Record.// return AllRelatedAgreements }
Any help you can provide will be greatly apprchated.
-
- Upton_X
- October 08, 2020
- Like
- 0
- Continue reading or reply
Which value assigned on Put method with multiple values?
Hi all, I have a question and a followup question.
I have a query that potentially pulls multiple records:
//Line 25, 26, and 27 List<Territory__c> terrList = [SELECT Name, OwnerId FROM Territory__c WHERE Name IN :allZips]; system.debug('terrList = '+terrList)
After gathing the list, I run a loop to assign the Name and OwnerId to a a Map
// Lines 36,37,38,39,40 system.debug('terrList.size()' +terrList.size()); for(Territory__c t : terrList){ system.debug('for(Territory__c t : terrList) runs'); ziptoId.put(t.Name, t.OwnerId);} system.debug ('ziptoId.size() = '+ziptoId.size());It appears that the the code will use the first record in query and assign to the Map. Is this True?
If this is true, is there a way to assign a random value (of the queried values) to the map instead of first?
- Upton_X
- March 16, 2021
- Like
- 0
- Continue reading or reply
No such column 'CreatedDated' on entity 'Case'.
I keep getting this error. Why?
List<Case> userCreatedCases = [SELECT Id, CreatedById FROM Case WHERE CreatedDated = 2005-10-08T01:02:03Z];
- Upton_X
- February 13, 2021
- Like
- 0
- Continue reading or reply
Last Day of Month for DATETIME method
I need datetime specifically because I can only query Case.CreatedDate with that datatype.
Line 8 Specifically
trigger ScratchTrigger on Case (before insert) { //Write a trigger that will prevent each user from creating more than 99 cases a month //Create values for this month Date dayToday = Date.Today(); DateTime firstDayOfMonth = DateTime.newInstance(daytoday.toStartOfMonth().addMonths(-1), Time.newInstance(0,0,0,0)); DateTime lastDayOfMonth = DateTime.newInstance(dayToday.addMonths(1).toStartofMonth().addDays(-1)), Time.newInstance(0,0,0,0)); system.debug('Todays Date = '+dayToday+ ' firstDayOfMonth = '+firstDayOfMonth+ ' lastday of Month = '+lastDayOfMonth); //When a new case is crated, iterate through Trigger.New and add case to UserCreated Cases if(Trigger.isInsert){ List<Case> UserCreatedCases = [SELECT Id, CreatedById, CreatedDate FROM Case WHERE (CreatedDated >= :firstDayOfMonth) AND (CreatedDate <= :lastDayOfMonth)]; } }
- Upton_X
- February 11, 2021
- Like
- 0
- Continue reading or reply
Test Class for VFP Controller
I’ve stumbled through writing the controller and visualforce page.
However, I am having trouble with achieving 75% code coverage with a test .
Here is my class
public class CombineContractAgreement { Id LegalEntityId; public CombineContractAgreement(ApexPages.StandardController controller){ LegalEntityId = ((SB_Legal_Entity__c)controller.getRecord()).Id; } public List<APXT_Redlining__Contract_Agreement__c>GetAllRelatedAgreement() { List <APXT_Redlining__Contract_Agreement__c> AllRelatedAgreements; AllRelatedAgreements = [SELECT Name, Agreement_Type__C, APXT_Redlining__Status__c, APXT_Redlining__Effective_Date__c, Agreement_Name__c FROM APXT_Redlining__Contract_Agreement__c WHERE Customer_Legal_Entity__c = :LegalEntityId OR Customer_Legal_Entity_2__c = :LegalEntityId OR Customer_Legal_Entity_3__c = :LegalEntityId OR Party_4__c = :LegalEntityId OR Party_5__c = :LegalEntityId]; return AllRelatedAgreements; } }
Visualforce
<apex:page StandardController="SB_Legal_Entity__c" extensions="CombineContractAgreement" lightningStylesheets="true"> <apex:pageBlock title="All Related Agreements"> <apex:pageBlockTable value="{!AllRelatedAgreement}" var="item"> <apex:column > <apex:outputLink value="https://hyclonehorizon--sb1.lightning.force.com/{!item.ID}">{!item.Name}</apex:outputLink> </apex:column> <apex:column value="{!item.Agreement_Name__c}" /> <apex:column value="{!item.Agreement_Type__c}" /> <apex:column value="{!item.APXT_Redlining__Status__c}" /> <apex:column value="{!item.APXT_Redlining__Effective_Date__c}" /> </apex:pageBlockTable> </apex:pageBlock> </apex:page>
Test Class
@isTest public class CombineContractAgreementTest { static testMethod void testMethod1(){ //Create required legal alias for Legal Entitiy Object Legal_Alias__c ali = new Legal_Alias__c(); ali.Name = 'NameLegalAlias'; system.debug('The Legal Alias name is '+ali.Name); insert ali; //Create Legal Entity SB_Legal_Entity__c testEntity = new SB_Legal_Entity__c(); testEntity.Legal_Alias__c = ali.Id; testEntity.Name = 'testLegalEntity'; insert testEntity; //Create Contract Agreement APXT_Redlining__Contract_Agreement__c testAgreement = new APXT_Redlining__Contract_Agreement__c(); testAgreement.Customer_Legal_Entity__c = testEntity.id; testAgreement.APXT_Redlining__Status__c = 'inactive'; testAgreement.APXT_Redlining__Effective_Date__c = date.today(); testagreement.Agreement_Name__c = 'TestAgreement'; insert testAgreement; System.debug('The Contract Agreements Legal Entity is' + testAgreement.Customer_Legal_Entity__c); Test.StartTest(); //current page equal the legal entity page ApexPages.StandardController stdctrl = new ApexPages.StandardController(testEntity); ApexPages.currentPage().getParameters().put('id',testEntity.Id); CombineContractAgreement testCombineAgree = new CombineContractAgreement(stdctrl); stdctrl.cancel(); Test.Stoptest(); } }
Result:
- Upton_X
- December 14, 2020
- Like
- 0
- Continue reading or reply
Combine Two Related List
Hello everyone,
Through searching various past threads it looks like this questions has been asked a few times, however I am hoping to get some specific help.
I've just last week started dedicating myself to learning to develop (SF99!) So I understand this is beyond my range, but this is a need at my current Org, and I thought it would be fun to actually try to do some development.
Note* I understand that a junction object could be an option here. Given the users that will be using these objects, the fewer clicks the better so I need to stick with this model.
We have a Contract Agreement Object that has 5 lookup fields for parties that can be related to the Contract Agreement. On the Party Object, I have 5 list. I would like to combine them into one list, and display them on the Party object. Here is what I have for code so far, I'm a newbie so I'm sure I am far off, but I'm excited to learn.
public class CombineContractAgreement { public CombineContractAgreement(ApexPages.StandardController controller) { } Public List <Contract_Agreement__C> AllRelatedAgreements; Public List<Contract_Agreement__c>GetAllRelatedAgreement() { AllRelatedAgreements = [SELECT id, RecordTypeId, APXT_Redlining_Status__c, APXT_Redlining__Effective_Date__c, Agreement_Name__c FROM Contract_Agreement__c WHERE //Current record is value on any 1 of 5 lookup fields on the Contract Agreement Record.// return AllRelatedAgreements }
Any help you can provide will be greatly apprchated.
- Upton_X
- October 08, 2020
- Like
- 0
- Continue reading or reply