Variable does not exist: Quantity

Although it does exist as I added a value to it and it worked fine

this is the code

public class AssetTriggerTest2 {

    @isTest static void test(){

        Account acc = new Account();
        acc.Name = 'farah test';
        insert acc;

        Opportunity opp = new Opportunity();
        opp.Name = 'tests';
        opp.CloseDate = Date.today();
        opp.StageName ='Demo';
        opp.AccountId = acc.Id;
        opp.Demo_Date__c = Date.today();
        opp.New_Vehicle_Feed_Provider__c ='AutoFunds';
        insert opp;

        Product2 p1 = new Product2();
        p1.Family = '360 Suite';
        p1.IsActive = true;
        p1.Name ='360 Suite';    
        insert p1;

        Id pricebookId = Test.getStandardPricebookId();
        PricebookEntry pb1 = new PricebookEntry();
        pb1.Product2Id =p1.Id;
        pb1.Pricebook2Id = pricebookId;
        pb1.UnitPrice = 100.00;
        pb1.IsActive = true;
        insert pb1;

        OpportunityLineItem oli = new OpportunityLineItem(
                                                OpportunityId = opp.Id,
                                                Quantity = 5,
                                                PricebookEntryId = pb1.Id,
                                                TotalPrice = Quantity * pb1.UnitPrice
        insert oli;

        opp.StageName = 'Closed Won';
        update opp;



You can't reference another field in your Field=value assignment pairs. If you want to reference a value relative to another field, use a constant to store the initial value.

OpportunityLineItem oli = new OpportunityLineItem(
    Quantity = QUANTITY_VALUE,
    TotalPrice = QUANTITY_VALUE * pb1.UnitPrice
    // other fields

Or if you really want to reference the value of another field, you can break it out into a separate assignment, outside your SObject constructor.

OpportunityLineItem oli = new OpportunityLineItem(
    Quantity = 5
    // other fields
oli.TotalPrice = oli.Quantity * pb1.UnitPrice;

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.