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
Bogdan PascuBogdan Pascu 

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;
        }

    }
}

 
Best Answer chosen by Bogdan Pascu
SATHISH REDDY.SATHISH REDDY.
Here's the updated version
//trigger

trigger listingLocationUpdate on pba__Listing__c (before insert, before update) { 
    for (pba__Listing__c listing : [SELECT Id, Location__r.Name, fm_Location__c, Location__c FROM pba__Listing__c WHERE Id IN: Trigger.New]]) {
        if (!Trigger.isInsert) {
            if (Trigger.oldMap.get(listing.Id).Location__c == listing.Location__c) {
                continue;
            }
        }
        if (listing.Location__c == null) {
            listing.fm_Location__c = '';
        }else{
            listing.fm_Location__c = listing.Location__r.Name;
        }
    }
}


//Test class

@isTest
public class listingLocationUpdateTest{
    static testmethod void testListingLocationUpdate(){
        Test.Starttest();
        pba__Location__c pbaLocation = new pba__Location__c();
        pbaLocation.Name = 'Chicago';
        insert pbaLocation;

        pba__Listing__c pbaListing = new pba__Listing__c();
        pbaListing.Name = 'Listing Insert'; 
        pbaListing.Location__c = pbaLocation.Id;
        pbaListing.fm_Location__c = pbaLocation.Name;
        insert pbaListing;

        pbaListing.Name = 'Listing Update';
        update pbaListing;
        Test.Stoptest();   
    }
}

Please mark as the best ans if this helps. thanks!

All Answers

SATHISH REDDY.SATHISH REDDY.
Hi Bogdan,

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!
Bogdan PascuBogdan Pascu
Hello

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
Bogdan PascuBogdan Pascu
yes the trigger is working fine I don't had any problems so far. 
SATHISH REDDY.SATHISH REDDY.
Here you go. Let me know how it goes & please mark as the best answer if worked as expected. Thanks!
//trigger

trigger listingLocationUpdate on pba__Listing__c (before insert, before update) { 
    for (pba__Listing__c listing : [SELECT Id, Location__r.Name, fm_Location__c, Location__c FROM pba__Listing__c WHERE Id IN: Trigger.New]]) {
        if (!Trigger.isInsert) {
            if (Trigger.oldMap.get(listing.Id).Location__c == listing.Location__c) {
                continue;
            }
        }
        if (listing.Location__c == null) {
            listing.fm_Location__c = '';
        }else{
            listing.fm_Location__c = listing.Location__r.Name;
        }
    }
}


//Test class

@isTest
public class listingLocationUpdateTest{
    public static void testListingLocationUpdate(){
        Test.Start();
        pba__Location__c pbaLocation = new pba__Location__c();
        pbaLocation.Name = 'Chicago';
        insert pbaLocation;

        pba__Listing__c pbaListing = new pba__Listing__c();
        pbaListing.Name = 'Listing Insert'; 
        pbaListing.Location__c = pbaLocation.Id;
        pbaListing.fm_Location__c = pbaLocation.Name;
        insert pbaListing;

        pbaListing.Name = 'Listing Update';
        update pbaListing;
        Test.Stop();   
    }
}

 
Bogdan PascuBogdan Pascu
Hello

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.
Bogdan PascuBogdan Pascu
Hello I removed those test but when I run the test i get the same message: Add test methods to your test class.
SATHISH REDDY.SATHISH REDDY.
Here's the updated version
//trigger

trigger listingLocationUpdate on pba__Listing__c (before insert, before update) { 
    for (pba__Listing__c listing : [SELECT Id, Location__r.Name, fm_Location__c, Location__c FROM pba__Listing__c WHERE Id IN: Trigger.New]]) {
        if (!Trigger.isInsert) {
            if (Trigger.oldMap.get(listing.Id).Location__c == listing.Location__c) {
                continue;
            }
        }
        if (listing.Location__c == null) {
            listing.fm_Location__c = '';
        }else{
            listing.fm_Location__c = listing.Location__r.Name;
        }
    }
}


//Test class

@isTest
public class listingLocationUpdateTest{
    static testmethod void testListingLocationUpdate(){
        Test.Starttest();
        pba__Location__c pbaLocation = new pba__Location__c();
        pbaLocation.Name = 'Chicago';
        insert pbaLocation;

        pba__Listing__c pbaListing = new pba__Listing__c();
        pbaListing.Name = 'Listing Insert'; 
        pbaListing.Location__c = pbaLocation.Id;
        pbaListing.fm_Location__c = pbaLocation.Name;
        insert pbaListing;

        pbaListing.Name = 'Listing Update';
        update pbaListing;
        Test.Stoptest();   
    }
}

Please mark as the best ans if this helps. thanks!
This was selected as the best answer
Bogdan PascuBogdan Pascu
Many thanks for your help now it is working
SATHISH REDDY.SATHISH REDDY.
@bogdan - pls mark as the best answer so that others in the community can benefit from this. Thank you!