• John Edwards 3
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 0
    Replies
Hello Crew,

I have a problem that is absolutely baffling me. I am trying to write a test class for the following function where it successfully inserts new OpportunityLineItems:

 public static String insertOpportunityLineItems(List<OpportunityLineItem> oppLineItemsToUpdate)
    {
        String updateExceptions = '';
        try
        {
            insert oppLineItemsToUpdate;
        }
        catch(Exception e)
        {
            updateExceptions += 'Update OpportunityLineItems: ' + String.valueOf(oppLineItemsToUpdate) + ', Exception: ' + e.getMessage() + ' ';
        }
        System.debug(updateExceptions);
        return updateExceptions;
    }  

The test I have written is:

@isTest
    public static void newInsertOpportunityLineItemsSuccess()
    {  
        OpportunityLineItem startLinteItem = [
            Select 
            Id, 
            OpportunityId,
            Product2Id,
            PricebookEntryId,
            Quantity,
            UnitPrice,
            Split_Products__c 
            From OpportunityLineItem 
            WHERE Quantity = 1
            AND ARC__c = 2
            AND TotalPrice = 1]; //'1,2,3'
        List<OpportunityLineItem> startList = New List<OpportunityLineItem>();
        for(Integer i; i < 2; i++)
        {
            startList.add(New OpportunityLineItem(
                            OpportunityId = startLinteItem.OpportunityId,
                            Product2Id = startLinteItem.Product2Id,
                            PricebookEntryId = startLinteItem.PricebookEntryId,
                            Quantity = startLinteItem.Quantity,
                            UnitPrice = startLinteItem.UnitPrice,
                            ARC__c = 20));
        }
        String testResult = OpportunityLineItemTriggerHandler.insertOpportunityLineItems(startList);
        System.assertEquals('', testResult);
        List<OpportunityLineItem> testLineItems = [Select Id From OpportunityLineItem Where ARC__c = 20];
        System.assertEquals(2, testLineItems.size());
    }

This fails, with the error INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]. However, when I take the creation of the OpportunityLineItems out of the loop it passes:

 @isTest
    public static void newInsertOpportunityLineItemsSuccess()
    {  
        //This object is setup in the @setup method
        OpportunityLineItem startLinteItem = [
            Select 
            Id, 
            OpportunityId,
            Product2Id,
            PricebookEntryId,
            Quantity,
            UnitPrice,
            Split_Products__c 
            From OpportunityLineItem 
            WHERE Quantity = 1
            AND ARC__c = 2
            AND TotalPrice = 1]; //'1,2,3'
        List<OpportunityLineItem> startList = New List<OpportunityLineItem>();
        startList.add(New OpportunityLineItem(
                            OpportunityId = startLinteItem.OpportunityId,
                            Product2Id = startLinteItem.Product2Id,
                            PricebookEntryId = startLinteItem.PricebookEntryId,
                            Quantity = startLinteItem.Quantity,
                            UnitPrice = startLinteItem.UnitPrice,
                            ARC__c = 20));
        startList.add(New OpportunityLineItem(
                            OpportunityId = startLinteItem.OpportunityId,
                            Product2Id = startLinteItem.Product2Id,
                            PricebookEntryId = startLinteItem.PricebookEntryId,
                            Quantity = startLinteItem.Quantity,
                            UnitPrice = startLinteItem.UnitPrice,
                            ARC__c = 20));
        String testResult = OpportunityLineItemTriggerHandler.insertOpportunityLineItems(startList);
        System.assertEquals('', testResult);
        List<OpportunityLineItem> testLineItems = [Select Id From OpportunityLineItem Where ARC__c = 20];
        System.assertEquals(2, testLineItems.size());
    }

As if this behavior wasn't bizarre enough if I add just one more OpportunityLineItem to the list and try and again *queue stranger things music* it fails again with the same error INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

@isTest
    public static void newInsertOpportunityLineItemsSuccess()
    {  
       //This object is setup in the @setup method
        OpportunityLineItem startLinteItem = [
            Select 
            Id, 
            OpportunityId,
            Product2Id,
            PricebookEntryId,
            Quantity,
            UnitPrice,
            Split_Products__c 
            From OpportunityLineItem 
            WHERE Quantity = 1
            AND ARC__c = 2
            AND TotalPrice = 1]; //'1,2,3'
        List<OpportunityLineItem> startList = New List<OpportunityLineItem>();
        startList.add(New OpportunityLineItem(
                            OpportunityId = startLinteItem.OpportunityId,
                            Product2Id = startLinteItem.Product2Id,
                            PricebookEntryId = startLinteItem.PricebookEntryId,
                            Quantity = startLinteItem.Quantity,
                            UnitPrice = startLinteItem.UnitPrice,
                            ARC__c = 20));
        startList.add(New OpportunityLineItem(
                            OpportunityId = startLinteItem.OpportunityId,
                            Product2Id = startLinteItem.Product2Id,
                            PricebookEntryId = startLinteItem.PricebookEntryId,
                            Quantity = startLinteItem.Quantity,
                            UnitPrice = startLinteItem.UnitPrice,
                            ARC__c = 20));
        startList.add(New OpportunityLineItem(
                            OpportunityId = startLinteItem.OpportunityId,
                            Product2Id = startLinteItem.Product2Id,
                            PricebookEntryId = startLinteItem.PricebookEntryId,
                            Quantity = startLinteItem.Quantity,
                            UnitPrice = startLinteItem.UnitPrice,
                            ARC__c = 20));
        String testResult = OpportunityLineItemTriggerHandler.insertOpportunityLineItems(startList);
        System.assertEquals('', testResult);
        List<OpportunityLineItem> testLineItems = [Select Id From OpportunityLineItem Where ARC__c = 20];
        System.assertEquals(2, testLineItems.size());
    }

I am both mystified and horrified. I have similar logic in a trigger I am about to push to production and all end-to-end and ui tests have passed but I don't know if I can deploy with this cockroach in the back of my mind.

If someone could provide some insight I would be extremely grateful.

Thanks!



 
Hello crew, this problem has got me tasting pennies so if anyone could lend some insight I would be eternally grateful. So this is a vastly simplified version of what I am working with (excuse me if this question is poorly formatted, this submission form is a nightmare):

trigger OpportunityTrigger on Opportunity (after insert, after update) {
   <Unimportant Logic Here>
   Set<String> emailSet = OpportunityTriggerHandler.unimportantLogic(Trigger.oldMap, Trigger.NewMap)
   List<Contact> matchingContactsOpenAccounts = OpportunityTriggerHandler.matchingContactsWithAcctStatus(emailSet, 'OPEN');
<More Unimortant Logic>
}

public class OpportunityTriggerHandler {

    public static List<Contact> matchingContactsWithAcctStatus(Set<String> emailSet, 
                                                               String accountStatus)
    {
        List<Contact> matchingContacts = [
            Select
            AccountId,
            Email
            From Contact
            Where Email In :emailSet
            And Account.Billing_Status__c = :accountStatus
            And Account.Original_Close_Date__c != Null       
        ];
        return matchingContacts;
    }
}

@isTest
public class OpportunityTriggerTest {
   @testSetup
       private static void setup()
       {
           <Set Up A Bunch of Stuff>
       }
   
   @isTest
    private static void testWinBackInsertWithNoMatchingAccounts()
    {
        Account beforeOpp = [Select Id from Account Where Name = 'Acct One'];
        Date now = System.today();
        Opportunity o = New Opportunity(
            Name = 'Test Opp',
            AccountId = beforeOpp.Id,
            StageName = 'Booked at DOW',
            CloseDate = now);
        insert o;
        Account afterOpp = [Select Id, Win_Back__c from Account Where Name = 'Acct One'];
        System.assert(afterOpp.Win_Back__c);
    }
}

So the problem I am running into is with the SOQL query in the method matchingContactsWithAcctStatus. This method accepts a list of strings (emails) and an account status and queries for contacts with a matching email and account bill status WHERE the Original_Close_Date__c != Null. 

Original_Close_Date__c is a rollup field that holds the date of the first closed Opportunity on the account. Now here is the fun part. When I functionally test this trigger by creating objects in my dev org, it works fine. If I create mock data in the above test class and call the static function directly in my test class it works fine. However, in the above example, when I insert a mock Opportunity to fire the trigger IN A TEST CLASS the SOQL function is no longer able to access the value in that rollup field. It always returns Null value when calling the SOQL query from within that function. I literally invoked the trigger in the test class and ran it side by side calling the method with the same params and watched as they returned different data.

My initial reaction is to just stop using rollup functions in Apex triggers if they can't reliably be tested. I can just throw in another function that determines whether or not that field is null but this seems so... dumb. Am I missing anything? 

Thanks!
Hello Team,

I have followed all of the processes for pushing Change Sets and Profile accessibility but all of my fields are still showing as hidden when I push to production. I have:

1. Checked the field accessibility on the field in my sandbox and it is editable for all profiles.

User-added image

2. I created my change set with the fields in question and ALL profiles.

User-added imageUser-added image
3. I have uploaded and deployed the change set.
4. At this point, the field level security for all profiles has been switched to hidden.
User-added image

After reviewing all documentation and previously written articles this should be everything that needs to be done. We have an exorbitant amount of profiles, which is another problem in and of itself, and I don't have time to go profile by profile amending this every time I push changes up to production.

Any ideas?
Hello Crew,

I have a problem that is absolutely baffling me. I am trying to write a test class for the following function where it successfully inserts new OpportunityLineItems:

 public static String insertOpportunityLineItems(List<OpportunityLineItem> oppLineItemsToUpdate)
    {
        String updateExceptions = '';
        try
        {
            insert oppLineItemsToUpdate;
        }
        catch(Exception e)
        {
            updateExceptions += 'Update OpportunityLineItems: ' + String.valueOf(oppLineItemsToUpdate) + ', Exception: ' + e.getMessage() + ' ';
        }
        System.debug(updateExceptions);
        return updateExceptions;
    }  

The test I have written is:

@isTest
    public static void newInsertOpportunityLineItemsSuccess()
    {  
        OpportunityLineItem startLinteItem = [
            Select 
            Id, 
            OpportunityId,
            Product2Id,
            PricebookEntryId,
            Quantity,
            UnitPrice,
            Split_Products__c 
            From OpportunityLineItem 
            WHERE Quantity = 1
            AND ARC__c = 2
            AND TotalPrice = 1]; //'1,2,3'
        List<OpportunityLineItem> startList = New List<OpportunityLineItem>();
        for(Integer i; i < 2; i++)
        {
            startList.add(New OpportunityLineItem(
                            OpportunityId = startLinteItem.OpportunityId,
                            Product2Id = startLinteItem.Product2Id,
                            PricebookEntryId = startLinteItem.PricebookEntryId,
                            Quantity = startLinteItem.Quantity,
                            UnitPrice = startLinteItem.UnitPrice,
                            ARC__c = 20));
        }
        String testResult = OpportunityLineItemTriggerHandler.insertOpportunityLineItems(startList);
        System.assertEquals('', testResult);
        List<OpportunityLineItem> testLineItems = [Select Id From OpportunityLineItem Where ARC__c = 20];
        System.assertEquals(2, testLineItems.size());
    }

This fails, with the error INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]. However, when I take the creation of the OpportunityLineItems out of the loop it passes:

 @isTest
    public static void newInsertOpportunityLineItemsSuccess()
    {  
        //This object is setup in the @setup method
        OpportunityLineItem startLinteItem = [
            Select 
            Id, 
            OpportunityId,
            Product2Id,
            PricebookEntryId,
            Quantity,
            UnitPrice,
            Split_Products__c 
            From OpportunityLineItem 
            WHERE Quantity = 1
            AND ARC__c = 2
            AND TotalPrice = 1]; //'1,2,3'
        List<OpportunityLineItem> startList = New List<OpportunityLineItem>();
        startList.add(New OpportunityLineItem(
                            OpportunityId = startLinteItem.OpportunityId,
                            Product2Id = startLinteItem.Product2Id,
                            PricebookEntryId = startLinteItem.PricebookEntryId,
                            Quantity = startLinteItem.Quantity,
                            UnitPrice = startLinteItem.UnitPrice,
                            ARC__c = 20));
        startList.add(New OpportunityLineItem(
                            OpportunityId = startLinteItem.OpportunityId,
                            Product2Id = startLinteItem.Product2Id,
                            PricebookEntryId = startLinteItem.PricebookEntryId,
                            Quantity = startLinteItem.Quantity,
                            UnitPrice = startLinteItem.UnitPrice,
                            ARC__c = 20));
        String testResult = OpportunityLineItemTriggerHandler.insertOpportunityLineItems(startList);
        System.assertEquals('', testResult);
        List<OpportunityLineItem> testLineItems = [Select Id From OpportunityLineItem Where ARC__c = 20];
        System.assertEquals(2, testLineItems.size());
    }

As if this behavior wasn't bizarre enough if I add just one more OpportunityLineItem to the list and try and again *queue stranger things music* it fails again with the same error INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

@isTest
    public static void newInsertOpportunityLineItemsSuccess()
    {  
       //This object is setup in the @setup method
        OpportunityLineItem startLinteItem = [
            Select 
            Id, 
            OpportunityId,
            Product2Id,
            PricebookEntryId,
            Quantity,
            UnitPrice,
            Split_Products__c 
            From OpportunityLineItem 
            WHERE Quantity = 1
            AND ARC__c = 2
            AND TotalPrice = 1]; //'1,2,3'
        List<OpportunityLineItem> startList = New List<OpportunityLineItem>();
        startList.add(New OpportunityLineItem(
                            OpportunityId = startLinteItem.OpportunityId,
                            Product2Id = startLinteItem.Product2Id,
                            PricebookEntryId = startLinteItem.PricebookEntryId,
                            Quantity = startLinteItem.Quantity,
                            UnitPrice = startLinteItem.UnitPrice,
                            ARC__c = 20));
        startList.add(New OpportunityLineItem(
                            OpportunityId = startLinteItem.OpportunityId,
                            Product2Id = startLinteItem.Product2Id,
                            PricebookEntryId = startLinteItem.PricebookEntryId,
                            Quantity = startLinteItem.Quantity,
                            UnitPrice = startLinteItem.UnitPrice,
                            ARC__c = 20));
        startList.add(New OpportunityLineItem(
                            OpportunityId = startLinteItem.OpportunityId,
                            Product2Id = startLinteItem.Product2Id,
                            PricebookEntryId = startLinteItem.PricebookEntryId,
                            Quantity = startLinteItem.Quantity,
                            UnitPrice = startLinteItem.UnitPrice,
                            ARC__c = 20));
        String testResult = OpportunityLineItemTriggerHandler.insertOpportunityLineItems(startList);
        System.assertEquals('', testResult);
        List<OpportunityLineItem> testLineItems = [Select Id From OpportunityLineItem Where ARC__c = 20];
        System.assertEquals(2, testLineItems.size());
    }

I am both mystified and horrified. I have similar logic in a trigger I am about to push to production and all end-to-end and ui tests have passed but I don't know if I can deploy with this cockroach in the back of my mind.

If someone could provide some insight I would be extremely grateful.

Thanks!