At a minimum, this trigger should have two sections. There should be a different section for BeforeInsert than BeforeUpdate. Why? Because in BeforeInsert, you won't have an order Id to work with unless you plan to work with a previous Id from the previous year. In a BeforeUpdate, you'll have an existing Id to work with.
Further, it appears to me that there's an issue with your query. You've not declared it for the object it's to return, nor as a list since you didn't put a limit 1 on the query.
Also, your trigger hasn't been "bulkified"; something we need to help you with by putting the query outside of any for loops. Here's what I'd suggest. You'll also notice that I've added debug statements so you check to see if your query is returning any results.
trigger SalesOrderTrigger on gii__SalesOrder__c (before insert, before update) {
Sales_year__c SalesYr = [select id from Sales_year__c where name = '2016' limit 1];
system.debug('SalesYrId = '+SalesYr.Id);
if (trigger isBefore & trigger.isInsert){
Sales_year__c objSales_year = new Sales_year__c();
// It appears you're planning to use the same id for every new record. If not, we
// need to collect the names of each record and get a new id for each of them
for(gii__SalesOrder__c objOrder : trigger.new){
objOrder.Sales_Year__c= SalesYr.Id ;
}
insert objOrder;
}
if(trigger.isBefore && trigger.isUpdate){
Sales_year__c objSales_year = new Sales_year__c();
// objSales_year = [select id from Sales_year__c where name = '2016'];
// we don't need to query again since we did this before the 1st if statement
// We first need to check and see if the record is from 2016 before doing anything
set<id>orders2update = new set<id>();
// let's compare using trigger.old to see if old order has 2016 Id
for(gii__SalesOrder__c objOrder : trigger.new){
if(trigger.old.get(objOrder.Id).Sales_Year__c != SalesYr.Id){
orders2update.add(objOrder.Id);
} else{
// order is from 2016 process normally
}
//If I understand your question correctly, we now have a set of Ids that need to
//have new sales year order Ids assigned to them with 2016 Ids.
for(Id oid:orders2update){
gii__SalesOrder__c objOrder = trigger.newmap.get(oid)
objOrder.Sales_Year__c= SalesYr.Id ;
}
update objOrder;
}
}
Sales_Year__c
field empty?