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
WikWik 

Error in the Test Class

Hi,

I have written a test class for the following trigger:

Trigger:

 

trigger serviceAgreementInactive on Service_Agreement__c(after update){

Set<id> servids = new Set<id>();

List<Equipment__c> eq=new List<Equipment__c>();
for(Service_Agreement__c serv: Trigger.new){

if(serv.status__c == 'Inactive' && serv.Status__c != trigger.oldMap.get(serv.id).Status__c) {

servids.add(serv.id); } }
List<Equipment__c> equipmentsToMakeExpire = [Select id, Contract_Status__c from Equipment__c where Job_Number__c in :servids];

for(Equipment__c e : equipmentsToMakeExpire){

e.Contract_Status__c = 'Under Expired Contract'; eq.add(e); }

if(!eq.isEmpty())

update eq; }

 

 

Test Class:

 

@isTest private class TestServiceAgreementInactive {

 static testMethod void testServiceAgreement() {

    Service_Agreement__c servAggrement1 = new Service_Agreement__c(Account__c = 'Test Service Agreement Account1', Name = 'Test Service Agreement1', CurrencyIsoCode = 'Canadian Dollar', Status__c = 'Active');

  Service_Agreement__c servAggrement2 = new Service_Agreement__c(Account__c = 'Test Service Agreement Account2', Name = 'Test Service Agreement2', CurrencyIsoCode = 'U.S. Dollar', Status__c = 'Active');   

List<Service_Agreement__c> lstServAgreement = new List<Service_Agreement__c>();

  lstServAgreement.add(servAggrement1);

  lstServAgreement.add(servAggrement2);

  insert lstServAgreement;

     Equipment__c equip1 = new Equipment__c(Name = 'Equipment 1', CurrencyIsoCode = 'Canadian Dollar', Job_Number__c = lstServAgreement[0].Id); 

  Equipment__c equip2 = new Equipment__c(Name = 'Equipment 2', CurrencyIsoCode = 'U.S. Dollar', Job_Number__c = lstServAgreement[1].Id);   

 List<Equipment__c> lstEquipment = new List<Equipment__c>();

  lstEquipment.add(equip1);

  lstEquipment.add(equip2);

  insert lstEquipment;  

    lstServAgreement[0].Status__c = 'Inactive';

  lstServAgreement[1].Status__c = 'Inactive';

     update lstServAgreement;

            } }

 

Upon running this in the Dev Console it throws an error as: "System.StringException: Invalid id: Test Service Agreement Account1"

Best Answer chosen by Admin (Salesforce Developers) 
Abhi_TripathiAbhi_Tripathi

If you are attempting to use the values in the Currency picklist field for validation rules, workflow rules, or any integrations which reference these values, the values must be referenced by their three-letter ISO Code only (i.e. "CAD", "ARS", "BRL") and not by the full picklist value as they appear in the salesforce user interface (i.e. "CAD - Canadian Dollar", "ARS - Argentine Peso", "BRL - Brazilian Real").

All Answers

Abhi_TripathiAbhi_Tripathi

Hi, 

In your test class code where you are creating a record of Service Aggrement 

 

You are assignng a string to lookup field of Account

 

Insert a record of Account and then assign that accounts Id on that field like this

 

@isTest

private class TestServiceAgreementInactive {

 

 static testMethod void testServiceAgreement() {

 

//Insert account

Account acc = new Account(Name = 'test');

insert acc;

 

    Service_Agreement__c servAggrement1 = new Service_Agreement__c(Account__c = acc.Id , Name = 'Test Service Agreement1', CurrencyIsoCode = 'Canadian Dollar', Status__c = 'Active');

  Service_Agreement__c servAggrement2 = new Service_Agreement__c(Account__c = acc.Id Name = 'Test Service Agreement2', CurrencyIsoCode = 'U.S. Dollar', Status__c = 'Active');   

List<Service_Agreement__c> lstServAgreement = new List<Service_Agreement__c>();

  lstServAgreement.add(servAggrement1);

  lstServAgreement.add(servAggrement2);

  insert lstServAgreement;

     Equipment__c equip1 = new Equipment__c(Name = 'Equipment 1', CurrencyIsoCode = 'Canadian Dollar', Job_Number__c = lstServAgreement[0].Id); 

  Equipment__c equip2 = new Equipment__c(Name = 'Equipment 2', CurrencyIsoCode = 'U.S. Dollar', Job_Number__c = lstServAgreement[1].Id);   

 List<Equipment__c> lstEquipment = new List<Equipment__c>();

  lstEquipment.add(equip1);

  lstEquipment.add(equip2);

  insert lstEquipment;  

    lstServAgreement[0].Status__c = 'Inactive';

  lstServAgreement[1].Status__c = 'Inactive';

     update lstServAgreement;

            } }

 

hit kudos if this solution helped you

 

WikWik

it is also not accepting the currency value.

Currency is a Picklist.

Abhi_TripathiAbhi_Tripathi

what is your Exact error? 

WikWik

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, Currency ISO Code: invalid currency code: cur: [CurrencyIsoCode]

Abhi_TripathiAbhi_Tripathi

Use API name for CurrencyIsoCode , which should have "__c" 

WikWik

It is a Standard field

Abhi_TripathiAbhi_Tripathi

If you are attempting to use the values in the Currency picklist field for validation rules, workflow rules, or any integrations which reference these values, the values must be referenced by their three-letter ISO Code only (i.e. "CAD", "ARS", "BRL") and not by the full picklist value as they appear in the salesforce user interface (i.e. "CAD - Canadian Dollar", "ARS - Argentine Peso", "BRL - Brazilian Real").

This was selected as the best answer
WikWik

Thanx that worked. but new error

 

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please select equipment Series.: [Series__c]

Abhi_TripathiAbhi_Tripathi

insert some value to that field Series__c

 

Mark that solution as Correct with kudos( star ) for other peoples queries

WikWik

hey thanx.

 

The test ran and that too without error.

How do i check the code covergae?

Abhi_TripathiAbhi_Tripathi

Go to that class for which you have written test class.

On the top of the class you can see the code coverage. but only after running your test class

 

Go gor this post and join my blog its good for test class, apex basics

 

http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html