• Pyramid
  • NEWBIE
  • 0 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 5
    Replies

 

The hierarchy of objects for this problem is Account - Contract - Invoice - Shipment.  
Contract is detail to master with account,
Invoice is detail to master with contract and lookup with account. and
Shipment is detail to master with invoice and lookup with both contract and account.  

The code below  populates the account field on the shipment record when the shipment record is created for the first time.  The problem that I am trying to solve is how to populate the contract field also at the same time that the account field is populated because currently I have to enter the contract number manually even though the shipment record belongs to one and only one contract through the invoice record.

/*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);
        }
    }
}

 

Any help will be much appreciated!

 

Thanks

 

Doug

 

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;

        } 

   }

I have a few long lines of code that I need to wrap onto 2 lines rather than just using one line.  Is there a statement continuation character or something that will let me do that?

 

Thanks,

 

Doug

I've got a report that shows all accounts, all contracts and all invoices but what I want is all accounts, all contracts and only the most recent invoice.  Does anyone know how to do this?  Thanks

I've got a report that shows all accounts, all contracts and all invoices but what I want is all accounts, all contracts and only the most recent invoice.  Does anyone know how to do this?  Thanks

I have a unit test for a trigger which has to create a contact.  LastName is a required field to create a contact.  I use The following statements to create the contact.

 

 

 Contact con= new Contact(accountId=acc.Id,LastName='Test1');
        insert con;       

 

In the sandbox this statement works fine.  But after deploying to the production version when the test is run it fails with the following message:

 

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName]", Failure Stack Trace: "Class.TestupdateEvent_HistoryInfo.triggerTest: line 15, column 9 External entry point"

 

Here's hoping someone can guide me out of this forest to a happy resolution

 

Thanks

 

Doug

 

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;

        } 

   }

I've got a report that shows all accounts, all contracts and all invoices but what I want is all accounts, all contracts and only the most recent invoice.  Does anyone know how to do this?  Thanks

I've got a report that shows all accounts, all contracts and all invoices but what I want is all accounts, all contracts and only the most recent invoice.  Does anyone know how to do this?  Thanks

I have a unit test for a trigger which has to create a contact.  LastName is a required field to create a contact.  I use The following statements to create the contact.

 

 

 Contact con= new Contact(accountId=acc.Id,LastName='Test1');
        insert con;       

 

In the sandbox this statement works fine.  But after deploying to the production version when the test is run it fails with the following message:

 

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName]", Failure Stack Trace: "Class.TestupdateEvent_HistoryInfo.triggerTest: line 15, column 9 External entry point"

 

Here's hoping someone can guide me out of this forest to a happy resolution

 

Thanks

 

Doug