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
Andrew AldisAndrew Aldis 

Custom Button to Create Child Records

I am trying to create a button that will run a quary then create child records.  The query seems to be working but the insert fails, usually do to incorrect data types such as dates and ids coming across as strings.  I have rewritten it a couple of time to upsert and create the records but run into the same problem either way.  When I remove the quotes from the values I am going to insert it fails to recognize the values.  Can some one help I feel like there are just 1 or 2 little tweaks needed to get this working.


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

var sol =  sforce.connection.query("Select Id, Name, SCMC__Sales_Order__c, SCMC__Sales_Order__r.Id, Installation_ID__c, SCMC__Item_Master__r.Id, SCMC__Item_Master__r.PSA_Task_Automation__c, SCMC__Item_Master__r.PSA_Task_Automation_Assigned_To__c, SCMC__Item_Master__r.PSA_Task_Automation_Budg_Bill_Hours__c, SCMC__Item_Master__r.PSA_Task_Automation_Duration__c, SCMC__Item_Master__r.PSA_Task_Automation_Notes__c, SCMC__Item_Master__r.PSA_Task_Automation_Role__c, SCMC__Item_Master__r.PSA_Task_Automation_Task_Name__c, PSA_Created__c, PSA_Task_Automation__c From SCMC__Sales_Order_Line_Item__c where PSA_Task_Automation__c = True AND PSA_Created__c = False AND SCMC__Sales_Order__r.Installation__c = '{!SFDC_Project__c.Id}'");
var records = sol.getArray("records");




if("{!$User.Id}"== "00580000008JMjO" ){

confirm("This will create "+records.length+" Project Tasks.");
for(var i=0; i<=records.length; i++){
var psa = new sforce.SObject("Project_Task__c");
psa.Project__c = '{!SFDC_Project__c.Id}';
psa.Assigned_To__c = 'sol.SCMC__Item_Master__r.PSA_Task_Automation_Assigned_To__c';
psa.Project_Task_Notes__c = 'sol.SCMC__Item_Master__r.PSA_Task_Automation_Notes__c';
psa.Role__c = 'sol.SCMC__Item_Master__r.PSA_Task_Automation_Role__c';
psa.Task__c = 'sol.SCMC__Item_Master__r.PSA_Task_Automation_Task_Name__c';
psa.Start_Date__c = '{!TODAY()}';
psa.Duration__c = 'sol.SCMC__Item_Master__r.PSA_Task_Automation_Duration__c';
}
var result = sforce.connection.upsert('Id',[psa]);

if (result[0].getBoolean("success")) {
  alert("new tasks created with id " + result[0].id);
} else {
  alert("failed to create tasks " + result[0]);
}
alert('There are no automated tasks to create.');
SalesFORCE_enFORCErSalesFORCE_enFORCEr
This will give you current date
var utc = new Date().toJSON().slice(0,10);
Andrew AldisAndrew Aldis
That does not change anything, it still says wrong data type.  Thanks though.