You need to sign in to do that
Don't have an account?
stefanodl
trigger to inser new opportunity
Hello,
I need to create a trigger in order to insert a new opportunity when a speficic condition is verified on existing opportunity.
The ruel should be the following:
IF Opp is Won and if Opp.NumberField < Acc.Number field THEN insert a new Opp with Type equals to Upsell.
Thanks for your help.
Stefano
Here is your trigger
I am not sure as I have not implemented this, but is there any stage name "Won" ???
And I also hope you have tested this trigger and it is not creating any cascading???
Please update any API Name or value as pre your requirement, I used API Name and values from ur post.
Now that's looks good to me, good work Shashikant.
One more change in trigger
Take this out side the for loop.
Hello Ankit,
thanks a lto for your help!
I changed name of fields to the correct ones and i tried to run a test, but the test is not covering the following part:
Can you pplease explain me what does the line 9 mean (opp.clone(false)) ?
Also, no Opportynity is created after the conditions are verified.
Am I missing something?
Many thanks,
Stefano
Clone false means creating a clone of the record which will hav same field values as the record we are cloning. false in parameter will remove id from the new cloned record as we want to insert a new oppor tunity record with new id.
Hello,
thanks for your explanation.
Unfortunately, th trigger is not working. Here is the updated trigger:
And this is the Test:
Test is covering only 44% and Trigger is not inserting and Opportunity.
What's wrong according to you?
Many thanks in advance.
Stefano
I guess trigger must be failing as you are using the field "opp.Account.N_of_Hotels__c" which is from Account.
This field would not be directly accesible from the opp record in trigger. you will need to make a query on the Account and bring correesponding Accounts and then use them while comparing.
Make sure you dont make a query on Account in loop. As previosuly post said, make DML action out of loop.
I've created a new formula field in Opportunitty recalling the same field from Account, but still not working.
Also, I've added "Update Oppty1 and Update Oppty2 in the apex classs, and test covering is always 44%.
It's really strange.
Hello,
at the end the trigger now it's working!
thanks,
Stefano
What was the issue?
HI,
I've tweaked and activate the following trigger:
trigger insertNewRevenue on Opportunity (after update) {
List<Opportunity> listOppor = new List<Opportunity>();
for(Opportunity opp : Trigger.new)
{
//Please update number api name of opp.NumberField and opp.Account.AccountNumber as per your requirement
if(opp.StageName == 'closed Won')
{
Opportunity oppNew = opp.clone(false);
oppNew.Type = 'Upsell';
//By chaning status you will by pass trigger for newly created opportunity
//Some StageName except the one used in entry condition (Won in your case)
oppNew.StageName = 'Forecasted';
oppNew.Name = 'TO BE UPDATED';
listOppor.add(oppNew);
}
if(listOppor.size() > 0)
insert listOppor;
}
}
It's look ok, but I would like to automatically change the closing date to the following month.
Today all opportunity close date is the last day of the month, so when it create a new opp upon closing the current one, I need to have the closing date updated to the following month.
Any idea how to do it ?
Thanks
Barbara
Also I did not understand this part of the triger : && opp.Account != null && opp.NumberField < opp.Account.AccountNumber)
So i deleted it. I hope it's okay