3

I understand generally WHY this happens, but I'm curious if there is a better way to write the two references in code below so that if I RENAME the FIELD in SF, it updates the code?

{!REQUIRESCRIPT ("/soap/ajax/13.0/connection.js")} 

var formid = "{!Company_Form_Opp__c.Id}"; 
var userid = "{!$User.Id}"; 
var FormToUpdate = new sforce.SObject("Company_Form_Opp__c"); 

var padyear = {!YEAR(TODAY())}; 
var padmonth = {!MONTH(TODAY())}; 
if( padmonth.toString().length == 1 ) { padmonth = '0'+padmonth } 
var padday = {!DAY(TODAY())}; 
if( padday.toString().length == 1 ) { padday = '0'+padday } 

if( "{!Company_Form_Opp__c.RETURNFUNDS_Returned_By_Signature__c}" != '' ) { 
alert("Already signed!"); 

} else { 

FormToUpdate.set("Id", formid); 
FormToUpdate.set( "RETURNFUNDS_Returned_By_Signature__c" , userid); 
FormToUpdate.set( "RETURNFUNDS_Returned_By_Signature_Date__c" , padyear + '-' + padmonth + '-'+ padday ); 
var Result = sforce.connection.update([FormToUpdate]); 

if(Result[0].getBoolean("success")) { 
function redirect() { parent.frames.location.replace("/{!Company_Form_Opp__c.Id}"); } 
redirect(); 
} else { 
alert("Error"); 
} 

}

The field reference at the "Already Signed!" IF updates itself. The two below in FORMTOUPDATE.SET() do not. What could I do to alter the FORMTOUPDATE references so that if I alter the Field name the code updates itself?

  • No "solution" for this at this time (2013-04-18). Leaving question OPEN for now per suggestion received on Meta site. – AMM Apr 18 '13 at 15:43
2

Edit: As at April 2013, $ObjectType is not evaluated within Custom Button context. The solution below should be considered a non-starter at this time :(

You could leverage the cached sObject describe result methods.

For example: {!$ObjectType.Object__c.fields.Field__c.Name} returns the field name.

Or in your case:

FormToUpdate.set('{!$ObjectType.Company_Form_Opp__c.fields.RETURNFUNDS_Returned_By_Signature__c.Name}', userid); 
FormToUpdate.set('{!$ObjectType.Company_Form_Opp__c.fields.RETURNFUNDS_Returned_By_Signature_Date__c.Name}', padyear + '-' + padmonth + '-'+ padday);

In Visualforce context, those references are coupled to the schema and will update accordingly.

  • 1
    Nice. I was thinking do a describe, but couldn't quite wrap my brain around how you could have it be coupled to the schema. – Daniel Hoechst Apr 12 '13 at 16:47
  • Sounds awesome. Will check it out shortly. Thanks. – AMM Apr 12 '13 at 18:01
  • Hmm. Error: Field Company_Form_Opp__c.fields.RETURNFUNDS_Returned_By_Signature_Date__c.Name does not exist. Check spelling (happens for both lines) (I did fix the first line where you had _Date in the var name) – AMM Apr 12 '13 at 18:09
  • @AMM can you confirm the object name works via {!$ObjectType.Company_Form_Opp__c.Name}? – bigassforce Apr 12 '13 at 18:38
  • 1
    Well crap :( I guess the $ObjectType global is completely absent from Custom Button contents then. – bigassforce Apr 12 '13 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.