0

I am getting the following error:

OpptypeLogTrig: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.OpptypeLogTrig: line 56, column 1

I have been talking to Salesforce for over an hour and they haven't resolved, is anyone able to see the error/resolution

trigger OpptypeLogTrig on Opportunity (Before Update ) 
{

    List<Opportunity> oppLstToUpdate=new List<Opportunity>();
    set<String> thirdSet = new Set<String>();
    Set<String> LineItemSet = new Set<String>();




    //Declare ID Variables

    Set<Id> Accountids = new Set<Id>();
    Set<id> Oppsid = new Set<Id>();

    //Declare sets

    list<Asset> ProductFamiliesAssests = [Select Asset_family__C from Asset where accountid in :Accountids];
    List<OpportunityLineItem> ProductFamiliesOpps = [Select Asset_family__C from OpportunityLineItem where opportunityid in :oppsid];


    for (Asset Asst : ProductFamiliesAssests) 
    {
        IF(asst.Asset_Family__c != Null)
        {
            thirdSet.add(Asst.Asset_family__C );
        }
    }

    for (OpportunityLineItem Oppline : ProductFamiliesopps)
    {
        If(oppline.asset_family__c != Null)
        {
            LineItemSet.add(Oppline.Asset_family__C );
        }
    }

    //Add OppAssetFamily and AssetassetFamily to set


    //Keep Matches in thirdSet

    thirdSet.retainAll(LineItemSet);

    //Add opp and account ids into set for checking later

    for(Opportunity Opplist1 : Trigger.new)
    {
        if(OppList1.id != null){

            Accountids.add(opplist1.accountid);
            oppsid.add(Opplist1.id);

            List<Opportunity> opp = [SELECT Id,type, account.First_Customer_Date__c, closedate from Opportunity Where Id =: Opplist1.Id];

//line 56              IF(opplist1.closedate < opplist1.account.First_Customer_Date__c + 180)

        {
            opplist1.type = 'New Business';
        }


            Else if (!thirdSet.isEmpty()){

                 opplist1.type = 'Upsell';

                }
        Else{

             opplist1.type = 'Cross-Sell';


        }
            oppLstToUpdate.add(opplist1);
        }


            }

    update oppLstToUpdate;

}
  • 3
    Which line is 56? – Adrian Larson Apr 25 '17 at 14:36
  • 2
    Expanding on Adrian's comment, if you're asking for help, try to make it as easy as possible for people to help you. Code blocks here don't have line numbers, and most people won't be able to pick out line 56 without counting. It takes a non-insignificant amount of time to count to 56. If someone came and removed the extraneous blank lines from your trigger, that would also potentially alter the line count (making it even harder for people to help). A comment on the line before saying something like // The line below is line 56, the one the error is on would be immensely helpful. – Derek F Apr 25 '17 at 15:02
  • IF(opplist1.closedate < opplist1.account.First_Customer_Date__c + 180) – user37980 Apr 25 '17 at 15:05
  • There are some fundamental errors here. I would suggest you look into reading how to write triggers here – willard Apr 25 '17 at 15:23
0

You have used following SOQL query inside for loop which is bad practice.

List<Opportunity> opp = [SELECT Id,type, account.First_Customer_Date__c, closedate from Opportunity Where Id =: Opplist1.Id];

Hopefully, line 56 is this line and opplist1.account could be null.

IF(opplist1.closedate < opplist1.account.First_Customer_Date__c + 180)

Before using account attribute like account.First_Customer_Date__c, you must query that.

You have queried the record and stored that into List<Opportunity> opp, but you haven't used that.

You need bulkify the code removing SOQL query out of for loop and then try to fix this NullPointerException.

| improve this answer | |
  • Hi Santanu, Salesforce added in this list. im not sure where to place it or how to get that account.First_Customer_Date__c into the loop. Thank you – user37980 Apr 25 '17 at 15:10
  • 1
    Also, you will not get nullpointer, but List<OpportunityLineItem> ProductFamiliesOpps will always be empty. as Set<id> Oppsid = new Set<Id>(); is empty. Try to revisit entire code. – Santanu Boral Apr 25 '17 at 15:15
0

It appears to me that your issues are likely caused by your declarations for your sets at the beginning of your trigger which call queries that are based on the accountIds from trigger.new which you don't obtain until much further down in your code at for(Opportunity Opplist1 : Trigger.new).

I'm specifically speaking of these two lines which won't return any results:

list<Asset> ProductFamiliesAssests = [Select Asset_family__C from Asset where accountid in :Accountids];
List<OpportunityLineItem> ProductFamiliesOpps = [Select Asset_family__C from OpportunityLineItem where opportunityid in :oppsid];
| improve this answer | |

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.