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
DEV SFDCDEV SFDC 

create child records with javascript detail page button

Hi there,
i have a java script button on the Opportunity Deatil page  which which  clicked it should create a new Project Manager and
2. it should get the list of Quote Items  from the opportunity and then it has to  take the id created from the Project Manger  then finally it has to create project Summary records from QuoteLineItems
Here is the code that i worked with:

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var connection = sforce.connection;
var oid="{!Opportunity.Id}";
var pm = new sforce.SObject("Project_Manager__c");
alert('Opportunity ID'+oid);
pm.Opportunity__c='{!Opportunity.Id}';
var result=sforce.connection.create([pm]);
alert('Project Manager Insert successful'+pm);

var result = sforce.connection.query("Select PricebookEntry.Product2Id,UnitPrice,Quantity,Description From OpportunityLineItem Where OpportunityId = '{!Opportunity.Id}'");
var records = result.getArray("records");
alert(records);
var CreateRecords=[];

if(records[0]==null)
alert('no records to insert');
for(var i=0;i<records.length;i++)
{
   var PS=new sforce.SObject("RTC__Project_Summary__c");
   PS.Ordered__c=records[i].Quantity;
   PS.Project_ID__c='pm.id';         // i think Not getting the Project Manager Id from the above step
   PS.Generic_Product_Name__c=records[i].PricebookEntry.Product2Id;
   PS.Line_Description__c=records[i].Description;
   PS.Total_Sales_Price__c=records[i].UnitPrice;
   UpdateRecords.push(PS);
  
}
sforce.connection.create([CreateRecords]);

problem: Project manager is inserted but the project summary records are not created when clicked on the button.

Any help would be much appreciated.

Many Thanks
Best Answer chosen by DEV SFDC
Swati GSwati G
Hi,

Below code should work. change is in variable name and in getting id from result.

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var connection = sforce.connection;
var oid="{!Opportunity.Id}";
var pm = new sforce.SObject("RTC__Project_Manager__c");
alert('Opportunity ID'+oid);
pm.RTC__Opportunity__c='{!Opportunity.Id}';
var pmResult =sforce.connection.create([pm]); // changed result name

var result = sforce.connection.query("Select PricebookEntry.Product2Id,UnitPrice,Quantity,Description From OpportunityLineItem Where OpportunityId = '{!Opportunity.Id}'");
var records = result.getArray("records");
alert(records);
var CreateRecords=[];

if(records[0]==null)
alert('no records to insert');
for(var i=0;i<records.length;i++)
{
   var PS=new sforce.SObject("RTC__Project_Summary__c");
   PS.RTC__Ordered__c=records[i].Quantity;
   alert('Ordered'+PS.RTC__Ordered__c);
   PS.RTC__Project_ID__c= pmResult[0].Id; // Change code here

   alert('Mapped Project Id'+PS.RTC__Project_ID__c);

  PS.RTC__Generic_Product_Name__c=records[i].PricebookEntry.Product2Id;
   PS.RTC__Line_Description__c=records[i].Description;
   PS.RTC__Total_Sales_Price__c=records[i].UnitPrice;
   CreateRecords.push(PS);
  
}
result=sforce.connection.create([CreateRecords]);
alert(result);

All Answers

Swati GSwati G
Hi,

In this statement, you have used pm.id in single quote PS.Project_ID__c='pm.id';.

try to replace it with PS.Project_ID__c=pm.Id; 

DEV SFDCDEV SFDC
Hi Swathi,
Thanks for the response
I tried with the above code but didn't worked!!
Swati GSwati G
Hi,

You are pushing records in UpdateRecords array and creating CreateRecords array. Push records in CreateRecords array and try.
DEV SFDCDEV SFDC
It was my fault modified and here is the modified code

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var connection = sforce.connection;
var oid="{!Opportunity.Id}";
var pm = new sforce.SObject("RTC__Project_Manager__c");
alert('Opportunity ID'+oid);
pm.RTC__Opportunity__c='{!Opportunity.Id}';
var result=sforce.connection.create([pm]);

var result = sforce.connection.query("Select PricebookEntry.Product2Id,UnitPrice,Quantity,Description From OpportunityLineItem Where OpportunityId = '{!Opportunity.Id}'");
var records = result.getArray("records");
alert(records);
var CreateRecords=[];

if(records[0]==null)
alert('no records to insert');
for(var i=0;i<records.length;i++)
{
   var PS=new sforce.SObject("RTC__Project_Summary__c");
   PS.RTC__Ordered__c=records[i].Quantity;
   alert('Ordered'+PS.RTC__Ordered__c);
   PS.RTC__Project_ID__c='pm.Id';// i didn't see any ID here
   alert('Mapped Project Id'+PS.RTC__Project_ID__c);

  PS.RTC__Generic_Product_Name__c=records[i].PricebookEntry.Product2Id;
alert('Product Name'+PS.RTC__Generic_Product_Name__c);
   PS.RTC__Line_Description__c=records[i].Description;
   PS.RTC__Total_Sales_Price__c=records[i].UnitPrice;
   CreateRecords.push(PS);
   
}
result=sforce.connection.create([CreateRecords]);
alert(result);

still not working!!

Swati GSwati G

Dont use single quote there. replace that line with the below statement.

PS.RTC__Project_ID__c=pm.Id;
DEV SFDCDEV SFDC
I tried but it didn't worked well and when i used alert statement it simply saying "Undefined".

Actually  when used this alert('Project Manager ID'+result); at line 9, it gives 
   User-added image
but we are not getting the Id from Project Manager to Project summary.
Swati GSwati G
ok, then use result.id in place of pm.id. first change the name of create projectmanager's result variable and use that result's id.
DEV SFDCDEV SFDC
as you said i replace result.id in place of pm.id, also changed the project manager's result variable to "result1".

the outcome is "result1.id" is undefined.


Swati GSwati G
try alerting 0th result's id alert(result[0].id); and use that while inserting
Swati GSwati G
This change will definetly work. I tried this on my end.
DEV SFDCDEV SFDC
Thanks for that and i got the id from resut[0].id but when i used PS.RTC__Project_ID__c=RTC__result[0].Id, it is throwing error.
DEV SFDCDEV SFDC
i tried using like this PS.RTC__Project_ID__c=result[0].Id; it is giving  ''cannot read propety  Id of undefined"
Abhi__SFDCAbhi__SFDC
use 

sforce.connection.create(CreateRecords);

I am not sure why have you used UpdateRecords.push(PS) , if you are creating null record.

use below lines to know the error message 

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);
}


Let me know if you get any issues.
Swati GSwati G
Hi,

Below code should work. change is in variable name and in getting id from result.

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var connection = sforce.connection;
var oid="{!Opportunity.Id}";
var pm = new sforce.SObject("RTC__Project_Manager__c");
alert('Opportunity ID'+oid);
pm.RTC__Opportunity__c='{!Opportunity.Id}';
var pmResult =sforce.connection.create([pm]); // changed result name

var result = sforce.connection.query("Select PricebookEntry.Product2Id,UnitPrice,Quantity,Description From OpportunityLineItem Where OpportunityId = '{!Opportunity.Id}'");
var records = result.getArray("records");
alert(records);
var CreateRecords=[];

if(records[0]==null)
alert('no records to insert');
for(var i=0;i<records.length;i++)
{
   var PS=new sforce.SObject("RTC__Project_Summary__c");
   PS.RTC__Ordered__c=records[i].Quantity;
   alert('Ordered'+PS.RTC__Ordered__c);
   PS.RTC__Project_ID__c= pmResult[0].Id; // Change code here

   alert('Mapped Project Id'+PS.RTC__Project_ID__c);

  PS.RTC__Generic_Product_Name__c=records[i].PricebookEntry.Product2Id;
   PS.RTC__Line_Description__c=records[i].Description;
   PS.RTC__Total_Sales_Price__c=records[i].UnitPrice;
   CreateRecords.push(PS);
  
}
result=sforce.connection.create([CreateRecords]);
alert(result);

This was selected as the best answer
DEV SFDCDEV SFDC
Hi Swati,
It worked thanks !!