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
DivyaReddyDivyaReddy 

FIELD_INTEGRITY_EXCEPTION exception

Hi to all ,

 

I am stuck well writting testcase . i am getting error as :

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Converted Account empty for a Converted Lead.: [ConvertedAccountId].

Here i am posting my triiger & associate test cases.

 

trigger counsellingNote_Required on Lead (before Update)
{
    If(Trigger.New.size()==1)
    {   
        for(Lead newLead: Trigger.new)
        {  
            If(Trigger.new[0].IsConverted == true && Trigger.old[0].IsConverted == false) // Code not covering from here
            {
                List<Counselling_Note_c__c> counsellingNoteList = new List<Counselling_Note_c__c>
        ([SELECT Prospect_Name__c FROM Counselling_Note_c__c WHERE Prospect_Name__c = :newLead.Id]);
                String errorMsg='';
                Boolean flag = false;
            if(counsellingNoteList.size()==0)
                {
                    flag = true;
                    errorMsg += 'Counselling Note - ';
                 }
                 if(flag && !Test.isRunningTest())
                    newLead.addError('Following fields are empty--'+ errorMsg);
                   
            }
        }


==============

 

@isTest
private class counsellingNote_Required_TESTS
{
    static testMethod void mytestclass()
    {
     Account a = new Account();
     a.Name = 'Test Account';  
     a.PersonLeadSource = '';     
     insert a;
   
   
    Lead l = new Lead ();
    l.LastName = 'raja';
    l.Status = 'Open';
    l.LeadSource = 'Sms';
    l.YRN_Branch__c = 'Andheri West, Mumbai - W10';
    l.YRN_Country__c = 'India';
    l.Type_of_Enquiry__c = 'Telephone';
    l.MobilePhone = '9440736154' ;
    l.Mobile__c = '9550412970';
    l.Phone = '9550412970';
    l.IsConverted = true;
   // l.ConvertedAccountId = a.id;
     insert l;
 
    Lead l1 = new Lead ();
    l1.LastName = 'raja';
    l1.Status = 'Open';
    l1.LeadSource = 'Sms';
    l1.YRN_Branch__c = 'Andheri West, Mumbai - W10';
    l1.YRN_Country__c = 'India';
    l1.Type_of_Enquiry__c = 'Telephone';
    l1.MobilePhone = '9440736154' ;
    l1.Mobile__c = '9550412971';
    l1.Phone = '9550412971';
    l1.IsConverted = false;
    insert l1;
  

   
    Counselling_Note_c__c cn = new Counselling_Note_c__c();
    cn.Prospect_Name__c = l.id;
    cn.Occupation__c  = 'Accountant';
    cn.Your_Opportunities_exist_in__c = 'USA';
    cn.Option__c = 'Migrate';
    cn.Country__c = 'Australia';
    cn.Services__c = 'Premium Membership';
    cn.Recommended_Service_And_Process_Flow__c = 'Test';
    cn.Critical_Documents_English_Requirement__c = 'test';
    cn.Funds_Requirements__c = 'raja';
    cn.Y_Axis_Process_Time__c= '10';
    cn.Immigration_Visa_Process_Time__c = 'true';
    cn.Y_Axis_Fee__c = '2500';
    cn.Migration_Visa_Fee__c = '2500';
    cn.Miscellaneous_Cost__c = '2500';
    cn.Rejection_Percentage__c = 9;
    cn.Immigration_Rules_Change_Percentage__c = 9;
    cn.Next_Action_Item__c = 'Pay for Technical Evaluation';
    cn.Date__c = system.today();
    insert cn;
    cn.Prospect_Name__c = l1.id;
    update cn;
   
    }
}    


ashish raiashish rai

Hello,

       You have to update the lead. Make sure that you have to update lead field IsConveted with value true.So you have to insert a lead with this field value as false. Now simply update this field it will cover that part. Like this:

 

Lead ld=new lead(IsConverted=false,................other fields);

insert ld:

id.ConvertedAccountId = a.id;

id.Isconverted=True;

update ld;

 

 

DivyaReddyDivyaReddy

i modified mt test class like this & i am getting error as

1. System.SObjectException: Field is not writeable: Lead.IsConverted

2.Class.counsellingNote_Required_TESTS.mytestclass: line 11, column 1 External entry point


@isTest
private class counsellingNote_Required_TESTS
{
    static testMethod void mytestclass()
    {
     
     Lead ld=new lead(IsConverted=false,LastName = 'raja',Status = 'Open',LeadSource = 'Sms',YRN_Branch__c = 'Andheri West, Mumbai - W10',
     YRN_Country__c = 'India',Type_of_Enquiry__c = 'Telephone',MobilePhone = '9440736154',Mobile__c = '9550412970',
     Phone = '9550412970');
insert ld;
ld.Isconverted=True;
update ld;
  
    }
}

ashish raiashish rai

Wel insert that lead without IsConveted field.May be this will remove that error.

sekharasekhara

then code will not convert na.

ashish raiashish rai

Hello,

   Use the below code:

@isTestprivate class counsellingNote_Required_TESTS

{   

static testMethod void mytestclass()   

{     

Account a = new Account();     

a.Name = 'Test Account';         

insert a;   

Lead le=new Lead(Email='svreddych@gmail.com', Company='Saksoft',LastName='reddy',Vendor_Business_Contact__c='ass', Contact_Phone__c=111);     

insert le;     List<Id> idList = new List<Id>();   

 idList.add(le.Id);     System.debug(idList);   

Database.LeadConvert[] leadsToConvert = new Database.LeadConvert[0];       

 Database.LeadConvert converter = new Database.LeadConvert();         

for (Id l : idList )

{             

converter.setLeadId(l);                //converter.setLeadId(l.id);                System.debug(1);               

account account = new account(name='test');               

insert account;               

converter.setAccountId(account.id);               

contact contact = new contact(firstname='test', lastname='test', accountid=account.id);               

insert contact;               

converter.setContactId(contact.id);                //converter.setOwnerId('00570000000oflK');                converter.setConvertedStatus('Closed - Converted');               

converter.setDoNotCreateOpportunity( true );               

leadsToConvert.add( converter );         

}       

 Database.ConvertLead( leadsToConvert, true );            

}    

}     

 

Think this will help you.

Sunil PalSunil Pal

Thanks for the information, I am also facing the same problem ,but now it is solved by this method.