I am trying to set the created case account to the opportunitylineitem.opportunity.account and the opportunity__c (lookup on opportunity) to the opportunitylineitem.opportunity. I am getting a null-pointer exception. I have tried checking if the list is not empty as well as try-catch exception. Please help. Code:

trigger eOneCaseCreate on OpportunityLineItem (after insert, after update) {

List<opportunitylineitem>opps=[Select id, Opportunity.Name, Opportunity.Description, opportunity.account.accountnumber, Opportunityid, Implementation_Requires_Master_Ticket__c, opportunity.accountid from opportunitylineitem where id in: Trigger.new];

For(OpportunityLineItem opp:opps)

    //System.debug(logginglevel.DEBUG,'Step 3'+act.RecordTypeID); 

    if(opp.Implementation_Requires_Master_Ticket__c == true) {

Case cse = new Case();

        cse.RecordTypeId = '012g0000000DG74';
        cse.status = 'In Queue';
        cse.Sub_Status__c = 'Awaiting Contract Processing';
        cse.origin = 'Contract';
        cse.priority = 'P3- Medium';

        try {

           cse.opportunity__r.id = opp.opportunityid;
           cse.account.id = opp.opportunity.accountid; // Causes a   NullPointerException
        } catch(NullPointerException npe) {
           System.debug('The following exception has occurred: ' + npe.getMessage());

        if(!opps.isEmpty()) {
            cse.opportunity__r.id = opp.opportunityid;
            cse.account.id = opp.opportunity.accountid;}
        cse.subject = opp.opportunity.name;
        cse.description = opp.opportunity.description;
        //cse.Partner_Account__c = opp.opportunity.Partner_Account__c;

            if(!opps.isEmpty()) { 
           cse.type ='New Account Services';

      else {cse.type = 'Add Service';

        insert cse;

  • Is your RecordTypeID Correct? ... You shouldn't be doing it that way though – EricSSH Jun 26 '15 at 15:56
  • Not to mention that you're doing the insert inside of a For loop rather than adding cse to a list, then checking to see if it's empty() before doing the insert which is why you could be inserting a null cse that you've just declared as new. – crmprogdev Jun 26 '15 at 15:58
  • Try to assign to cse.AccountId instead of to cse.Account.Id – Vigneshwaran G Jun 26 '15 at 16:57
  • I made changes as suggested but still having same error – Zane Jul 13 '15 at 23:17
  • List<case> newopplist = new list<case>(); For(OpportunityLineItem opp:opps) { if(opp.Implementation_Requires_Master_Ticket__c == true) { Case cse = new Case(); cse.opportunity__r.id = opp.opportunityid; } newopplist.add(cse); } } if(newopplist.isEmpty()) { insert newopplist; } //excerpt of changes. issue is still with cse.opportunity__r.id = opp.opportunityid – Zane Jul 13 '15 at 23:27

Had a typo with the api field name. Its working fine. Just adding it to my learning pains.

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.