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
DannyTKDannyTK 

Expression cannot be assigned at line -1 column -1 error on Test Class

Good morning everyone,  

I have a trigger that will assign a lookup value from a custom object (data_center_location__c) to an Opportunity based on a picklist field (location__c) on the Opportunity.  The trigger works okay, but i'm having trouble with my test class.  I'm receiving the Expression error when trying to save.  I also have to state that i'm not a developer so I don't have a great understanding of basic concepts when it comes to test classes:

Trigger is:

trigger UpdateDataCenterLocation on Opportunity (before insert, before update){
      Set<String> locationSet = new Set<String>();
      Map<String,Id> mapLocationSetwithDataCntr = new Map<String,Id>();
      for (Opportunity obj: trigger.new){
          if(!String.isBlank(obj.Location__c)){
              locationSet.add(obj.Location__c);
          }
      }
      if(!locationSet.isEmpty()){
          for(Data_Center_Location__c dataCenter : [SELECT Id,Opportunity_Location_Code__c FROM Data_Center_Location__c WHERE Opportunity_Location_Code__c IN :locationSet]){
              mapLocationSetwithDataCntr.put(dataCenter.Opportunity_Location_Code__c,dataCenter.Id);//Expecting there will be one record for each location, otherwise this map will have last Data_Center_Location__c Id
          }
      }
      for (Opportunity obj: trigger.new){
          if(!String.isBlank(obj.Location__c) && mapLocationSetwithDataCntr != null && mapLocationSetwithDataCntr.get(obj.Location__c) != null){
                  obj.Data_Center_Location__c = mapLocationSetwithDataCntr.get(obj.Location__c);
        }
          else{
              obj.Data_Center_Location__c = null;
          }
      }
  }

________
Again, the trigger works as should, the test class on the other hand doesn't:

@isTest

    Private class UpdateDataCenterLocationTestClass {
        static testMethod void UpdateDataCenterLocation () {
    
        List <Opportunity> OpportunityLst = new List <Opportunity>();
        Opportunity Opp = new Opportunity ();
       Opportunity.Name = 'test Opportunity';
       Opportunity.Location__c = 'TX-DAL-03LEW' ;
       Opportunity.Stagename = 'Stage 1: Qualified Prospect';
       
       Opportunity.Closedate = '2015, 2, 26';
        OpportunityLst.add(opp);
        
        insert Opp;
    
    }
    }

--------------------------
For the test class, i'm just trying to create an Opportunity with the location__c field populated with a value, seems easy enough.  Any guidance would be much appreciated.

Thanks everyone
-d
Best Answer chosen by DannyTK
lrw757lrw757
Hey there! Your problem is that you are not referencing the variable you assigned to the instance of the opportunity. Instead of "Opportunity.Name", etc., it should be "opp.Name". As a side note, if you are adding the opp to a list to be inserted, you should insert the entire list. 

@isTest

    Private class UpdateDataCenterLocationTestClass {
        static testMethod void UpdateDataCenterLocation () {
    
        List <Opportunity> OpportunityLst = new List <Opportunity>();
        Opportunity Opp = new Opportunity ();
       Opp.Name = 'test Opportunity';
       Opp.Location__c = 'TX-DAL-03LEW' ;
       Opp.Stagename = 'Stage 1: Qualified Prospect';
       
       Opp.Closedate = '2015, 2, 26';
        OpportunityLst.add(opp);
        
        insert OpportunityList;
    
    }
    }

All Answers

lrw757lrw757
Hey there! Your problem is that you are not referencing the variable you assigned to the instance of the opportunity. Instead of "Opportunity.Name", etc., it should be "opp.Name". As a side note, if you are adding the opp to a list to be inserted, you should insert the entire list. 

@isTest

    Private class UpdateDataCenterLocationTestClass {
        static testMethod void UpdateDataCenterLocation () {
    
        List <Opportunity> OpportunityLst = new List <Opportunity>();
        Opportunity Opp = new Opportunity ();
       Opp.Name = 'test Opportunity';
       Opp.Location__c = 'TX-DAL-03LEW' ;
       Opp.Stagename = 'Stage 1: Qualified Prospect';
       
       Opp.Closedate = '2015, 2, 26';
        OpportunityLst.add(opp);
        
        insert OpportunityList;
    
    }
    }
This was selected as the best answer
DannyTKDannyTK
Thanks for your help lrw, there are some basics that I need to learn for sure.  Coverage looks good with above, really appreciate it.  

-d