function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RachaelCRachaelC 

Upsert call not being recognized

Here is my code... for some reason, if I don't include the first line, then I get an error that "void" is not recognizable...

However I am getting this directly from DeveloperForce Guide so I am sure that it's me who is incorrect... So, I added the first line but even though now my code will save without giving me an error BUT the word upsert at line 24 should be blue, because it's a DML operation... so why isn't it? I am SO new to writing code so please forgive if this is a very novice question... I am writing this as a class...maybe it is really the trigger?

 

 

public class CreateAssetFromOli {

    public void upsertExample() {

    Opportunity opp = [SELECT Id, Name, AccountId, StageName,

                            (SELECT Id, PricebookEntry.Product2Id, PricebookEntry.Name

                             FROM OpportunityLineItems)

                       FROM Opportunity

                       WHERE HasOpportunityLineItem = true and StageName = 'Finance Approved'

                       LIMIT 1];

                      

    Asset[] assets = new Asset[]{};

 

    // Create an asset for each line item on the opportunity

    for (OpportunityLineItem lineItem:opp.OpportunityLineItems) {

 

        //This code populates the line item Id, AccountId, and Product2Id for each asset

        Asset asset = new Asset(Name = lineItem.PricebookEntry.Name,

                                Line_Item_ID__c = lineItem.Id,

                                AccountId = opp.AccountId,

                                Product2Id = lineItem.PricebookEntry.Product2Id);

                                assets.add(asset);

    }

   

    try {

        upsert assets Line_Item_ID__c;  // This line upserts the assets list with

                                        // the Line_Item_Id__c field specified as the

                                        // Asset field that should be used for matching

                                        // the record that should be upserted.

    } catch (DmlException e) {

        System.debug(e.getMessage());

        }

    }

}