Skip to content

Posts from the ‘Apex’ Category


Salesforce: Writing a Test class for Opportunity Product (OpportunityLineItem)

A note on writing a test class for Opportunity Product (OpportunityLineItem), when assigning a product to an OpportunityLineItem, we need to make sure the product is active. Otherwise, we will get integrity exception. To do that, we use isActive property to filter all the current active products.

    Account a = new Account(Name = 'Test Account Joe ');
    PricebookEntry pbID = [select Id, name from PricebookEntry where isActive = True limit 1];
    Opportunity o = new Opportunity();
    OpportunityLineItem ol = new OpportunityLineItem();
    insert a;

    o.AccountId = a.Id;
    o.Name = 'Test_Joe_123';
    o.StageName = 'Prospecting';
    o.CloseDate =;
    o.Type = 'New Business';
    insert o;
    ol.OpportunityId = o.Id;
    ol.Quantity = 1;
    ol.UnitPrice = 2.00;
    ol.PricebookEntryId = pbId.Id;
    insert ol;

Salesforce: Workaround to Assign Asset Name with Auto Increment Unique Value

In Salesforce, when creating an Asset entry, the Name field is a mandatory text field. For some users, this field has no particular purpose but cannot be left empty. For others, they may want to set the field with unique incremental id. In order to assign the Name field with an auto incremental value, a workaround solution is to first create a custom numeric field, (e.g.) Auto Update Id, with auto increment property.


Next step is to create a trigger which sets the Name field to the Auto_Update_Id value. Goes to Customize -> Assets -> Triggers and creates a trigger with the following simple code:

trigger SetNameToAutoIncrId on Asset (after insert) {

    for(Asset a:{
       for (Asset ua: [ Select Name, Auto_Update_Id__c FROM Asset where Id = ]) {
           ua.Name = ua.Auto_Update_Id__c;
           update ua;

Since the auto increment value won’t be assigned until an Asset record is created, therefore we have to create a trigger with after insert event. Also Salesforce does not allow users to modify the triggered record in after insert event. So that we need to issue another SOQL query, assign the result to another Asset object and then update the record to the DB.

Although the user still needs to input something into the mandatory Name field, the insert operation will automatically overwrite the Name field with an unique numerical id.


Developing a Salesforce Trigger

Lets assume we need to develop a trigger to perform two checks on opportunities:

  • When creating or updating an opportunity, the Type selection box must be selected with a value
  • When an opportunity stage is selected to ‘Closed/Won’, the opportunity must contain Products (OpportunityLineItem) entry

Read more »


Where in Salesforce to create a trigger

There are two ways to create a trigger in Salesforce page; Setup or Developer Console

Read more »


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

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

Read more »