You need to sign in to do that
Don't have an account?

Apex Test Class for a Trigger
I have the following trigger and I would need some help in creating the test class. Any help will be appreciated.
trigger listingLocationUpdate on pba__Listing__c (before insert, before update) { //Mantiene actualizado el picklist Location en base al lookup Location List<pba__Listing__c> listings = Trigger.new; Map<Id, pba__Listing__c> oldListings = null; if (!Trigger.isInsert) { oldListings = Trigger.oldMap; } for (pba__Listing__c listing : listings) { if (!Trigger.isInsert) { pba__Listing__c oldListing = oldListings.get(listing.Id); if (oldListing.Location__c == listing.Location__c) { continue; } } if (listing.Location__c == null) { listing.fm_Location__c = null; continue; } pba__Location__c l = [Select Name from pba__Location__c where Id =:listing.Location__c]; if (l != null) { listing.fm_Location__c = l.Name; } } }
Please mark as the best ans if this helps. thanks!
All Answers
Looks like your trigger doesn't follow the best practice where you wrote your SOQL query within for loop. I am unable to understand you logic here of what you are trying to accomplish. Please share the use case so that we can help you better.
Thanks!
I am not a developer so you are probably right about the trigger. The logic is the following:
I have this object pba__Listing__c where I have a lookup Location__c to this object: pba__Location__c
When I create a new pba__Listing__c I have to add the location via the lookup filed Location__c. So far so good.
I also have on the main object pba__Listing__c a picklist field fm_Location__c.
The logic of the trigger should be that whenever I create a new object and select the location lookup fileds the trigger should update the picklist field.
Example: I create a new object pba__Listing__c and I choose the location via the lookup filed Location__c let's say Chicago then my picklist field should update to Chicago fm_Location__c = Chicago
Many thanks for your help- I am getting this 2 errors on line 4 and 17: Method does not exist or incorrect signature: void Start() from the type System.Test
If I use the Test.StartTest and Test.StopTest I don't get any errors but then I cannot run the test as I get thie message: Add test methods to your test class.
Please mark as the best ans if this helps. thanks!