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
kickingboykickingboy 

How can I detect when a custom link has been clicked?

How can I detect when a custom link has been clicked so that I can update a field? For example if someone clicks on the google maps link on Account. Happy to do some preprocessing before passing the data through to the URL but I cant think of a solution.

Best Answer chosen by Admin (Salesforce Developers) 
kickingboykickingboy

 

{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")} 
{!REQUIRESCRIPT("/js/functions.js")} 

var objAcc = new sforce.SObject("Account"); 
var numValue = Number("{!Account.atestfield__c}");
numValue = numValue + 1;
objAcc.Id = "{!Account.Id}"; 
objAcc.atestfield__c =  String(numValue);
 
var saveRes=sforce.connection.update([objAcc]); 
if (saveRes[0].success=='false')
{ 
alert(saveRes[0].errors.message); 
}
var url = 'http://maps.google.com/maps?f=q&hl=en&q={!Account.BillingStreet}+{!Account.BillingCity}+{!Account.BillingState}&om=1'
window.open( url);

 

This code works. Seems that I needed to put the merge field in quotes. 

 

All Answers

b-Forceb-Force

Change your link content source to execute javascript,

 

Do some field update Sample code for field update

 

 

{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")} 
{!REQUIRESCRIPT("/js/functions.js")} 

var objInvoice = new sforce.SObject("Invoice__c"); 
objInvoice.Id = {!Id}; 
objInvoice.Email_Invoice__c=true; 
var saveRes=sforce.connection.update([objInvoice]); 
if (saveRes[0].success=='false')
{ 
alert(saveRes[0].errors.message); 
} 

 

and then redirect to specific url

 

 

 

Cheers,

Bala

kickingboykickingboy

 

Thanks Bala
Here is what I wrote based on your script  but I get an error "missing ; before statement " when I click the link. Also how do I redirect to the google maps URL 
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")} 
{!REQUIRESCRIPT("/js/functions.js")} 
var objAcc = sforce.SObject("Account"); 
objAcc.Id = {!Account.Id}; 
objAcc.atestfield__c = 'xxx'; 
var saveRes=sforce.connection.update([objAcc]); 
if (saveRes[0].success=='false')
alert(saveRes[0].errors.message); 
}

 

kickingboykickingboy

 

{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")} 
{!REQUIRESCRIPT("/js/functions.js")} 

var objAcc = new sforce.SObject("Account"); 
var numValue = Number("{!Account.atestfield__c}");
numValue = numValue + 1;
objAcc.Id = "{!Account.Id}"; 
objAcc.atestfield__c =  String(numValue);
 
var saveRes=sforce.connection.update([objAcc]); 
if (saveRes[0].success=='false')
{ 
alert(saveRes[0].errors.message); 
}
var url = 'http://maps.google.com/maps?f=q&hl=en&q={!Account.BillingStreet}+{!Account.BillingCity}+{!Account.BillingState}&om=1'
window.open( url);

 

This code works. Seems that I needed to put the merge field in quotes. 

 

This was selected as the best answer
b-Forceb-Force

surround code with try catch as best practice 

kickingboykickingboy

Good point. Thanks for your help