• Naureen Hameed 39
  • NEWBIE
  • 10 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hello 
I have a Trigger on Quote Line for Products
i)Integration
ii)Integration Bundle
iii) Platform Fee 

Upon selection of the above three products, an additional multiselect field on Quote Line should be chosen to give further details on on integrations (field "Platform__c")  or Plarform Fees (Custom_Platform_Selection__c).

The values of the additional field should then be stamped on a text field "PlatformText__c".  Thats the main function of this Trigger. 

trigger QuoteLineItemTrigger on QuoteLineItem (after insert, after update) {
    Set<Id> quoteIds = new Set<Id>();

    // Collect the Quote Ids
    for (QuoteLineItem quoteLine : Trigger.new) {
        quoteIds.add(quoteLine.QuoteId);
    }

    // Query the related Quote Line Items and populate the PlatformText__c field
    List<QuoteLineItem> quoteLinesToUpdate = new List<QuoteLineItem>();

    // Process the Quote Line Items for Integration products
    for (QuoteLineItem quoteLine : [SELECT Id, Product2.Name, Platform__c, Custom_Platform_Selection__c, PlatformText__c
                                    FROM QuoteLineItem
                                    WHERE QuoteId IN :quoteIds
                                    AND (Product2.Name = 'Integration' OR Product2.Name = 'Integration Bundle')
                                    AND Platform__c != null]) {
        String platformText = String.join(quoteLine.Platform__c.split(';'), ';');
        if (!platformText.equals(quoteLine.PlatformText__c)) {
            quoteLine.PlatformText__c = platformText;
            quoteLinesToUpdate.add(quoteLine);
        }
    }

    // Process the Quote Line Items for Platform Fee products
    for (QuoteLineItem quoteLine : [SELECT Id, Product2.Name, Platform__c, Custom_Platform_Selection__c, PlatformText__c
                                    FROM QuoteLineItem
                                    WHERE QuoteId IN :quoteIds
                                    AND Product2.Name = 'Platform Fee'
                                    AND Custom_Platform_Selection__c != null]) {
        String platformText = String.join(quoteLine.Custom_Platform_Selection__c.split(';'), ',');
        if (!platformText.equals(quoteLine.PlatformText__c)) {
            quoteLine.PlatformText__c = platformText;
            quoteLinesToUpdate.add(quoteLine);
        }
    }

    // Update the Quote Line Items
    if (!quoteLinesToUpdate.isEmpty()) {
        try {
            update quoteLinesToUpdate;
        } catch (DmlException e) {
            // Handle the exception if needed
        }
    }
}

Test Class: 

@isTest
private class QuoteLineItemTriggerTest {
    @isTest
    static void testTrigger() {
        // Ensure the standard pricebook is active
        Id pricebookId = Test.getStandardPricebookId();
        Pricebook2 standardPricebook = new Pricebook2(
            Id = pricebookId,
            IsActive = true
        );
        update standardPricebook;

        // Create test data
        Quote testQuote = new Quote(
            Name = 'Test Quote',
            OpportunityId = '0064U00000qZMNmQAO' // Replace with a valid Opportunity Id
        );
        insert testQuote;

        // Create Product2 records
        Product2 integrationProduct = new Product2(Name = 'Integration');
        insert integrationProduct;

        Product2 platformFeeProduct = new Product2(Name = 'Platform Fee');
        insert platformFeeProduct;

        // Create PricebookEntry records for your products in the standard Pricebook
        PricebookEntry integrationStandardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, // Use the retrieved pricebookId
            Product2Id = integrationProduct.Id,
            UnitPrice = 100.00, // Set an appropriate UnitPrice
            IsActive = true
        );
        insert integrationStandardPrice;

        PricebookEntry platformFeeStandardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, // Use the retrieved pricebookId
            Product2Id = platformFeeProduct.Id,
            UnitPrice = 50.00, // Set an appropriate UnitPrice
            IsActive = true
        );
        insert platformFeeStandardPrice;

        // Create a custom pricebook for testing
        Pricebook2 customPricebook = new Pricebook2(
            Name = 'Custom Pricebook',
            IsActive = true
        );
        insert customPricebook;

        // Create PricebookEntry records for your products in the custom pricebook
        PricebookEntry integrationPricebookEntry = new PricebookEntry(
            Pricebook2Id = customPricebook.Id,
            Product2Id = integrationProduct.Id,
            UnitPrice = 100.00, // Set an appropriate UnitPrice
            IsActive = true
        );
        insert integrationPricebookEntry;

        PricebookEntry platformFeePricebookEntry = new PricebookEntry(
            Pricebook2Id = customPricebook.Id,
            Product2Id = platformFeeProduct.Id,
            UnitPrice = 50.00, // Set an appropriate UnitPrice
            IsActive = true
        );
        insert platformFeePricebookEntry;

        // Update the Quote to use the custom pricebook
        testQuote.Pricebook2Id = customPricebook.Id;
        update testQuote;

        // Create QuoteLineItem records using the custom pricebook entries
        QuoteLineItem integrationLineItem = new QuoteLineItem(
            QuoteId = testQuote.Id,
            PricebookEntryId = integrationPricebookEntry.Id,
            Quantity = 2,
            Platform__c = 'Absolute;Citrix Systems'
        );
        QuoteLineItem platformFeeLineItem = new QuoteLineItem(
            QuoteId = testQuote.Id,
            PricebookEntryId = platformFeePricebookEntry.Id,
            Quantity = 3,
            Custom_Platform_Selection__c = 'Production Environment;CSM'
        );

        List<QuoteLineItem> quoteLineItems = new List<QuoteLineItem> {
            integrationLineItem,
            platformFeeLineItem
        };

        // Test trigger execution
        Test.startTest();
        insert quoteLineItems;
        Test.stopTest();

        // Verify results
        integrationLineItem = [SELECT PlatformText__c FROM QuoteLineItem WHERE Id = :integrationLineItem.Id];
        platformFeeLineItem = [SELECT PlatformText__c FROM QuoteLineItem WHERE Id = :platformFeeLineItem.Id];

        System.assertEquals('Absolute;Citrix Systems', integrationLineItem.PlatformText__c);
        System.assertEquals('Production Environment;CSM', platformFeeLineItem.PlatformText__c);
    }
}

Deploymeny Error: 
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [UnitPrice]: [UnitPrice]
Stack Trace: Class.QuoteLineItemTriggerTest.testTrigger: line 86, column 1

The error is looking for Unit Price but the Unit Price is in the test Class. 

Please help and thanks in advance. 


 
Hello working on a Trigger with three requirements: 

 A custom Object Evalutation "Evaluations__c" has a master detail relationship to "Opportunity".
Opportunity has a custom "Primary_eval_owner__c" field.

Requirements: 
1) When an Evaluation is created, Trigger should check the "Primary_eval_owner__c" field on Opportunity and if filled, assign Evaluation to existing eval owner.
2)If "Primary_eval_owner__c" is null, assign it to next owner in round robbin.
3)Update " "Primary_eval_owner__c" with the name of the new User assigned so subsequent Evaluations can be assigned to the same owner. 
4) We have two Users that need to round robbin.
5) Addditionally, I have created this trigger for assignment part (requirement 2) but the 4th last line is giving the error: "Field is not writable: Evaluations_c.Round_Robin_ID__c"

Thank you in advance. 

Trigger: 

trigger SCRoundRobinTrigger on Evaluations__c (before insert) {
    if(Trigger.isBefore){
        Double roundRobinValue = 1;
        List<Evaluations__c> Evalist = [SELECT Id, Round_Robin_ID__c, CreatedDate FROM Evaluations__c where Round_Robin_ID__c  != null
                                 order by CreatedDate desc limit 1];
        if(Evalist!= null && Evalist.size()>0){
            roundRobinValue = Evalist[0].Round_Robin_ID__c;
        }
       
        
        for(Evaluations__c c: Trigger.New){
            system.debug('###Round Robin Value : '+roundRobinValue);
            if(roundRobinValue == 2){
                roundRobinValue = 1;
                c.OwnerId = '0054U000009JgsIQAS';
            }
            else{
                roundRobinValue++; 
                c.OwnerId = '0054U000009J81gQAC';
            }
             c.Round_Robin_ID__c = roundRobinValue;
        }
    }
}
Hello working on a Trigger with three requirements: 

 A custom Object Evalutation "Evaluations__c" has a master detail relationship to "Opportunity".
Opportunity has a custom "Primary_eval_owner__c" field.

Requirements: 
1) When an Evaluation is created, Trigger should check the "Primary_eval_owner__c" field on Opportunity and if filled, assign Evaluation to existing eval owner.
2)If "Primary_eval_owner__c" is null, assign it to next owner in round robbin.
3)Update " "Primary_eval_owner__c" with the name of the new User assigned so subsequent Evaluations can be assigned to the same owner. 
4) We have two Users that need to round robbin.
5) Addditionally, I have created this trigger for assignment part (requirement 2) but the 4th last line is giving the error: "Field is not writable: Evaluations_c.Round_Robin_ID__c"

Thank you in advance. 

Trigger: 

trigger SCRoundRobinTrigger on Evaluations__c (before insert) {
    if(Trigger.isBefore){
        Double roundRobinValue = 1;
        List<Evaluations__c> Evalist = [SELECT Id, Round_Robin_ID__c, CreatedDate FROM Evaluations__c where Round_Robin_ID__c  != null
                                 order by CreatedDate desc limit 1];
        if(Evalist!= null && Evalist.size()>0){
            roundRobinValue = Evalist[0].Round_Robin_ID__c;
        }
       
        
        for(Evaluations__c c: Trigger.New){
            system.debug('###Round Robin Value : '+roundRobinValue);
            if(roundRobinValue == 2){
                roundRobinValue = 1;
                c.OwnerId = '0054U000009JgsIQAS';
            }
            else{
                roundRobinValue++; 
                c.OwnerId = '0054U000009J81gQAC';
            }
             c.Round_Robin_ID__c = roundRobinValue;
        }
    }
}