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

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
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
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
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;
Thanks for the response
I tried with the above code but didn't worked!!
You are pushing records in UpdateRecords array and creating CreateRecords array. Push records in CreateRecords array and try.
{!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!!
Dont use single quote there. replace that line with the below statement.
PS.RTC__Project_ID__c=pm.Id;
Actually when used this alert('Project Manager ID'+result); at line 9, it gives
but we are not getting the Id from Project Manager to Project summary.
the outcome is "result1.id" is undefined.
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.
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);
It worked thanks !!