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
Uday KUday K 

Code coverage and Exception error

Hi all,

I am getting the 70% coverage in the test class and also am getting the exception error "System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []", "Class.CLS_AccountEditPage.insertdata: line 60, column 1
Class.Test_CLS_AccountEditPage.AccountEditPage: line 36, column 1". The constructor block is not getting covered. Can some one help me.

 

public class CLS_AccountEditPage {
public String Account { get; set; }
Public String CompanyType{get;set;}
Public String AccountName{get;set;}
Public String Industry{get;set;}
Public String AccountSiteCity{get;set;}
Public String Phone{get;set;}
Public String Website{get;set;}
Public String ParentAccount{get;set;}
Public String AccountSiteLocation{get;set;}
public Account acc{get;set;}
public string surl = '';
public id aid{get;set;}
   
public CLS_AccountEditPage ()
{
acc=new account();
if(ApexPages.currentPage().getUrl() != null)
  {
    surl = ApexPages.currentPage().getUrl();
    if(surl.contains('001'))
   {
    aid= ApexPages.currentPage().getParameters().get('id');
    if(aid!=null  && aid!='')
    {
    acc=[select name,Industry,phone,Website,Account_Site_Location__c,Company_Type__c,Account_Site_City__c  from Account where id=:aid];
    AccountName=acc.name;
    Industry=acc.Industry;
    phone=acc.phone;
    Website=acc.Website;
    CompanyType=acc.Company_Type__c;
    AccountSiteCity=acc.Account_Site_City__c;
    AccountSiteLocation=acc.Account_Site_Location__c;
   }
    }
}
}
public  PageReference insertdata()
{
try{
acc.Name=AccountName;
acc.Industry=Industry;
acc.Phone=Phone;
acc.Website=Website;
acc.Company_Type__c=CompanyType;
acc.Account_Site_Location__c=AccountSiteLocation;
acc.Account_Site_City__c=AccountSiteCity;
insert acc;
}
catch(Exception e){
update acc;
}
PageReference pr=new PageReference('/'+acc.id);
return pr;
}
}

Test class;
@isTest
Public class Test_CLS_AccountEditPage
{
  Public static testMethod void AccountEditPage()
  {
    Account acc = new Account();
    acc.Name = 'Uday Test';
    //acc.Account = 'chatter';
    acc.Industry = 'Hotel';
    acc.Phone = '8375069778';
    acc.Website = 'www.google.com';
    acc.Company_Type__c = 'Sparkle Clean';
    acc.Account_Site_Location__c = 'India';
    acc.Account_Site_City__c = 'Delhi';
    insert acc;
    PageReference pageRef = Page.VF_AccountEditPage;
    Test.setCurrentPage(pageRef);
    CLS_AccountEditPage accEdit = new CLS_AccountEditPage();
    accEdit.surl = '001';
    accEdit.insertdata();
     apexpages.currentpage().getparameters().put('id',acc.id);
  }
}


Thanks,

Uday

Vinit_KumarVinit_Kumar

Uday,

 

Two things comes up in my mind:

 

1.) Can you check debug logs and see if the Account record is being inserted in Test class or not.

 

2.) In InsertData() method,in try block you are inserting account record and in catch block you are updating it without doing anything else.To update a record you need to specify the id of the record somethig like below :

 

insert acc;

 

Account acc2 = new Account(id=acc.id,<Other fields if needed>);

update acc2; // Here,I am updating the acc record 

Uday KUday K

Hi Vinit,


Thanks,

 

1. Account Record is inserting.

2. Here i tried to update the record same as you told, but am not able to get out of the same exception.

         Account acc2 = new Account(id=acc.id,<Other fields if needed>);

         update acc2; // Here,I am updating the acc record

I am getting as below in the debug log. I think the record is inserting and updating. But still am getting the Exception

and also please tell me how to cover the highlighted block in red in the test class. I tried in many ways, But am not able to do.

  

Op:Insert|Type:Account|Rows:1
Op:Update|Type:Account|Rows:1