You need to sign in to do that
Don't have an account?
Back to Basics!! Creating a new record with apex
Hi,
I am begining to explore apex - its a must i guess - i seem to be at the upper limit of what i can do with SF just using the UI.
So...as a start I was hoping one of the kind people on here could help me out......
What I am trying to acheive, i belive requires Apex. Effectivly, when the criteria is met on one object, a new record is created on the other record.
So, I have the standard Opportunity Object and a Custom Object called 'CS', when the 'Stage' of the Opportunity is set to 'Closed Won', I need a record to be created on 'CS' (which i should point out is linked by a lookup relationship - created by this trigger).
I gather, that this is real basic functionality, however was hoping someone could guide me in the right direction, perhaps with some sample code based on the above, so i can begin to understand how it all slots together.
Many thanks in advance for your help.
Steve
Yes there is a workaround. I am hoping after the trigger when workflow are executed then there must be some filed update is going which again call the trigger, and it all counts in one context.
So to avoid the execution of trigger again after the workflow update just create this class :
And modify your trigger like this :
Thanks
Ankit Arora
Blog | Facebook | Blog Page
All Answers
Use this trigger change API names as per yours in it
Try this :
There may be an instance where an opportunity have Stage equals close won. Also it is working at my end (without any syntax error and without any functionality break). Let me know if there is any problem using this.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Hi this works a treat - however it creates 2 new records???
Which one you worked on even though second one is just the same except case of insert,
tried first one but nothing happened - second one worked 'out of the box' but creates 2 CS records.
No this will create only one record. Don't worry try it. I have included the conditions : Trigger.isUpdate and Trigger.isInsert.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Are you having any workflow written on opportunity object?
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Ankit - I have tried it - it does work but it does create 2 records!!
Ok thats starnge I tested the first one and worked fine ,
Two reason why first did not work at your end
1) You directly setting it to Close/Won , and I just gave you a trigger for after update
2) I used 'Close/Won' instead of Close Won , you can change it lke this
Do you have a case where you might have stage as Close Won at insert directly.
Steve, there must be any workflow on opportunity which may cause the trigger run again. Can you confirm that is there any workflow written on oppty?
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Hi ok - i have got both to work now - (it was the 'Closed/Won' - should be 'Closed Won') However both still creating 2 records.
Yes I do have workflow on opportunites. The workflow that are there must be there, is there any way of getting the apex to run after the workflow.
Yes there is a workaround. I am hoping after the trigger when workflow are executed then there must be some filed update is going which again call the trigger, and it all counts in one context.
So to avoid the execution of trigger again after the workflow update just create this class :
And modify your trigger like this :
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Just update firstt one to this
It should work for you
There is a flaw in this Static class solution
in case there is a list having items for both inser and update then static variable will be set when item for insert will come and next item will not come in the trigger , so it will fail in such case.
Really ? Doesn't make sense to me as I don't think trigger get execute parallel in case of insert and update. And in both case the context is different :)
Have you done this before or just expecting this behavior.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Definitly i have done this trigger execution is such that it comes again and again in the trigger once with insert items without ids then with update items
I have tested it out and wrote a blog as well on this
http://forceschool.blogspot.com/2011/05/writing-apex-trigger-order-of-execution.html
Please let me know if i am missing any thing
You have discussed about "Cyclic Trigger Invocation" which is Trigger to Trigger. But in this case first trigger fired, and then workflow executes which again leads to execute the trigger. And in this case static Boolean value will work for sure.
Please correct me if I am wrong.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Hi - didnt mean to cause an argument! Sorry.....
The Static option works brilliantly! Thank you very very much!
Can you please explain to me how the Static Class works? So I can perhaps implement this myself in the future.
Gr8 that worked for you,
And that was not an argument, just a friendly conversation and purpose of that was just to help you and increase our knowledge as well if any of us miss any thing. Me, Ankit and others as well meet on community a lot and these conversations took place and our experience is that it has always increased our knowledge. Thats why community is the best place to learn new thing.
Hi All
I have a question regarding the solution of this post. Please dont mind if you find this a silly question.
My question is: what if we are updating the same object too in the ' after insert ' trigger? Although i know this is a bad approach. but salesforce still allows it.
will not the static approach affect my expected behaviour ?
Regards,
TryCoding
Funda of static class is very simple. When we execute a process in salesforce each property and values remain static in Static class till the process get completed. We do not need to instantiate the class to fetch its property or method, we can directly get the value with "ClassName.PropertyName".
As you have seen in the example provided we have a boolean property which is set to false by default when the process is started. And is set to true in the trigger. When this trigger is again called after the workflow get processed then value will remain to true as Trigger>Workflow>Trigger is executed in the same process.
Best is explained here :
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_static.htm
Thanks
Ankit Arora
Blog | Facebook | Blog Page
Hi,
this worked fine in my sandbox - however - I have gone to deploy and get the following:
Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
can you help?
This is because you are deploying the trigger on to production without its test class. You need to create a test class this trigger and deploy both to production.
Please create a new thread for it, pasting your trigger code there. Some one from us will provide you the test class for it.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
oh thanks for the advice.
Have done so here:
http://boards.developerforce.com/t5/Apex-Code-Development/Apex-Test-Help/td-p/316229
Hi - sorry to come back to this so long after the post - but as i have continued to develop our salesforce I am running into a problem - this trigger you have so kindly helped with me - which includes the StaticClass.flag works perfectly - however if i have used this trigger as a template to insert other records on the same object at the same time and the trigger dosnt trigger - i guess due to the fact that the flag is changed to true at the end of the trigger which means everything else using the flag dosnt work...which makes sense to me - however, how can I get this to work? Hope this makes sense....
Steve