You need to sign in to do that
Don't have an account?
Marco Pollastri 1
Trigger error message on Creation
Hi everyone,
I am trying to create a Tigger to display a simple error message.
I would like to block the user to create more than one Opp in a Supplier Account. So if the user is creating the second Opp related to a supplier account the System has to stop him to create the second Opp.
This is the trigger I have created:
trigger Error_Opp on Opportunity (before insert) {
for( Opportunity oppsupplier: trigger.new ){
if(oppsupplier.Opp_Count__c>=1 || oppsupplier.type= 'Supplier')
{
oppsupplier.addError('This is a Supplier Account, it can have only one Opportunity.');
}
}
}
The trigger works only with 'oppsupplier.Opp_Count__c>=1' condition, If i add thi condition ' || oppsupplier.type= 'Supplier' the system give me this error message:
'Error: Compile Error: Expression cannot be assigned at line 4 column 11'
How can I add an extra condition in the If?
Thanks
I am trying to create a Tigger to display a simple error message.
I would like to block the user to create more than one Opp in a Supplier Account. So if the user is creating the second Opp related to a supplier account the System has to stop him to create the second Opp.
This is the trigger I have created:
trigger Error_Opp on Opportunity (before insert) {
for( Opportunity oppsupplier: trigger.new ){
if(oppsupplier.Opp_Count__c>=1 || oppsupplier.type= 'Supplier')
{
oppsupplier.addError('This is a Supplier Account, it can have only one Opportunity.');
}
}
}
The trigger works only with 'oppsupplier.Opp_Count__c>=1' condition, If i add thi condition ' || oppsupplier.type= 'Supplier' the system give me this error message:
'Error: Compile Error: Expression cannot be assigned at line 4 column 11'
How can I add an extra condition in the If?
Thanks
You've Entered Single ' = ' sign Instead Of " == " Sign Please Mark It As Best Answer If It Helps
Thank You!
All Answers
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!
It seems the opportunity being checked through the for loop may not be having a value in the type fields.
Can you ensure if you have valid value in the 'type' field in opportunity object?
You may also want to add one more condition to check if the type fields is not blank or null.
Hope above information helps. Please mark as Best Answer so that it can help others in future.
Thanks.
This is the error message i get now
Thanks
You've Entered Single ' = ' sign Instead Of " == " Sign Please Mark It As Best Answer If It Helps
Thank You!
Hi Shivankur,
Yes the field exists on the Opp Object. Already exist a Validation rule in the Type field.
Thanks for clarifying,
Following type of code should work for you where double assignment operator (==) is used : Hope above information helps. Please mark as Best Answer so that it can help others in future.
Thanks.
This is what exactly I was looking for and is working.
---------------------------------------------------------------------------------
trigger Error_Opp on Opportunity (before insert) {
for( Opportunity oppsupplier: trigger.new ){
if(oppsupplier.Opp_Count__c == 1 && oppsupplier.type == 'Supplier')
{
oppsupplier.addError('This is a Supplier Account, it can have only one Opportunity.');
}
}
}