function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mony Gueorguiev 11Mony 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!
Aakanksha SinghAakanksha Singh
Hello,

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
Mony Gueorguiev 11Mony Gueorguiev 11
Hey Akanksha!

Thank you! I will try using this as my test class for my trigger. I'll give you an update.
Mony Gueorguiev 11Mony Gueorguiev 11
I get this error message

Error: Compile Error: expecting a colon, found 'prop.id'
Mony Gueorguiev 11Mony Gueorguiev 11
please help!
Aakanksha SinghAakanksha Singh
write the query as [select Primary_Old__c,id from PPS_Proposal__c where id = : prop.id];
Aakanksha SinghAakanksha Singh
The colon after where clause states an expression.
Aakanksha SinghAakanksha Singh
If your problem is solved then please mark it as solved.