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
BrandiTBrandiT 

Hyperlink Formula - execute javascript? - one click Task Completed button

Can I create a hyperlink formula field that will execute javascript?  I have a custom detail page button on tasks that marks a task complete with the following code:

 

{!requireScript("/soap/ajax/10.0/connection.js")};
sforce.connection.session = "{!$Api.Session_ID}";

function updateTask( )
{
try
{
var task = new sforce.SObject("Task");
task.Id = "{!Task.Id}";
task.Status = "Completed";
var result = sforce.connection.update([task]);
if (result[0].getBoolean("success") == false ) {
alert(result[0].errors.message);
return;
}
window.top.location.href=window.top.location.href;
}
catch (e) {
alert(e);
}
}

updateTask();

 

 

I'm trying to show this function in a list view.  I couldn't get a list button to work with this code so I was hoping if I could create a formula field to display a link that would do the same thing as the button, I could just add the field to my related list.

 

I'm trying to give my users the ability to mark tasks complete in a list view with one click (similar to home page functionality, but on opportunties).

 

Has anyone done this before?  Is it even possible?

Joseph D.ax1437Joseph D.ax1437

Did you ever figure this out?  Trying to do the same thing, but no way I can see to do it...

Alok_NagarroAlok_Nagarro

Hi Brandit,

 

The problem in your code is that you are handling single record to update, since in case of ListButton you are gonna place this button to task related list. So it will be expecting multiple records (which have user selected from list view).

 

Try the following code -

 

{!requireScript("/soap/ajax/10.0/connection.js")};
sforce.connection.session = "{!$Api.Session_ID}";

function updateTask( )
{
    try
    {
        var records = {!GETRECORDIDS($ObjectType.Task)};
                var allrecords=new Array();
        if (records[0] == null) {
            alert("Please select at least one record.")
        }
        else{
            for(var i=0;i<records.length;i++)
            {
                var task = new sforce.SObject("Task");
                                task.Id = records[i];
                 task.Status = "Completed";
                allrecords.push(task);
            }
            var result = sforce.connection.update(allrecords);
                for(var j=0;j<result.length;j++){
                if (result[j].getBoolean("success") == false ) {
                    alert(result[j].errors.message);
                    return;
                }
                }
                window.top.location.href=window.top.location.href;
        }
    }
catch (e) {
alert(e);
}
}

updateTask();

 

 

 

Hopw it will work !!

Rajiv BhattRajiv Bhatt
It is defnitely possible. You can use javascript in formula fields. You can even call javascript code which is lying in your static resources. Here I have explained in detail with code how to do it:

https://sfdc-gyaan.rhcloud.com/2014/04/referencing-javascript-file-from-static-resources-in-formula-fields/

Hope this helps!
Mazlow Cohen 12Mazlow Cohen 12
Can you help me with this? I am trying to put the following javascript. I want to update a field on a custom object, by clicking a hyperlink text field, that I willl put in a report. So the users can update the field without leaving the report. I want the hyperlink to execute the following or someway to update the record. I have a custom buttom on the layout, but I can't put a custom button in a report. Only fields. Thanks!

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
var ProcTask = new sforce.SObject("FSTR__Process_Task__c"); 
ProcTask.Id = '{!FSTR__Process_Task__c.Id}'; 
ProcTask.FSTR__Status__c = 'Completed'; 

var result = sforce.connection.update([ProcTask]); 
location.reload(true);