5

Following the documentation on navigation with the sforce.one object, I'm attempting to create Opportunity Products with a link on a Visualforce page that has been added to the page layout and is visible in the Details section of the Lightning Experience view of the Opportunity. I'm calling:

sforce.one.createRecord('OpportunityLineItem');

When clicking "Add Product" from the standard Lightning Experience interface, the "Opportunity" value on the lightbox form is pre-populated.

However, calling sforce.one.createRecord('OpportunityLineItem'); results in a blank "Opportunity" value and clicking in the Product textbox results in an error.

createRecord Add Product

Clearly, calling sforce.one.createRecord('OpportunityLineItem'); isn't providing the Opportunity ID to the Add Product form, but I haven't been able to find any documentation or discussion of how to make this work.

I did find a known issue (my reputation is too low to post two links, but it's issue id=a1p3A00000186xfQAA) with Salesforce, that looks like a similar problem.

I know I can create a Visualforce page of my own for adding products, but I'd rather leverage the "Add Product" functionality that already exists in Lightning Experience, if possible. Am I missing something, or are there any pointers you'd recommend?

Here are the steps to reproduce the issue:

  1. Create the following Visualforce page and enable "Available for Salesforce mobile apps and Lightning Pages"

    <apex:page standardController="Opportunity"> 
      <script type="text/javascript"> 
          function createOLI() { 
              sforce.one.createRecord('OpportunityLineItem'); 
          } 
      </script> 
      <div onclick="createOLI();">Create OpportunityLineItem</div> 
    </apex:page>
    
  2. Add the new Visualforce page to the Opportunity Page Layout

  3. View an Opportunity
  4. Click "Create OpportunityLineItem"

You'll see the "Create Opportunity Product" lightbox, but the Opp ID isn't passed in.

  • Can you please post your code here, so we can give a shot – Santanu Boral Jan 3 '17 at 23:35
  • Thanks for giving this a shot, @santanu-boral. I've added the steps to reproduce the issue above. – Philip F Jan 11 '17 at 22:52
3

This was likely not possible at the time this question was asked. It seems partly possible now, but still very buggy.

sforce.one.createRecord('OpportunityLineItem'); 

Before Summer'17 it was not possible to prepopulate fields. If the observation stated above was correct (I can't verify this anymore), it was not even possible to populate the fields manually. The prepopulation feature was added then and now you can do this:

sforce.one.createRecord('OpportunityLineItem',null,{ 
    OpportunityId : "0060Y000007uaVI", 
});

But the Product field is still readonly, you can't pick the product and you can't save it. It looks like this and gives you only an ugly error message:

enter image description here

Now if you know already the PricebookEntryId you are going to use, you can prepopulate this too:

sforce.one.createRecord('OpportunityLineItem',null,{ 
    OpportunityId : "0060Y000007uaVI", 
    PricebookEntryId: "01u0Y000001R5ro", 
});

On the UI the product field still is blank and readonly but at least the record gets saved.

Caution 1:

The this feature from Summer'17 works only if you put your Visualforce Page to API v40.0 or higher.

Caution 2:

Objectnames and Fieldnames are CASE-SENSITiVE! So opportunitylineItem is not OpportunityLineItem and only OpportunityLineItem is correct

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.