0

I am trying to create a custom button using Javascript to create a custom object record (Project__c) from another custom object record (Quote__c). The problem is I need to pull the Opportunity ID and Record Type from the Quote__c object into the Project__c object, but I'm not sure of the syntax to use in Javascript to accomplish this. Anytime I enter the fields below, I get an error telling me the field is invalid.

    {!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 

var crfId=sforce.connection.query("Select id from Project__c Where Quote__c.Id='{!Quote__c.Id}'"); 

var oppId=sforce.connection.query("Select Opportunity2__c.Id from Quote__c"); 
var quoteId=sforce.connection.query("Select Id from Quote__c");
var oppRTId=sforce.connection.query("Select RecordTypeId. from Opportunity"); 


if(crfId.size == 0) 
{ 

window.open("/apex/VF_Quote?id=quoteId&OppRecordType=oppRTId"); 
} 
else{ 
alert('The Opportunity already has a Contract Request associated to it.'); 
}
  • your oppId,quoteId,oppRtId would blow up. You are not selecting relative to the quote + why do you need seperate queries to extract same info from quote? Also u dont seem to be using crfId,oppId at all why query project,quote object then? – Rao Aug 29 '18 at 17:31
  • As you can probably tell, I am not at all a Javascript programmer. I am trying to re-purpose button code that currently sits on the Opp. I am trying to create the Project record and pre-populate the Opportunity, Quote, and Opportunity Record Type. The window.open VF page allows the user to change the RT if needed. – J. Neilan Aug 29 '18 at 17:35
  • "pre-populate the Opportunity, Quote, and Opportunity Record Type" do you have all these field on project object (or) are you trying to prepopulate fields on different objects? if you want to prepopulate on diff objects then you have to have some kinda lookup/master detail relation in respect to the project object. If you have link setup between the diff objects in context with the project object then you can prepopulate the objects related to the project record. – Rao Aug 29 '18 at 17:49
  • Those fields are all on the Project object. The button sits on the Quote object. Quote has a lookup field to Opportunity. I am trying to create a Project to pre-populate Quote in a lookup field, Quote's related Opp in a lookup field, and quote's related Opp Record Type in a RT field. – J. Neilan Aug 29 '18 at 17:51
1

If you are confident with Apex, then you can write a simple apex method and call that via your JavaScript button. See below example to see how straightforward it is:

Apex class

global class MyClass { 
    webservice static Id createProject(String quoteId) {
        // Fetch Quote__c and related fields
        // Create Project__c record and return the Id
        return project.Id;
    }
}

JavaScript button

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

var quoteId = '{!Quote__c.Id}'; // Something like this?
var projectId = sforce.apex.execute("MyClass","createProject", {'quoteId':quoteId}); 

// Refresh or open Project record window
0

Thank you for the suggestions. I was able to accomplish my button with the following Javascript:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 

var a=sforce.connection.query("Select id from Project__c Where Opportunity__c='{!Quote__c.Opportunity2Id__c}'"); 

var myOpp = "SELECT Id,Type FROM Opportunity WHERE Id = '{!Quote__c.Opportunity2Id__c}'";

result = sforce.connection.query(myOpp); 
records = result.getArray("records");
var relOpp = records[0];

if(a.size == 0) 
{ 

window.open("/apex/VF_Quote?id="+relOpp.Id+"&OppRecordType="+relOpp.Type); 
} 
else{ 
alert('The Opportunity already has a Contract Request associated to it.'); 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.