1

I have 2 custom object namely ServiceContract & ServiceRole and 1 standard object Account.In service role object,Account is Master-detail relationship to Account and ServiceContract is also Master-detail relationship to Account.I am writting test class for inserting service role records.I have to change state,Country,street field of service role should be same as Account billing address values i.e country,street,state etc.When i am inserting service role record,it is getting inserted successfully but value of fields Country,State,street as null.While service role has master detail relation ship to Account.I have written a trigger to copy or update values of country,State,Street as address of Account.Trigger is working fine.But in case of test class trigger is not working.Let me know what could the issue.Below is my test class.

static void test_copyAddress2ServiceRole(){ 
            test.startTest();  // Start of Test Exeution
            String strRecordTypeId = [Select Id From RecordType Where SobjectType = 'Account' and Name = 'Person Account'].Id;
            System.debug('value of Account recordtype id ::::: '+strRecordTypeId);
            Account acc1 = new Account(FirstName='testabc',LastName='xyz',recordtypeid=strRecordTypeId, BillingCity ='Chennai', BillingCountry='india',BillingPostalCode='600075', BillingStreet='water well street');
            insert acc1;
            System.debug('value of Account address ::::: '+acc1);
             ServiceContract sc = new ServiceContract();
        sc.Name = 'Test Service Contract';
        sc.Account=acc1;
        sc.Service_Contract_Type__c='Medical Alert Service';
        sc.StartDate = Date.today();
        sc.EndDate = Date.today()+60;
        insert sc; // fires the trigger
        System.debug('value of service contract ::::: '+sc);
        Service_Role__c srvcRole = new Service_Role__c();
        Id accountRecordTypeId = Schema.SObjectType.service_role__c.getRecordTypeInfosByName().get('Payer').getRecordTypeId();
        srvcRole.RecordTypeId = accountRecordTypeId;
        srvcRole.Person_Account__c = acc1.id;
        srvcRole.Service_Agreement__c = sc.id;
        insert srvcRole;
        System.debug('value of service role ::::: '+srvcRole);
        System.debug('value of service role ::::: '+srvcRole.street__c);
        System.debug('value of service role ::::: '+srvcRole.Country__c);
        test.stopTest();

    }

Values of srvcRole.street__c & srvcRole.Country__c is null after insert of srvcRole.And this srvcRole contains account values.

4

When you issue your insert call from Apex code, the trigger fires (as do field updates) and makes changes to the record that is committed to the database but the local copy (in your apex code) would not be expected to be changed. The only field value in your local copy that is affected by an insert is that the record ID is set.

So if you want to see what the value of srvcRole is after your insertion and the trigger has run and any field updates etc. then you need to query it back.

Service_Role__c srvcRole = new Service_Role__c();
Id accountRecordTypeId = Schema.SObjectType.service_role__c.getRecordTypeInfosByName().get('Payer').getRecordTypeId();
srvcRole.RecordTypeId = accountRecordTypeId;
srvcRole.Person_Account__c = acc1.id;
srvcRole.Service_Agreement__c = sc.id;
insert srvcRole;

// Here you need to query the data back from the database, your local variable has only had ID set (not any triggers etc.)
srvcRole = [select street__c, country__c from service_role__c where id = :srvcRole.id];

System.debug('value of service role ::::: '+srvcRole);
System.debug('value of service role ::::: '+srvcRole.street__c);
System.debug('value of service role ::::: '+srvcRole.Country__c);
0

I agree with Dough Only thing that you can retrieve is id of inserted record. All other values will not be available so query same record using id in another variable anthen use values.

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.