function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PyramidPyramid 

Which Ids are needed in an insert statement?

 

The code to test a trigger given below works.   My question relates to the 4th insert - Insert Test Shipment. Why does this insert need both the con.Id and the sernum.Id.  I try it with just sernum.Id and it fails with the message

 

"System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateShipmentInfo: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updateShipmentInfo: line 18, column 75: []"

 

It seems to me that just like the OLI_Serial_Number insert only requires con.Id and not acc.Id the Shipment insert should only need the sernum.Id.  Shipment is detail to master related to OLI_Serial_Number and lookup related to both Contract and Account.  Similarly, OLI_Serial_Number is detail to master related to Contract and lookup related to Account.

 

I'm trying to learn APEX and this kind of (what looks like) inconsistency is very confusing to me. 

 

Thanks in advance,

 

Doug

 

/*Salesforce.com Extension Doug copied from Serial_Number

This test class is used to test updateShipmentInfo trigger

*/

@isTestprivate class TestupdateShipmentInfo {    
    static testMethod void triggerTest() {                 

        //insert test account

        Account acc=

             new Account(name='Test Account 1.0');

        insert acc;


        //insert test contract

        Contract con=

             new Contract(accountId=acc.Id,next_action__c='none');

        insert con;

 

         //insert test OLI serial number

        OLI_Serial_Number__c sernum =

             new OLI_Serial_Number__c(contract__c=con.Id,name='Test SerNum 1.0');

        insert sernum;

 

         //insert test shipment

        Shipment__c ship =

             new Shipment__c(contract__c=con.Id,OLI_Serial_Number__c=sernum.Id);

        insert ship;

        } 

   }

_Prasu__Prasu_

Problem is in Trigger.updateShipmentInfo and not in the test method code. Could you post code of your trigger?

PyramidPyramid

Prasanna - here' s the code for the trigger updateShipmentInfo

 

/*Salesforce.com Extension Doug copied OLI_Serial_Number Trigger
This trigger is used to populate Account field on Shipment__c custom object
*/
trigger updateShipmentInfo on Shipment__c (before insert) {

    Set<Id> shipIds=new Set<Id>();
    Set<Id> conIds=new Set<Id>();
    
    for(Shipment__c ship:Trigger.new){
        shipIds.add(ship.Id);
        conIds.add(ship.contract__c);
    }
    
    if(shipIds.size() > 0){
    Map<Id,Contract> conMap = new Map<Id,Contract>([select AccountId from contract where id in:conIds]);
    
        for(Shipment__c ship:Trigger.new){
            ship.put(Shipment__c.Account__c, conMap.get(ship.contract__c).accountId);
        }
    }
}

 

Thanks,

 

Doug

rungerrunger

I would guess that there's a ship for which conMap.get(ship.contract__c) returns null.

 

Rich

dmchengdmcheng

The conMap.get requires a value for ship.Contract__c, so if you don't supply one, you get your error.

 

Also - I don't understand how your trigger is working at all since in a before insert trigger, the record IDs haven't been created yet, so ShipIDs will always empty.

 

 

PyramidPyramid

dmcheng,

 

I wish I could give you an intelligent answer to why this trigger works but I can't.  Salesforce developers wrote a trigger for me and  I modified it three times for other similar uses - this one is the third and most complicated modification but it works.  The basic idea is that I have a hierarchy of account - contract - invoice - shipment  where each object is in a detail master realtionship to the object above.  Hiher up objects are in a lookup relationshipp.  For example, shipment is in a detail master relationship with invoice and in a lookup relationship with both account and contract. 

 

When a new shipment record is created the invoice field is automatically populated but the account and contract fields are not.  This trigger automatically populates the account  field when the new record is saved.  This function is successfully accomplished with this trigger.

 

I’ve submitted another post which asks the question how can I also populate the contract field when a new shipment record is created.  Maybe you can help me on that also.

 

Thanks again for your help I’m trying to fully understand what you’ve said.

 

Regards,

 

Doug