You need to sign in to do that
Don't have an account?
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.
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;
}
}
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:
Is that really the logic you are seeking?
Let us know how you go!
All Answers
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;
// update a;
if (a.X50__c != 100 ){
a.Probability = 50;
// update a;
if (a.X75__c != 100 ){
a.Probability = 70;
//update a;
}
}
}
}
}
}
}
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;
}
}
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:
Is that really the logic you are seeking?
Let us know how you go!
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;
}}}}}