Create Opportunity

This example shows how to create an opportunity from the Product object.

  1. In Application Composer, expand the Product standard object and click the Actions and Links node.

  2. In the Script region of the Create Action or Link page, click the New icon to build your script and enter the following:

    def vo = newView('OpportunityVO');
    
    //create the opportunity
    def newRecord = vo.createRow();
    newRecord.setAttribute('Name',<ItemNumber attribute for Opportunity object> + " - " + now()); 
      
    //create revenue lines
    def revenueLine = newRecord.ChildRevenue;
    def createRevenue = revenueLine.createRow();
    createRevenue.setAttribute('ProductType','Item');
    createRevenue.setAttribute('InventoryItemId',<InventoryItemId attribute for Opportunity object>); 
      
    createRevenue.setAttribute('InventoryOrgId',<InvOrgId attribute for Opportunity object>);
      
    createRevenue.setAttribute('Quantity',10);
    createRevenue.setAttribute('UnitPrice',10);
    
    revenueLine.insertRow(createRevenue);
    vo.insertRow(newRecord);
    showmessage()

The showmessage() method is optional. It's a custom function that generates a warning message telling your users that a new opportunity is created and that they need to click Save or Save and Close. So, it can be removed from the code.

To display the message in a dialog box, do the following:

  1. Create a Text field in the Product object. You don't have to display it in the layout.

  2. Create an object function as shown below:

    • Name: showmessage

    • Type: Void

    • Code:

      def ran = new Random()
      setAttribute('Showmessage_c',ran.nextInt())
  3. Create a Field Validation.

    Enter an error message: "A new opportunity was created, please click Save or Save and Close.

    Code:

    adf.error.warn(null)