You need to sign in to do that
Don't have an account?
Cushty
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
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
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.
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
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?
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
Regards
Nothing seems to happen, this is the criteria in the process builder
it says global constant.null
Rule criteria
Field update
Hope this helps you :)
Kind regards,
Carlos
Try opening the image in another tab to see it larger :)
Regards
I tried this but it doesn't work when creating an opportunity from the New button thats on the Opportunity related items screen
have you activated the process? I often forgot to do it... :)
Regards