0

I'm trying to experiment from this page on sforce.one.createRecord(). My goal is to be able to update and/or add to existing opportunity records.

Below is my code. I click the link, but it just refreshes the page. Is there something obvious that I'm doing wrong? I'm in a basic trailhead generated org with API v48. I'm doing this through the dev console.

<apex:page> 
<script>
  function doTheThing(){
     sforce.one.createRecord('OpportunityLineItem', null,
                             {
                                 OpportunityId : "0066g00000ktbhhAAA", 
                                 Product2Id : "Product2", 
                                 ProductCode : "GC1060", 
                                 Quantity : "2",
                             }
                            );
  }
</script>  
<apex:outputLink onClick="doTheThing()">Add Product GC1060</apex:outputLink>
</apex:page>

1 Answer 1

1

Aside from any other problems, there's at least two easily visible problems:

Product2Id : "Product2", 

Any time you have an Id field, you need the 15- or 18-character Id. You'll want to look up the correct value.

Quantity : "2",

Anything inside quotes is a "string", but Quantity is a "number." Numbers do not have quotes around them:

Quantity: 2,

Also, there's two other less-obvious problems.

Product2Id : "Product2", 

This field is Read-Only. Use PricebookEntryId instead (and it'll be a PricebookEntry object, not a Product2 or Pricebook2 object).

For the second problem, you need to check the documentation for required fields on records. Notably, for an OpportunityLineItem, you need to specify either UnitPrice or TotalPrice along with the Quantity.

Finally, this code only works in the Salesforce1 app. Make sure you're trying it on your phone or tablet, not a desktop device/web browser. If you want a mechanism supported across all devices and experiences, you'll want to take a look at the apex:remoteObjects Visualforce component.

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.