You need to sign in to do that
Don't have an account?
Mony Gueorguiev 11
Test Class Help? New To Apex
Hey Guys,
So this is my trigger:
trigger PrimaryProposal on PPS_Proposal__c (before insert) {
Set<Id> oppIds = new Set<Id>();
//create an empty set to hold unique Opportunity Ids
//Loop through our incoming Proposals
for(PPS_Proposal__c newProp : trigger.new)
{
oppIds.add(newProp.Opportunity_Name__c);
//Add their Ids to our oppIds
}
//Retrieve any old proposals who have an Opportunity Id that matches one in that set and whose Primary field is checked
List<PPS_Proposal__c> oldProposals = new List<PPS_Proposal__c>([Select Id,Primary_Old__c
From PPS_Proposal__c
Where Primary_Old__c = true
AND Opportunity_Name__c in :oppIds]);
//Loop over our list of old proposals
for(PPS_Proposal__c oldProp : oldProposals)
{
oldProp.Primary_Old__c = false;
//change each of the old proposals primary fields to unchecked
}
update oldProposals;
}
//Loop through our incoming Proposals
for(PPS_Proposal__c newProp : trigger.new)
{
oppIds.add(newProp.Opportunity_Name__c);
//Add their Ids to our oppIds
}
//Retrieve any old proposals who have an Opportunity Id that matches one in that set and whose Primary field is checked
List<PPS_Proposal__c> oldProposals = new List<PPS_Proposal__c>([Select Id,Primary_Old__c
From PPS_Proposal__c
Where Primary_Old__c = true
AND Opportunity_Name__c in :oppIds]);
//Loop over our list of old proposals
for(PPS_Proposal__c oldProp : oldProposals)
{
oldProp.Primary_Old__c = false;
//change each of the old proposals primary fields to unchecked
}
update oldProposals;
Its my first time writing something of this size and while I'm glad its finally working in Sandbox, I need to push it to production, but really do not know how to write the test class for this, I understand what its supposed to be but have no idea how to create it.
I'd appreciate any help!
Thanks!
So this is my trigger:
trigger PrimaryProposal on PPS_Proposal__c (before insert) {
Set<Id> oppIds = new Set<Id>();
//create an empty set to hold unique Opportunity Ids
//Loop through our incoming Proposals
for(PPS_Proposal__c newProp : trigger.new)
{
oppIds.add(newProp.Opportunity_Name__c);
//Add their Ids to our oppIds
}
//Retrieve any old proposals who have an Opportunity Id that matches one in that set and whose Primary field is checked
List<PPS_Proposal__c> oldProposals = new List<PPS_Proposal__c>([Select Id,Primary_Old__c
From PPS_Proposal__c
Where Primary_Old__c = true
AND Opportunity_Name__c in :oppIds]);
//Loop over our list of old proposals
for(PPS_Proposal__c oldProp : oldProposals)
{
oldProp.Primary_Old__c = false;
//change each of the old proposals primary fields to unchecked
}
update oldProposals;
}
//Loop through our incoming Proposals
for(PPS_Proposal__c newProp : trigger.new)
{
oppIds.add(newProp.Opportunity_Name__c);
//Add their Ids to our oppIds
}
//Retrieve any old proposals who have an Opportunity Id that matches one in that set and whose Primary field is checked
List<PPS_Proposal__c> oldProposals = new List<PPS_Proposal__c>([Select Id,Primary_Old__c
From PPS_Proposal__c
Where Primary_Old__c = true
AND Opportunity_Name__c in :oppIds]);
//Loop over our list of old proposals
for(PPS_Proposal__c oldProp : oldProposals)
{
oldProp.Primary_Old__c = false;
//change each of the old proposals primary fields to unchecked
}
update oldProposals;
Its my first time writing something of this size and while I'm glad its finally working in Sandbox, I need to push it to production, but really do not know how to write the test class for this, I understand what its supposed to be but have no idea how to create it.
I'd appreciate any help!
Thanks!
You can't write test classes for trigger direct as you do for apex classes. The key to right test class for trigger is comparing the value before and after the SOQL operation. For ex: in this case-
@isTest
private class PrimaryProposalTest{
@isTest static void testmeth(){
PPS_Proposal__c prop = new PPS_Proposal__c(name = 'object name',Primary_Old__c='true');
system.debug('value of Primary_Old__c :'+prop.Primary_Old__c);
insert prop;
PPS_Proposal__c prop1 = [select Primary_Old__c,id from PPS_Proposal__c where id = prop.id];
system.debug('value of Primary_Old__c :'+prop1.Primary_Old__c);
system.assertequals('false',prop1.Primary_Old__c);
}
}
Thank You
Thank you! I will try using this as my test class for my trigger. I'll give you an update.
Error: Compile Error: expecting a colon, found 'prop.id'