1

Need help

Below is my trigger, basically is trigger is fire up whenever is a record inserted with this 2 lookup field (BMCServiceDesk__FKChange_Request__c) and (BMCServiceDesk__FKtask__c)

trigger Custom_UpdateManifestOrder on BMCServiceDesk__Change_Manifest__c (after insert) {
    List<BMCServiceDesk__Change_Manifest__c> ManifestLcc = new List<BMCServiceDesk__Change_Manifest__c>();
    for(BMCServiceDesk__Change_Manifest__c cm : Trigger.New){

       List<BMCServiceDesk__Change_Request__c> list_CR = [SELECT ID FROM BMCServiceDesk__Change_Request__c WHERE ID=:cm.BMCServiceDesk__FKChange_Request__c and IsDeleted=False ];

        if(list_CR.size() > 0){
        Integer order = [select count() from BMCServiceDesk__Change_Manifest__c  Where BMCServiceDesk__FKChange_Request__c=:cm.BMCServiceDesk__FKChange_Request__c];


        BMCServiceDesk__Change_Manifest__c cmanifest = new BMCServiceDesk__Change_Manifest__c(id = cm.id, BMCServiceDesk__Order__c = order);
        ManifestLcc.add(cmanifest);
        }
    }
    if(ManifestLcc.size()>0)
    update ManifestLcc;
}

Test Class is simple just insert new record on BMCServiceDesk__Change_Manifest__c and inserting (BMCServiceDesk__FKChange_Request__c) and (BMCServiceDesk__FKtask__c) harded

@isTest
public class Test_ChangeManifestSequence 
{
static testMethod void validatetrigger()
{



BMCServiceDesk__Change_Manifest__c chm = new BMCServiceDesk__Change_Manifest__c(BMCServiceDesk__FKTask__c = 'a2FN00000005zvK',BMCServiceDesk__FKChange_Request__c ='a1IN00000038oYq');

insert chm;


}
}

Error Message Below

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, BMCServiceDesk.ChangeRequestTrigger: execution of BeforeUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

(BMCServiceDesk)

: []

  • 1
    There some problems with your functional and test code. 1. You are making soql in loop. Don't do that. 2. You have hardcoded ids in test method. Don't do that. Error is coming from different trigger which is ChangeRequestTrigger please post its code so that we could help. – Kiran Machhewar Dec 13 '15 at 10:04
2

First dont query inside for loop. We know there is 100 soql in single transaction limit. SO if you insert at time 100 records then you will hit the 101 soql error.

So just try to bulkify your code. Pls dont just copy paste try to understand. If there is mistake in my code please correct that.

trigger Custom_UpdateManifestOrder on BMCServiceDesk__Change_Manifest__c (after insert) {
    set<Id> setBMCServiceDeskFKChangeRequest = new set<Id>();
    List<BMCServiceDesk__Change_Manifest__c> ManifestLcc = new List<BMCServiceDesk__Change_Manifest__c>();
    map<Id, Integer> mapBMCServiceDeskFKChangeReqTocount = new map<Id, Integer>();
    for(BMCServiceDesk__Change_Manifest__c cm : Trigger.New)
    {
        if(cm.BMCServiceDesk__FKChange_Request__c != null)
        {
            setBMCServiceDeskFKChangeRequest.add(cm.BMCServiceDesk__FKChange_Request__c);
        }
    }
    for(AggregateResult aggResult: [select BMCServiceDesk__FKChange_Request__c, 
                                           count(Id) total
                                      from BMCServiceDesk__Change_Manifest__c  
                                     Where BMCServiceDesk__FKChange_Request__cIN: setBMCServiceDeskFKChangeRequest 
                                  GROUP BY BMCServiceDesk__FKChange_Request__c])
    {
        mapBMCServiceDeskFKChangeReqTocount.put(aggResult.get('BMCServiceDesk__FKChange_Request__c'), aggResult.get('total'));
    }
    for(BMCServiceDesk__Change_Manifest__c cm : Trigger.New)
    {
        if(cm.BMCServiceDesk__FKChange_Request__c != null && 
                                mapBMCServiceDeskFKChangeReqTocount.containsKey(cm.BMCServiceDesk__FKChange_Request__c))
        {
            ManifestLcc.add(new BMCServiceDesk__Change_Manifest__c(id = cm.id, BMCServiceDesk__Order__c = mapBMCServiceDeskFKChangeReqTocount.get(cm.BMCServiceDesk__FKChange_Request__c)));
        }
    }
    update ManifestLcc;
}

Test Class Related to test you can't use hard coded id here. For unit testing create your dummy records in test class as I commented below and use asserts to check your expected result.

@isTest
public class Test_ChangeManifestSequence 
{
    static testMethod void validatetrigger()
    {
        //first create BMCServiceDesk__FKChange_Request__c records here 

        // then create BMCServiceDesk__FKTask__c records here. 

        // create few BMCServiceDesk__Change_Manifest__c use above created BMCServiceDesk__FKTask__c and BMCServiceDesk__FKChange_Request__c id for BMCServiceDesk__Change_Manifest__c records.
        BMCServiceDesk__Change_Manifest__c chm = new BMCServiceDesk__Change_Manifest__c(BMCServiceDesk__FKTask__c = 'a2FN00000005zvK',BMCServiceDesk__FKChange_Request__c ='a1IN00000038oYq');

        insert chm;

        // after insert use system.assetequals to check you BMCServiceDesk__Order__c equal to above created BMCServiceDesk__Change_Manifest__c records or not

    }
}

Cheers :)

  • 1
    Hi Ratan, You save day Thankyou very much – wai kiat Dec 14 '15 at 2:18
  • @waikiat Most welcome – Ratan Paul Dec 14 '15 at 3:36

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.