Skip to content

June 26, 2015

Salesforce: Triggering Closed/Won Opportunity to New Entry in Assets Table associated with Opportunity.

by Joe Kuan

In Salesforce, when an opportunity stage becomes ‘Closed/Won’, it doesn’t automatically convert the Products in Opportunity (OpportunityLineItem) into Assets. Luckily, there are two contributed packages available in Salesforce’s Appexchange; one by Comity Design and the other one by Salesforce Lab.

In this article, we

  • Install the Closed/Won trigger package
  • Add custom field to Asset table to reference Opportunity
  • Modify trigger code to update the reference to Opportunity in the Asset table
  • Show how to include the Assets table in the Accounts page layout

Install Package

Lets install the Salesforce Lab package into our sandbox. Once the package is installed, we can see it in the App Setup -> Deploy -> Installed Packages.

Salesforce installed package

When we click on the installed package name and click on the ‘View Components’ button at the top. It shows the package details as follows:

salesforce package details

As we can see, the package contain 3 components: trigger function, test trigger class and a custom field. The Converted_to_Asset is a custom boolean field added to the Opportunity Product object (which is stored in OpportunityLineItem table). This field is mainly to avoid any OLI being inserted again into the Assets table. For example: for some reasons a user changes the opportunity Closed/Won stage to other setting, then adds more product items and later changes the stage back to Closed/Won which triggers CreateAssetonClosedWon handler. Only the new OLI records are inserted to the Assets table.

Lets click on the CreateAssetonClosedWon name which displays the trigger source code. The code is very simple.


However, we want to improve the package a little further. At the time of writing, Salesforce’s Asset table doesn’t keep a reference of Opportunity. This is necessary if there are different sales people (Opportunity Owner) opportunities on the same customer (Account). We would like to view the customer Account with Asset and also able to trace back to different opportunities.

Add Asset -> Opportunity Relationship

So the next step is to create a Opportunity custom reference field in the Asset table. Select Customize -> Assets -> Fields, then select the ‘Lookup Relationship’ radio button, select option ‘Opportunity’ for the ‘Related To’. Enter ‘Opportunity’ for the Field Name. Click ‘Next’ all the ways until the ‘Save’ button. Once the custom field is created, it should look like this at the bottom of Customize -> Assets -> Fields:


Modify Trigger Code to Update the Relationship

The next task is to edit the trigger code to update this custom Opportunity field. Click on ‘Edit’ button on CreateAssetonClosedWon trigger (under Installed Package -> View Components) and add the following highlighted line:

            a.Name = ol.PricebookEntry.Product2.Name;
            a.Opportunity__c = opptyId;
            ol.Converted_to_Asset__c = true;

Update Accounts Page Layouts to Show Assets Table

Finally, we would like to display the Assets table in the customer Account page. Go to Customize -> Accounts -> Page Layouts. Select ‘Related Lists’ like below screenshot, then drag ‘Assets’ on the right hand side onto the page:

Salesforce edit page layout

Once the item is dropped, we should see something like this in the layout edit page.


Since the Opportunity is a custom field, it is not included by default. Click on the top left spanner icon and add the Opportunity field to the right.


Click ‘Ok’, and ‘Save’ to update the layout. Lets add couple Products to an Opportunity, then set the Stage to Closed/Won and observe the outcome on the Account page.


Now we can see that an Assets table is included in the customer Account page associated with reference to the Opportunities.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: