0

I have two custom Object , Salesorder and SalesYear.SalesYear has two records - 2015 and 2016.SalesOrder Has a lookup to Salesyear.What i am trying to achieve is when i click on new salesorder , it should auto populate a value for salesyear i.r 2016

For this i have written a trigger, and it doesnot works for some reason which i am not aware of.

Can some one guide me in achieving this?

trigger SalesOrderTrigger on gii__SalesOrder__c (before insert, before update) {



    Sales_year__c objSales_year = new Sales_year__c();
    objSales_year = [select id from Sales_year__c where name = '2016'];
    for(gii__SalesOrder__c objOrder : trigger.new){
        objOrder.Sales_Year__c= objSales_year.id ;
    }
}
10
  • after saving the record Sales_Year__c field empty?
    – Ratan Paul
    Feb 29, 2016 at 11:23
  • 1
    Samir you need to change your approach. If you check the order of execution. You will find standard validation fire before trigger.. So first salesforce validation fire then after trigger fire.. that's the reason you are getting error.
    – Ratan Paul
    Feb 29, 2016 at 11:28
  • 1
    Samir.. I think you can use javascript button to populate this field.. Override your New button with custom javascript button and pass the SalesYear Id in Url and based on URL hacking populate this Id in UI.. Hope this will help you.
    – Ratan Paul
    Feb 29, 2016 at 12:01
  • 1
    This post will surely help you. raydehler.com/cloud/clod/… Feb 29, 2016 at 12:22
  • 1
    hard coding year = 2016 is going to be an issue when it is 2017
    – cropredy
    Feb 29, 2016 at 16:49

1 Answer 1

1

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;
    }
}
3
  • this was a standard detail page so i had to use the url hacking to make it work
    – Samir
    Mar 9, 2016 at 11:46
  • 1
    Oh, for a new record, you put the year field and the value? That's an easier solution than a trigger. Did you use the year value of today()? If not, you'll need to change it every calendar year.
    – crmprogdev
    Mar 9, 2016 at 14:28
  • it was just a small POC for Demo so didnt use today().But thanks ,it will surely help in other cases
    – Samir
    Mar 9, 2016 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.