0

In my simple use case I am unable to get code coverage for Account and URL fields any suggestions. In the URL field I am appending string what I get from the form to the URL field. Here's the code for my apex class and test class::

public without sharing class Create_Deal{
   public static string saveRecord(Deal__c itemd){
        string url = 'https://www.amazon.com/s/p-';
        map<string,string> info = new map<string,string>();
        List<Account> results = [SELECT id FROM Account WHERE Account_ID__c = : itemd.Seller_ID__c];
        if (results.size() != 0){
            itemd.Account__c  = results.get(0).id;
            itemd.SPM_ID__c  = url + itemd.Sears_Item_SPM_or_A__c; 
            try{  
               insert itemd;
               //return itemd.id;
               info.put('status','success');
               info.put('message',itemd.id);
            } catch(exception e) { 
               system.debug('-------getMessage-----------'+e.getMessage());
            }

TEST CLASS::

@isTest
private class Create_Deal_Test{
   static testMethod void test_saveRecord_UseCase1(){
      Account itdAct = new Account() ;
      itdAct.Name = '123Cycles';
      itdAct.Account_ID__c = '12332';
      insert itdAct;
      Deal__c itmd = new Item_Deal__c(Seller_ID__c='1234',Email__c = '[email protected]', price__c = 23211, Category__c = 'vert', Item_Description__c = 'Itemdescription' );
      itmd.Account__c = itdAct.Id;
      itmd.SPM_ID__c = 'www.amazon.com';
      insert itmd;
      Create_Deal.saveRecord(itmd);
   }
}
2
  • I edited your post to clean up the code formatting, and there are 3 closing braces (}) that seem to be left off the end of your first class. They should close out the if statement, the saveRecord method, and the class itself. I am assuming they are actually in your original code, and they got lost in a copy/paste into the question. However, I don't like to add things like that when editing a question - just in case they were left out of your original code and are the actual cause of the problem. If it was just a typo/paste issue, you might want to edit your question to add them.
    – Moonpie
    Commented Aug 17, 2021 at 11:51
  • Thank you! Moonpie.
    – Laxman
    Commented Aug 17, 2021 at 11:53

1 Answer 1

2

In your case, during executing test class this query [SELECT id FROM Account WHERE Account_ID__c = : itemd.Seller_ID__c] returns nothing.

In order to cover these lines:

itemd.Account__c  = results.get(0).id;
itemd.SPM_ID__c  = url + itemd.Sears_Item_SPM_or_A__c;
...

You need to assign Seller_ID__c for your Deal with the same value as for Account_ID__c.

Therefore, your test method should look something like this:

// Assign same seller id for Account and Deal__c
String sellerId = '1234';

Account itdAct = new Account(
    Name = '123Cycles',
    Account_ID__c = sellerId
);
insert itdAct;

Deal__c itmd = new Item_Deal__c(
    Seller_ID__c = sellerId,
    Email__c = '[email protected]',
    price__c = 23211,
    Category__c = 'vert',
    Item_Description__c = 'Itemdescription',
    Account__c = itdAct.Id,
    SPM_ID__c = 'www.amazon.com'
);
insert itmd;

Create_Deal.saveRecord(itmd);

But, to be honest, I don't understand why you need Account_ID__c field since you can just use relationship to the account in the query:

[SELECT id FROM Account WHERE Id = : itemd.Account__c]
1
  • Thank you!! for the quick reply and help, your suggestion helpful for me and increases my code coverage. Account_ID__c is a custom field which I am comparing with the custom field Seller_ID__c under Deal__c object. Whenever there is a new Deal it automatically searches for seller Id in the database and auto populate Account field under Deal__c object.
    – Laxman
    Commented Aug 17, 2021 at 12:10

You must log in to answer this question.

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