You need to sign in to do that
Don't have an account?

JavaScript detail page button to update task records
Hi there,
i am wroking with the javascript detail page button.
My question here is we have a Detail page button on the Work stage tasks which when clicked should pick the Project reference from the Work stage task record and it should update with the custom filed "Project Reference" on the Task.
Any help would be much appreciated.
{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var Wid="{!Work_Stage_Tasks__c.Id}";
alert('First HI'+Wid);
var query="Select id,Project_Reference__c from Task where WhatId='{!Work_Stage_Tasks__c.Id}'";
var records=sforce.connection.query(query);
var records1=records.getArray("records");
alert(records1);
var UpdateRecords=[];
for(var i=0;i<records1.length;i++)
{
var NewTask=new sforce.SObject("Task");
NewTask.Project_Reference__c='{!Work_Stage_Tasks__c.Project_Reference__c}';
UpdateRecords.push(NewTask);
}
result=sforce.connection.update([UpdateRecords]);
Many Thanks inadvance!!
i am wroking with the javascript detail page button.
My question here is we have a Detail page button on the Work stage tasks which when clicked should pick the Project reference from the Work stage task record and it should update with the custom filed "Project Reference" on the Task.
Any help would be much appreciated.
{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var Wid="{!Work_Stage_Tasks__c.Id}";
alert('First HI'+Wid);
var query="Select id,Project_Reference__c from Task where WhatId='{!Work_Stage_Tasks__c.Id}'";
var records=sforce.connection.query(query);
var records1=records.getArray("records");
alert(records1);
var UpdateRecords=[];
for(var i=0;i<records1.length;i++)
{
var NewTask=new sforce.SObject("Task");
NewTask.Project_Reference__c='{!Work_Stage_Tasks__c.Project_Reference__c}';
UpdateRecords.push(NewTask);
}
result=sforce.connection.update([UpdateRecords]);
Many Thanks inadvance!!
For updating multiple records use -:
{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var query="Select id,Project_Reference__c from Task where WhatId='{!Work_Stage_Tasks__c.Id}'";
var recrds=[];
var updateRec = [];
recrds=sforce.connection.query(query);
var records1=recrds.getArray("records");
for(var i=0;i<records1.length;i++)
{
var NewTask=new sforce.SObject("Task");
NewTask.Id=records1[i].Id;
NewTask.Project_Reference__c='{!Work_Stage_Tasks__c.Project_Reference__c}';
updateRec.push(NewTask);
}
var result = sforce.connection.update(updateRec);
if (result[0].success=='false') {
alert('Couldn\'t save Application due to: ' + result[0].errors.message)
alert(result[0].errors);
} else {
alert('Application Created Successfully');
location.reload(true);
}
This is working fine in my org for multiple records.
Let me know if you get any issues.
All Answers
While creating task instance also populate id field.
for(var i=0;i<records1.length;i++)
{
var NewTask=new sforce.SObject("Task");
NewTask.id = records1[i].id;
NewTask.Project_Reference__c='{!Work_Stage_Tasks__c.Project_Reference__c}';
UpdateRecords.push(NewTask);
}
result=sforce.connection.update([UpdateRecords]);
let me know if this works or not.
{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var query="Select id,Project_Reference__c from Task where WhatId='{!Work_Stage_Tasks__c.Id}'";
var recrds=sforce.connection.query(query);
var records1=recrds.getArray("records");
alert(records1);
alert(recrds.records['Id']);
for(var i=0;i<records1.length;i++)
{
var NewTask=new sforce.SObject("Task");
NewTask.Id = recrds.records['Id'];
NewTask.Project_Reference__c='{!Work_Stage_Tasks__c.Project_Reference__c}';
}
var result=sforce.connection.update([NewTask]);
if (result[0].success=='false') {
alert('Couldn\'t save Application due to: ' + result[0].errors.message)
} else {
alert('Record Updated Successfully');
location.reload(true);
}
Let me know if you get any issues
Thanks for the reply.
when i tried to run your code it is giving error that "Id not specified in an update call"
Getting the same error 'Id not specified in an update call'
recrds=sforce.connection.query(query);
var records1=recrds.getArray("records");
alert('-----'+recrds.records['Id']);
for(var i=0;i<records1.length;i++)
{
var NewTask = new sforce.SObject("Task");
NewTask.Id=records1[i].Id;
NewTask.Project_Reference__c='{!Work_Stage_Tasks__c.Project_Reference__c}';
}
Let me know if you face any issues
Thanks for the code.
The above code worked for only one task.
i need to be able to update multiple tasks.
It didn't worked for a single record as well.
For updating multiple records use -:
{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var query="Select id,Project_Reference__c from Task where WhatId='{!Work_Stage_Tasks__c.Id}'";
var recrds=[];
var updateRec = [];
recrds=sforce.connection.query(query);
var records1=recrds.getArray("records");
for(var i=0;i<records1.length;i++)
{
var NewTask=new sforce.SObject("Task");
NewTask.Id=records1[i].Id;
NewTask.Project_Reference__c='{!Work_Stage_Tasks__c.Project_Reference__c}';
updateRec.push(NewTask);
}
var result = sforce.connection.update(updateRec);
if (result[0].success=='false') {
alert('Couldn\'t save Application due to: ' + result[0].errors.message)
alert(result[0].errors);
} else {
alert('Application Created Successfully');
location.reload(true);
}
This is working fine in my org for multiple records.
Let me know if you get any issues.
That worked well!!