function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Marco Pollastri 1Marco 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


 
Best Answer chosen by Marco Pollastri 1
CharuDuttCharuDutt
HIi Marco
You've Entered Single ' ' sign Instead Of " == " Sign
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.');
       }
   }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Marco
Try Below Code
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.');
       }
   }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
ShivankurShivankur (Salesforce Developers) 
Hi Marco,

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.
Marco Pollastri 1Marco Pollastri 1
Hi @CharuDutt,
This is the error message i get now 
Thanks 

Error message
CharuDuttCharuDutt
HIi Marco
You've Entered Single ' ' sign Instead Of " == " Sign
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.');
       }
   }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
Marco Pollastri 1Marco Pollastri 1

Hi Shivankur,

Yes the field exists on the Opp Object. Already exist a Validation rule in the Type field.
 
ShivankurShivankur (Salesforce Developers) 
Hi Marco,

Thanks for clarifying,

Following type of code should work for you where double assignment operator (==) is used :
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.');
       }
   }
}
Hope above information helps. Please mark as Best Answer so that it can help others in future.

Thanks.
Marco Pollastri 1Marco Pollastri 1
Hi everyone, I think the most complete question was from ChauDutt, thanks a million Shivankur. 

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.');
       }
   }
}