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
CushtyCushty 

Default Stage Name when creating opportunity

Hi,

I am new to triggers and struggling to create one.
I am trying to create a trigger which will default the Opportunity Stage Name to 'Prospecting'  whether the opportunity is created from scratch or from an Account.  I have tried this but nothing happens

Simple apex trigger on opportunity
trigger OppDefaultStageName on Opportunity (before insert, before update) {
     for(Opportunity opp: trigger.new){
           (opp.StageName = 'Qualified Opportunity');
          
     }
}

Test class
@isTest
private class TestDefaultStageName
{
 static testMethod void myTest()
 { 
 
 Account acc = new Account( Name = 'Test', BillingCountry = 'United Kingdom', Industry = 'Insurance' );
  insert acc;
 
  Opportunity opp = new Opportunity(Name = 'Opp 1', StageName = 'Qualified Opportunity', 
  Opportunity_Type__c = 'PQQ', Estimated_Value__c = 1000, Probability__c = '0-19%', Bid_Date__c = System.today(), 
  CloseDate = System.today(), Description = 'Test', AccountId = acc.Id);
  
  insert opp;
  }
 }

I know this is going to be simple but help would be appreciated
 
Carlos Campillo GallegoCarlos Campillo Gallego
Hi Cushty,

I'm not sure if the origin of the Opportunity is relevant for you in this case or if you are just mentioning it. Even if this origin was relevant, I think that your best shot would be to create a process with Process Builder and you won't need even the test class. The thing is that you are just updating a field so it'd be quite easy to achieve this with a process.

Give it a try and let me know if I can help you further. :)

Kind regards,

Carlos.
KaranrajKaranraj
You can do that using Workflow or Process builder to update the record value without writing single line of code.

1. Create your workflow rule and select Opportunity object
2. Select 'on create' and set the entry criteria
3. Then select Field update in the workflow action
4. Select Stage field and set the value as 'Prospecting'

If you look for solution only in trigger, then here is code. But i highly recommend you to go with workflow rule or process builder
trigger OppDefaultStageName on Opportunity (before insert, before update) {
     for(Opportunity opp: trigger.new){
           opp.StageName = 'Prospecting';   
     }
}


 
CushtyCushty
Hi,

Yes the process builder might do it.
How would I create this in the process builder?, I have tried the object Opportuntiy and criteria is StageName = Null and then update record stage name to the new one.
This has not worked....is there a way to make this change before the opportunity is created or am I missing something?
KaranrajKaranraj
Change condition StageName NOT NULL and then try creating new Opportunity record
CushtyCushty
Hi,

I have tried the criteria... Stage Name.. Is null.... boolean.... True, I do not get a not null option

I guess a formula like (NOT(ISPICKVAL(StageName, "") or somethign like that instead
Carlos Campillo GallegoCarlos Campillo Gallego
Since Stage Name is not a lookup field, I think that you should use ISBLANK instead of ISNULL...

Regards
CushtyCushty
Nearly!

Nothing seems to happen, this is the criteria in the process builder 

User-added image

it says global constant.null
Chandra Sekhar CH N VChandra Sekhar CH N V
I would advise going for a workflow rule because you need to mention fields like Closed date,name, forecast in process builder. Also, ensure when you are creating manually from the object, hide the field in the edit page of the layout.

User-added image
Carlos Campillo GallegoCarlos Campillo Gallego
I have just created a process and checked that works and is really simple, take a look to these pics:
Rule criteria
opportunity PB
Field update
User-added image


Hope this helps you :)

Kind regards,

Carlos
Carlos Campillo GallegoCarlos Campillo Gallego
In case you can't see the image, the rule criteria is just ISNEW()  then you just have to make the field update.
Try opening the image in another tab to see it larger :)

Regards
CushtyCushty
Hi,

I tried this but it doesn't work when creating an opportunity from the New button thats on the Opportunity related items screen
Carlos Campillo GallegoCarlos Campillo Gallego
Hi Cushty,

have you activated the process? I often forgot to do it... :)

Regards
Karen WheelerKaren Wheeler
None of these work. I'm having the same problem because SF STILL holds the control on the Stage field being required. Processbuilder doesn't work nor does Workflow. I'll be trying the trigger next...