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
Kon DeleKon Dele 

Onclick Javascript Button Issue

I am trying to create a custom button on the Quote page that creates a Sales Request record when clicked.
The Sales Request is a child object and has a lookup to Quote & Opportunity fields.
I keep getting this error: 'Cannot set property 'Id' of undefined'
Any direction would be appreciated. Here's what I have so far:

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

var srq = new sforce.SObject("Sales_Request__c");
  srq.Id = "{!Sales_Request__c.Id}";
  srq.Quote__r.Id = "{!Quote.Id}";
  srq.Quote__r.OpportunityId = "{!Opportunity.Id}";
  srq.Status_c = "New";
  srq.Type__c = "New";
 
  var result = sforce.connection.create([srq]);
    
   if(result[0].getBoolean("success")){
   window.location = "/" + result[0].id + "/e";
    }else{
    alert('Could not create record '+result);
}


Best Answer chosen by Kon Dele
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Hi Kon,

Please check this if it works :

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

var srq = new sforce.SObject("Sales_Request__c");
  srq.Id = "{!Sales_Request__c.Id}";
  srq.Quote__c = "{!Quote.Id}";
  srq.Status_c = "New";
  srq.Type__c = "New";
 
  var result = sforce.connection.create([srq]);
    
   if(result[0].getBoolean("success")){
   window.location = "/" + result[0].id + "/e";
    }else{
    alert('Could not create record '+result);
}

Thanks,
N.J

All Answers

Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Hi Kon,

Please check this if it works :

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

var srq = new sforce.SObject("Sales_Request__c");
  srq.Id = "{!Sales_Request__c.Id}";
  srq.Quote__c = "{!Quote.Id}";
  srq.Status_c = "New";
  srq.Type__c = "New";
 
  var result = sforce.connection.create([srq]);
    
   if(result[0].getBoolean("success")){
   window.location = "/" + result[0].id + "/e";
    }else{
    alert('Could not create record '+result);
}

Thanks,
N.J
This was selected as the best answer
Kon DeleKon Dele
Hi N.J,

Awesome! Thank you, works perfectly! One more clarification, how could I automatically populate the Opportunity field related to the Quote on the object?

I created a lookup Opportunity field but that doesn't seem to work. Any ideas?

Kon
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Right, you can have opportunity lookup on your object and assign related opportunity to object .
e.g  srq.Opportunity__c = "{!Quote.Opportunity}"

If you want to add individual fields from opportunity then you might need to query quote related opportunity to access individual fields.

Thanks,
N.J