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
KarenCKarenC 

Need help with button/trigger/page layout....weird problem...

I don't know java, however, I received some help and wrote a button to clone a quote...see revise button below..

Here is the code behind the button:  

 

 

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}result = sforce.connection.query("select id,opportunityid from Quote where id = '{!Quote.Id}'")window.top.location.href =    "{!URLFOR( $Action.Quote.NewQuote ,null,[clone=1,id=Quote.Id,retURL="/"&Quote.Id],true)}&oppid=" +result.records.OpportunityId +"&00N60000001rya1=1&00N60000001rtxU={!Quote.Version__c+1}"

 

When the insert on the cloned quote is done a before insert trigger is fired.  The purpose of the trigger is to find the max revision number within a quote within an opportunity and then increment the revision number by one.

 

The trigger works fine as long as I have all the fields displayed on the screen below.  The weird part is once I remove some of these fields from the page layout the trigger no longer works.  I don't want to leave the fields (such as revision_chkbox__c) as these fields are behind the scene fields as switches.  

 

Sorry for long post -

 

Please advise,,,,I am banging my head against the wall....

 

 
trigger quoteversioning on Quote (before insert) {
    Quote q;
    for (quote insertedquote : Trigger.new) {
        if (insertedquote.revision_chkbox__c == true) {
            q = [select version__c from quote
                where quote_number_test__c = :insertedquote.quote_number_test__c and
                      opportunityid = :insertedquote.opportunityid
                order by version__c desc
                limit 1];
            insertedquote.version__c = q.version__c + 1;
        }
    }
}
sfdcfoxsfdcfox

You're technically in the wrong board; this is a question for the AJAX Toolkit & S-Controls board (you are using JavaScript, not Java)...

 

Anyways, your problem is that you're creating the record through the edit page, which will enforce visibility on fields that you have. Since the fields are not generated, their values are silently ignored.

 

Instead, create the record through the API entirely, then jump to the created record ID when you're done:

 

{!requirescript('/soap/ajax/22.0/connection.js')}
var currentQuote = sforce.connection.query('select id,opportunityid from quote where id = \'{!Quote.Id}\'')
var newQuote = new sforce.SObject('Quote')
newQuote.OpportunityId = currentQuote.records.OpportunityId
newQuote.revision_checkbox__c = true
newQuote.version__c = parseInt({!Quote.version__c}) + 1
var insertResult = sforce.connection.insert([newQuote])
if(insertResult.getBoolean('success'))
  window.top.location.href = insertResult[0].Id
else
  alert('Problem creating new revision.')

My AJAX Toolkit knowledge is a bit rusty, but that should be the general idea. Just insert the new record and have the trigger run without any intervention. By bypassing the edit page, you get full access to the fields (assuming Field Level Security is visible and not read-only).

 

You could even skip *this* step and just call Apex Code directly using sforce.apex.execute (requires /soap/ajax/22.0/apex.js), which minimizes the JavaScript coding you have to write, with the trade-off being that you'll need more Apex Code. Field Level Security is completely ignored in that case, and you have more freedom to do what you want it to do.

_Prasu__Prasu_

Simplest solution will be setting the default value for the field "revision_chkbox__c" to true from the field level on Quote object.

 

Better appraoch will be using the above given way for implementing this.