0

This is my trigger:

trigger contractStatus on Contract (after insert, after update)
{  

    Set<Id> accIds = new Set<Id>();


    List<Account> newAcc = new List<Account>();
    List<Contract> cons = new List<Contract>();
    Id accRecordTypeIdVP = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Vendor-Prospect').getRecordTypeId();
    Id accRecordTypeIdCP = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Client-Prospect').getRecordTypeId();
    Id vendorRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Vendor').getRecordTypeId();
    Id clientRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Client').getRecordTypeId();

    for(Contract c : trigger.new)
    {
        if (c.Status == 'Signed')
            {

               accIds.add(c.accountid);
         }  
    }

    List <Account> accs =  [SELECT id, Name, recordtypeid from Account WHERE Id IN :accIds];

    for(Account acc :accs)
        {

                if(acc.RecordTypeId == accRecordTypeIdVP)
                {
                    acc.RecordTypeId = vendorRecordTypeId;
                }
                else if(acc.RecordTypeId == accRecordTypeIdCP)
                {
                    acc.RecordTypeId = clientRecordTypeId;
                }

                newAcc.add(acc);

            }

        if(newAcc.size() > 0)
        {
            Database.update(newAcc);
        }

}

I have been trying to write a test class but I keep getting various errors. The idea I had would be to create a test Contract and set the status to Signed and the record type to vendor or client prospect. Then I would insert this record and call the trigger to fire? I know I am also supposed to use an assert statement to verify the result but I could use some help. This is my test class so far:

@isTest
private class contractStatusTest
{
    public static List<Account> accountsList;
    public static List<Contract> contractsList;

    /** Test with an existing contract **/
    static testMethod void testWithExistingContract()
    {    
        Contract c = new Contract();
        Test.startTest();

        c.Status = 'Signed';
        //RecordType = 'Vendor-Prospect'));
        c.RecordTypeId = [select Id from RecordType where Name = 'Vendor-Prospect' and SobjectType = 'Contract'].Id;
        insert c;

        contractStatus();
        //insert accountsList;

        // Verification
        //System.assertEquals( , );

        Test.stopTest();
    }


}

1 Answer 1

0

@nik Try this out

@isTest
private class contractStatusTest
{
/** Test with an existing contract **/
static testMethod void testWithExistingContract()
{    
    Id accRecordTypeIdVP = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Vendor-Prospect').getRecordTypeId();
    Id accRecordTypeIdCP = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Client-Prospect').getRecordTypeId();

    List<Account> acc = new List<Account>();

    Account a = new Account();
    a.Name='a';
    a.RecordtypeId = accRecordTypeIdVP;
    acc.add(a);

    Account a1 = new Account();
    a1.Name='a1';
    a1.RecordtypeId = accRecordTypeIdCP ;
    acc.add(a1);

    insert acc;

    Contract c = new Contract ();
    c.AccountId = a.id;
    c.status = 'Signed';

    Test.startTest();
    insert c;
    Test.stopTest();

    Id vendorRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Vendor').getRecordTypeId();

    Account accts = [Select RecordtypeId From Account Where id = :a.id];
    System.assertEquals(accts.RecordtypeId, vendorRecordTypeId);
}
}
6
  • I am getting an error when I run this: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please use international phone format: [Phone] Stack Trace:Class.contractStatusTest.testWithExistingContract: line 22, column 1
    – Nik
    Commented Aug 13, 2015 at 11:54
  • @Nik this is validation error phone is required field and it do have a format mention in validation rule, you need to check as need to add the phone no according to that in test class as i am inserting name. Their may be required fields that you need to check and need to insert when you insert a record in test class. These required fields can not be identify me or any one because we make field required based on the client requirement. I hope you have validation rule set on contact. For checking validation rule you need to go to Build> Contact(expand it)> Validation Rules
    – Anu
    Commented Aug 13, 2015 at 12:07
  • Thank you @Anu for helping but now I am getting this error: System.NullPointerException: Attempt to de-reference a null object at line 28 column 1. That is the line where the c.status value is assigned.
    – Nik
    Commented Aug 13, 2015 at 14:50
  • @Nik i have updated my code try with this one.
    – Anu
    Commented Aug 14, 2015 at 4:29
  • Still have the same error @Anu "System.DmlException: Insert failed. First exception on row 0; first error: INVALID_STATUS, invalid status: []" Stack Trace: "Class.contractStatusTest.testWithExistingContract: line 29, column 1" Could it be because the status field is a picklist?
    – Nik
    Commented Aug 14, 2015 at 8:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .