+ Start a Discussion
faceroy123faceroy123 

Trigger not executing

trigger volumes on case (before Insert, before Update) {
        for(case x : Trigger.New){        
                     

       if (x.region__c == 'South West'){   
           x.consignments_volumes__c = Volumes__c.getinstance('1. January 2012 SW').Id__c; 

       if (x.region__c == 'North West'){   
           x.consignments_volumes__c = Volumes__c.getinstance('2. February 2012').Id__c;
                            
} 
   }}}

Hi, relatively new to this so I appreciate any help!

 

Can anyone advise why this trigger does not work? it's referencing a custom setting.

 

If I remove the 'if' line, then it seems to work. Any ideas?

 

Thank you

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

all the System.debug does is prints the line.  You need to activate debug logs, then get your trigger to run.  Then you can look at the log of what happened and see what printed up there.

All Answers

Damien_Damien_

You need not correctly place your braces:

if (x.region__c == 'South West'){   
           x.consignments_volumes__c = Volumes__c.getinstance('1. January 2012 SW').Id__c; 

       if (x.region__c == 'North West'){   
           x.consignments_volumes__c = Volumes__c.getinstance('2. February 2012').Id__c;
                            
} 
   }

Your second if statement is contained INSIDE of your first, and so the following line:

x.consignments_volumes__c = Volumes__c.getinstance('2. February 2012').Id__c;

never gets executed because it is impossible for:

x.region__c == 'South West' && x.region__c == 'North West'

which is what your logic really states.

faceroy123faceroy123

Thanks Damien,

 

I think I understand, I have moved one of the braces but this still does not work,

trigger volumes on case (before Insert, before Update) {
        for(case x : Trigger.New){        
                     

if (x.region__c == 'South West'){   
           x.consignments_volumes__c = Volumes__c.getinstance('1. January 2012 SW').Id__c; }
       if (x.region__c == 'North West'){   
           x.consignments_volumes__c = Volumes__c.getinstance('2. February 2012').Id__c;
                            
} 
   }    

 where am I going wrong?

 

Thanks for your help

Damien_Damien_

 but this still does not work

What exactly doesn't work?

 

Try throwing a debug in to find out what the value of your region is to make sure the values are what you are expecting:

trigger volumes on case (before Insert, before Update)
{
  for(case x : Trigger.New)
  {        
    System.debug('x region = ' + x.Region__c);//Does it ever show up as 'South West' or 'North West'?
    if (x.region__c == 'South West')
    {   
      x.consignments_volumes__c = Volumes__c.getinstance('1. January 2012 SW').Id__c;
    }
    if (x.region__c == 'North West')
    {   
      x.consignments_volumes__c = Volumes__c.getinstance('2. February 2012').Id__c;
    } 
  }
}

 Try lining up your code better so it is more readable please.  (You don't have to line it up the same way I do, but keep each line correctly tabbed at least.  It makes it easier for someone else to help you debug your issue.)

faceroy123faceroy123

The lookup field 'consignments_volumes' does not populate.

 

However, this does work

trigger volumes on case (before Insert, before Update)
{
  for(case x : Trigger.New)
  { 
      x.consignments_volumes__c = Volumes__c.getinstance('1. January 2012 SW').Id__c;
    }
}

 When I add the 'if' criteria it doesn't update the field.

 

Thank you.

 

Damien_Damien_

Did you try it with the System.debug in there?  If so, what printed up?  Does it meet the criteria of those if statements?

faceroy123faceroy123

I'm not sure what to expect with System debug. It didn't work with that in there.

 

 

Damien_Damien_

all the System.debug does is prints the line.  You need to activate debug logs, then get your trigger to run.  Then you can look at the log of what happened and see what printed up there.

This was selected as the best answer
faceroy123faceroy123

Thank you,

 

I found that there are 2 region fields - region_2__c was what I was looking for! meh!!

 

Thanks again