Long
long ago I posted a blog about updating other
object's status automatically. I have been planning for the next blog ever
since. Finally took a break and posting one of the trick which I used in one of
the project or implementation.
You all might know that in PE (Professional Edition) you can't
write apex codes. That cuts off building much complex functionality from the
instance. But sometimes clients are so adamant that they don't understand (or
try so) developer's pain of inability of building the process that requires
coding. Why? Because they have paid money!!
One of such situation which I recently came across: To re-use the
Opportunity as Tender, but Stages should be different!! Here Opportunity Name
cannot be used, as Tender Number (auto number) needs to be copied to the name.
I could have used Tender as a custom object, but if I
did I had to re-do the standard functionality of adding products,
quotes. Adding Products can be done with a junction object, but what about
Quote, syncing etc?? Remember, it is PE. Hence had to reuse the Opportunity
object.
Now the problem of overriding Opportunity Stage values with Tender
Stage and copying the Tender Number to name to be solved. Obvious solution for
different stages would be merging both Opportunity stages and Tender Stages
into Stage field OR overriding with a VF
page and hiding the Stage. The first one is gonna piss-off the
client (obviously design is not good), the latter looks promising but the Stage
field is mandatory and it cannot be updated
through workflow because validation is executed before workflow.
Did workflow field update (yes, this client had purchased workflow along with
PE) come to your mind for copying the value? It doesn't work as (again)
validation rule are executed before workflow field updates, hence it shows
error on Name field.
Enough of dragging the problem, coming to the solution: Create 2
VF pages - one for Opportunity and other for Tender. Create 2 list buttons -
New Opportunity and New Tender, these will be shown under Account, in
Opportunity related list. We won’t have any problem with Opportunity VF as we
are gonna use all standard fields. Problem would be with
Tender VF. Solution for this is to fill the mandatory field values
through JavaScript. Hide the Stage, CloseDate, Name (or any other standard
fields) from VF page and in the background fill some values in them.
Here is the Visualforce page code with some
JavaScript trick:
JavaScript trick:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <apex:page id="page_opp" standardcontroller="Opportunity"> <apex:form id="form_opp"> <script type="text/javascript"> function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } } addLoadEvent(test); function test() { var d=new Date(); document.getElementById("{!$Component.j_id2:gen:cls}").value = d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear(); document.getElementById("{!$Component.j_id2:gen:stg}").value = "SomeStageValue"; } </script> <apex:sectionheader subtitle="New Tender" title="Tender Details" /> <apex:pageblock title="Tender Information"> <apex:pagemessages/> <apex:pageblockbuttons location="Both"> <apex:commandbutton action="{!Save}" value="Save" /> <apex:commandbutton action="{!Cancel}" value="Cancel"/> </apex:pageblockbuttons> <apex:pageblocksection id="gen" showheader="false" title="General"> <apex:inputfield label="Tender Name" value="{!Opportunity.Name}" /> <apex:inputhidden id="stg" value="{!Opportunity.StageName}" /> <apex:inputhidden id="cls" value="{!Opportunity.CloseDate}" /> <apex:inputfield value="{!Opportunity.Tender_Authority__c}" /> ..... ... .. </apex:pageblocksection> </apex:pageblock> </apex:form> </apex:page> |
Comments are expected as there can be other ways of doing this. If you have any idea please share..