+ Start a Discussion
DshorizonDshorizon 

Trigger Update , Insert Help

Hi, I am new to writing apex triggers and I'm currently running into a wall trying to find out why this isn't updating. 

 

Basically i have three formula fields as each one becomes 100 percent i would like to have the trigger update my probability field to that percentage. My code is currently throwing no errors but never updates the probability field. 

 

Here is my trigger 

 

trigger UpdateOpportunity on Opportunity (after update , after insert) {  
  for (Opportunity a : Trigger.new) {  
    if(a.Probability < 0.8 ){ 
        if (a.X25_Complete__c != 1 ) {
         
         if (a.X50__c != 1 ){
          
          if (a.X75__c != 1 ){
          a.Probability = .75;
          update a;
          }
         
         else
         a.Probability = .50;
         update a;
         }
        
        else
        a.Probability = .25;
        update a;
        }
      
       } 
  
   }
 }

 

Many thanks. 

Best Answer chosen by Admin (Salesforce Developers) 
jrotensteinjrotenstein

Hi there, let's see if I can be of assistance..

 

Firstly, I notice that you've created a "before update, before insert" trigger. However, you also have  an IF statement that only executes code if "trigger.isUpdate". If that's always the situation, you should make your trigger only "before update", without the "before insert".

 

Secondly, you don't need to call 'update' in a Trigger. Just change the value on the object and the system will take care of the update for you.  Oh, actually I see that you've commented out those lines, so you can just get rid of them anyway.

 

It's hard to know what's going wrong without having more information.

 

I'd suggest rewriting it to be a simple update just to make sure that your code is being executed, for example:

 

 

trigger UpdateOpportunity on Opportunity (before update) {  
  for (Opportunity a : Trigger.new) {
    a.Probability = 42;
    }
  }
}

 

trigger UpdateOpportunity on Opportunity (before update) {

  for (Opportunity a : Trigger.new) {

    a.Probability = 42;

  }

}

 

This should update all your probabilities to 42 when a record is updated (only do this on a test system!).

 

If that's working, have a think about what you're actually trying to accomplish. In reading your code, the logic seems to be as follows:

ProbabilityX25_Complete__cX50__cX75__cRESULTING PROBABILITY
85(Any value)(Any value)(Any value)85 (unchanged)
42100(Any value)(Any value)42 (unchanged)
42(Not 100)10010025
42(Not 100)(Not 100)10050
42(Not 100)(Not 100)(Not 100)75

 

Is that really the logic you are seeking?

 

Let us know how you go!

All Answers

DshorizonDshorizon

I think i found one issue, percents arn't treated as true values buy whole numbers. Also my Else statements were cutting off everything 

 

here is my new code Still does not behave correctly. :-/ 

 

 

trigger UpdateOpportunity on Opportunity (before update , before insert) {  
  for (Opportunity a : Trigger.new) {
  if (trigger.isUpdate){  
    if(a.Probability < 80 ){ 
        if (a.X25_Complete__c != 100 ) {
         a.Probability = 25;
        a.Probability_Per_del__c ='25%-Many competitors.Months before decision'; 
       // update a;
         if (a.X50__c != 100 ){
             a.Probability = 50;
         a.Probability_Per_del__c ='50%-Early phase, a few competitors in oppty.Decision within 1-2 months'; 
        // update a;
          if (a.X75__c != 100 ){
          a.Probability = 70;
          a.Probability_Per_del__c ='70%-Excellent chance,decision in 1-2 weeks'; 
          //update a;
          }
         }
        }
      } 
     }
   }
 }

trigger UpdateOpportunity on Opportunity (before update , before insert) {  

  for (Opportunity a : Trigger.new) {

  if (trigger.isUpdate){  

    if(a.Probability < 80 ){ 

        if (a.X25_Complete__c != 100 ) {

         a.Probability = 25;

 

       // update a;

         if (a.X50__c != 100 ){

             a.Probability = 50;

 

        // update a;

          if (a.X75__c != 100 ){

          a.Probability = 70;

 

          //update a;

          }

         }

        }

      } 

     }

   }

 }

 

jrotensteinjrotenstein

Hi there, let's see if I can be of assistance..

 

Firstly, I notice that you've created a "before update, before insert" trigger. However, you also have  an IF statement that only executes code if "trigger.isUpdate". If that's always the situation, you should make your trigger only "before update", without the "before insert".

 

Secondly, you don't need to call 'update' in a Trigger. Just change the value on the object and the system will take care of the update for you.  Oh, actually I see that you've commented out those lines, so you can just get rid of them anyway.

 

It's hard to know what's going wrong without having more information.

 

I'd suggest rewriting it to be a simple update just to make sure that your code is being executed, for example:

 

 

trigger UpdateOpportunity on Opportunity (before update) {  
  for (Opportunity a : Trigger.new) {
    a.Probability = 42;
    }
  }
}

 

trigger UpdateOpportunity on Opportunity (before update) {

  for (Opportunity a : Trigger.new) {

    a.Probability = 42;

  }

}

 

This should update all your probabilities to 42 when a record is updated (only do this on a test system!).

 

If that's working, have a think about what you're actually trying to accomplish. In reading your code, the logic seems to be as follows:

ProbabilityX25_Complete__cX50__cX75__cRESULTING PROBABILITY
85(Any value)(Any value)(Any value)85 (unchanged)
42100(Any value)(Any value)42 (unchanged)
42(Not 100)10010025
42(Not 100)(Not 100)10050
42(Not 100)(Not 100)(Not 100)75

 

Is that really the logic you are seeking?

 

Let us know how you go!

This was selected as the best answer
DshorizonDshorizon

Thank you very much for your reply. Your method worked perfectly for building this. 

 

here is the working version of the trigger behaving properly.

 

trigger UpdateOpportunity on Opportunity (before update) {
  for (Opportunity a : Trigger.new) {
  if (a.Probability < 80)
  if (a.X25_Complete__c > 0) {
              a.Probability = 25; 
               if (a.X50__c > 0) {
               a.Probability = 50; 
               if (a.X75__c > 0) {
               a.Probability = 75;
 }}}}}