1

I was writing the test class and found the below error and i am unable to figure it out ..

System.DmlException: Insert failed. First exception on row 0 with id a0D1h0000003rUREAY; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Apex class

public with sharing class DTNA_CUA_AddNewConcession {

public Concession__c obj_Conc{get;set;}
public Boolean isFCC {get;set;}
public Concession__c objConAsIs;
String rcuResponse='';
String respMessage='';
String rcuFlag='';
String Str_ConcessionNumer='';
String IntId='';
String profileName='';
User Obj_User = new User();
Id userId=null;
Id profileId=null;
public DTNA_CUA_AddNewConcession(ApexPages.StandardController stdController) {

    this.obj_Conc= (Concession__c)stdController.getRecord();
    objConAsIs = new Concession__c();
    //UserRegionCode = 'RG';
    //ConcessionId = ApexPages.currentPage().getParameters().get('id');
    userId = userinfo.getUserId();
    profileId = userinfo.getProfileId();
    profileName=[Select Id,Name from Profile where Id=:profileId].Name;
    List<PermissionSetAssignment> lstcurrentUserPerSet = [ SELECT Id, PermissionSet.Name,AssigneeId FROM PermissionSetAssignment  WHERE AssigneeId = :Userinfo.getUserId() and (PermissionSet.Name = 'ConcLive_CUA_Screen_Access')];
    if(lstcurrentUserPerSet.size() > 0 || profilename == 'System Administrator'|| profilename == 'DTNA - FCCC Sales'|| profilename == 'DTNA - WST DOA'|| profilename == 'DTNA - FTL DOA'){
        isFCC = true;
    }
    Obj_User = [Select Id,Mainframe_ID__c,FederationIdentifier,District__c from User where Id=:userId];

}
public pagereference save(){

    RCURequest('A');
    system.debug('#####before insert'+obj_conc);
    insert obj_Conc;
    system.debug('#####after insert'+obj_conc);
    Pagereference p =  new Pagereference('/apex/DTNAConcessionLive?id='+obj_Conc.Id);                        
    p.setRedirect(true);
    return p;

}

public void RCURequest(String ReqType)
{
    rcuResponse+='RCURequest::' + ReqType;
    IntId = Str_ConcessionNumer + '_' + String.valueof(Math.round(Math.random()*1000));
    iibIms_ConcessionApprovalRequest.ClientInfo_element ClientInfo =
        new iibIms_ConcessionApprovalRequest.ClientInfo_element ();

    ClientInfo.UserID=ConcessionRCURequest__c.getValues('ClientInfoUserID').Value__c;

    iibIms_ConcessionApprovalRequest.Security_element SecurityInfo = 
        new iibIms_ConcessionApprovalRequest.Security_element();

    SecurityInfo.UserID = ConcessionRCURequest__c.getValues('SecurityInfoUserID').Value__c;
    SecurityInfo.Password = ConcessionRCURequest__c.getValues('SecurityInfoPassword').Value__c;

    iibIms_ConcessionApprovalRequest.IMSInfo_element IMSInfo =
        new iibIms_ConcessionApprovalRequest.IMSInfo_element();


    IMSInfo.Security = SecurityInfo;
    IMSInfo.TransactionName='CSCNCUA';
    rcuResponse+='$'+obj_Conc.LEAD_CONC_NO__c;
    rcuResponse+='$'+objConAsIs.LEAD_CONC_NO__c;
    Integer CurrentDealerNetPrice=Integer.valueof(obj_Conc.DLR_NET_PRC__c);
    Integer DealerProfit =Integer.valueof(obj_Conc.EST_DLR_PROF__c);
    //RequiredFields for Adding Concessions  
    IMSInfo.MessageData='    ¢  XX' + ReqType +'¢' + Obj_User.Mainframe_ID__c + '¢'+ Str_ConcessionNumer +'¢¢¢';
    IMSInfo.MessageData += obj_Conc.Dealer_Code__c + '¢';
    IMSInfo.MessageData += obj_Conc.CONT_PRSN_NAME_42__c + '¢';
    IMSInfo.MessageData += obj_Conc.CUST_NAME_50__c + '¢¢¢¢¢¢¢';
    IMSInfo.MessageData += obj_Conc.VEH_MDL_NO__c + '¢';
    IMSInfo.MessageData += obj_Conc.VEH_BASE_MDL_NO__c + '¢';
    IMSInfo.MessageData += obj_Conc.CMPTR_CD__c  + '¢¢¢¢';
    IMSInfo.MessageData += obj_Conc.RQST_MIN_UNITS__c + '¢';
    IMSInfo.MessageData += obj_Conc.RQST_MAX_UNITS__c + '¢';
    IMSInfo.MessageData += CurrentDealerNetPrice + '¢' ;
    IMSInfo.MessageData += obj_Conc.CURR_FREIGHT__c + '¢';
    IMSInfo.MessageData += DealerProfit + '¢';
    IMSInfo.MessageData += obj_Conc.TOT_MIN__c + '¢';     
    IMSInfo.MessageData += obj_Conc.TOT_MAX__c + '¢¢¢'; 
    IMSInfo.MessageData += obj_Conc.RQST_CONC_PCT__c + '¢¢¢¢'; 
    IMSInfo.MessageData += formatToMMDDYY(obj_conc.DATE_DELV_RQST__c) + '¢';
    IMSInfo.MessageData += formatToMMDDYY(obj_conc.DATE_LATEST_ACCEPT__c) + '¢';
    IMSInfo.MessageData += obj_Conc.REQ_PRICELVL__c + '¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢¢EOF'; 

    rcuResponse+=IMSInfo.MessageData;


    iibIms_ConcessionApprovalRequest.ServiceResponseMsg_element ServiceResponse = new iibIms_ConcessionApprovalRequest.ServiceResponseMsg_element();
    iibImsConcessionRequest.IIB_generic_internet_soap11 proxyClass = new iibImsConcessionRequest.IIB_generic_internet_soap11();
    proxyClass.clientCertName_x=ConcessionRCURequest__c.getValues('clientCertName_x').Value__c;
    proxyClass.clientCertPasswd_x=ConcessionRCURequest__c.getValues('clientCertPasswd_x').Value__c;
    system.debug(' proxyvalue '+ proxyClass);

    try
    {
        ServiceResponse = proxyClass.ExecTransaction(ClientInfo,IMSInfo);
        DTNA_IntegrationLogging.IntegrationLoggingReqInsert( IMSInfo.TransactionName,'IMS-Mainframe',(ReqType == ' ') ? 'Approval' : ReqType,IntId, IMSInfo.TransactionName+IMSInfo.MessageData,'Requested');
        System.debug(' serviceresponse______ ' + ServiceResponse );
        obj_Conc.Approval_Comments__c='';
    }
    Catch(Exception e)
    {
        System.debug(e);
        DTNA_IntegrationLogging.IntegrationLoggingReqInsert( IMSInfo.TransactionName,'IMS-Mainframe',(ReqType == 'R') ? 'Rejection' : (ReqType == ' ') ? 'Approval' : 'Update',IntId,IMSInfo.MessageData,'Requested');
        DTNA_IntegrationLogging.IntegrationLoggingRespUpdate(IntId, ServiceResponse + e.getMessage() + e.getStackTraceString(),'SOAP Request Failure');
        rcuResponse=rcuResponse+e+e.getStackTraceString();
        rcuFlag = 'ERROR';
        respMessage = ConcessionRCURequest__c.getValues('ExceptionMessageTemplate').Value__c + ' ' + e.getMessage();
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,respMessage ));
    }
    System.debug(ServiceResponse);

    if(ServiceResponse!= null)
    {
        if(ServiceResponse.ErrorResponse != null)
        {
            DTNA_IntegrationLogging.IntegrationLoggingRespUpdate(IntId, ServiceResponse.ErrorResponse.ErrorDescription,'Responded with ERROR');
            rcuResponse += (ConcessionRCURequest__c.getValues('ErrorResponseMessageTemplate').Value__c + ServiceResponse.ErrorResponse.ErrorDescription);
        }
        else
        {
            try
            {
                if(!Test.isRunningTest())
                {
                    DTNA_IntegrationLogging.IntegrationLoggingRespUpdate(IntId, ServiceResponse.SyncResponse.IMSMessageData,'Response Received');
                }
                else
                {
                    iibIms_ConcessionApprovalRequest.SyncResponse_element ObjSyncResp = new iibIms_ConcessionApprovalRequest.SyncResponse_element();
                    ObjSyncResp.IMSMessageData = '2H7798¢JFTL238 ¢E¢E-570 DATA CHANGED, PRESS ENTER BEFORE SUBMIT OR CHK        ¢EOF';
                    ServiceResponse.SyncResponse = ObjSyncResp;
                }
                respMessage = ServiceResponse.SyncResponse.IMSMessageData;
                rcuResponse += respMessage;
                if(respMessage.split('¢')[3].left(1) == 'I')
                {
                    respMessage = ConcessionRCURequest__c.getValues('InfoSyncResponseMessageTemplate').Value__c +respMessage.split('¢')[3].substring(respMessage.split('¢')[3].indexOf(' ')+1);
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,respMessage ));

                    //   LoadLiveConcessionData();

                }
                else if(respMessage.split('¢')[3].left(1) == 'E')
                {
                    respMessage = ConcessionRCURequest__c.getValues('ErrorSyncResponseMessageTemplate').Value__c + ' ' + respMessage.split('¢')[3].substring(respMessage.split('¢')[3].indexOf(' ')+1);
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,respMessage ));
                    //    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,respMessage ));

                }
                else
                {
                    respMessage = ConcessionRCURequest__c.getValues('OtherSyncResponseMessageTemplate').Value__c + ' ' + respMessage.split('¢')[3].substring(respMessage.split('¢')[3].indexOf(' ')+1);
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,respMessage ));    
                }

                //rcuResponse = ServiceResponse.SyncResponse.IMSMessageData;
                system.debug('######response='+ServiceResponse.SyncResponse.IMSMessageData);
                obj_Conc.Name = ServiceResponse.SyncResponse.IMSMessageData.split('¢')[0];



            }
            catch(Exception e)
            {
                System.debug(e);
                rcuResponse+=rcuResponse+e+e.getStackTraceString();
            }
            //LoadLiveConcessionData();                  
        }
    }
}
//Formatting the date to mmddyy
public string formatToMMDDYY(Date d){    
    string str='';
    if(d.month() < 10){
        str = '0'+d.month();
    }else{
        str = str + d.month();
    }
    if(d.day() < 10){
        str = str + '0'+d.day();
    }else{
        str = str + d.day();
    }
    str = str + math.mod(d.year(), 100);

    system.debug(str);
    return str;
}

//Calculating the approved concession percentage and Requested Concession Percentage 
public void CalcOnChangedConcPct()
{
    obj_Conc.RQST_CONC_PCT__c = obj_Conc.RQST_CONC_PCT__c.setscale(2);
    obj_Conc.RQST_CONC_AMT__c = (obj_Conc.RQST_CONC_PCT__c /100)*obj_Conc.DLR_NET_PRC__C;
    obj_Conc.RQST_CONC_AMT__c = obj_Conc.RQST_CONC_AMT__c.round();
    rcuResponse+= obj_Conc.RQST_CONC_AMT__c;
    CalcTranChanges();
}
public void CalcOnChangedConcAmt()
{
    obj_Conc.RQST_CONC_AMT__c = obj_Conc.RQST_CONC_AMT__c.setscale(0,System.RoundingMode.HALF_UP);
    obj_Conc.RQST_CONC_PCT__c = (obj_Conc.RQST_CONC_AMT__c /obj_Conc.DLR_NET_PRC__C)*100;
    obj_Conc.RQST_CONC_PCT__c = obj_Conc.RQST_CONC_PCT__c.setscale(2);
    rcuResponse+= obj_Conc.RQST_CONC_PCT__c;
    CalcTranChanges();
}

//Calculate the Approved Concession Amount based on the Approved Percentage change
public void CalcOnChangedAprConcPct()
{
    obj_Conc.APPR_CONC_PCT__c = obj_Conc.APPR_CONC_PCT__c.setscale(2);
    obj_Conc.APPR_CONC_AMT__c = ((obj_Conc.APPR_CONC_PCT__c /100)*obj_Conc.DLR_NET_PRC__C).setscale(0,System.RoundingMode.HALF_UP);
    obj_Conc.APPR_CONC_AMT__c = obj_Conc.APPR_CONC_AMT__c.round();
    rcuResponse+= obj_Conc.APPR_CONC_AMT__c;
    CalcTranChanges();
}

//Calculate the Approved Concession Percentage based on the Approved Amount change    
public void CalcOnChangedAprConcAmt()
{
    obj_Conc.APPR_CONC_AMT__c = obj_Conc.APPR_CONC_AMT__c.round();
    obj_Conc.APPR_CONC_PCT__c = (obj_Conc.APPR_CONC_AMT__c /obj_Conc.DLR_NET_PRC__C)*100;
    obj_Conc.APPR_CONC_PCT__c = obj_Conc.APPR_CONC_PCT__c.setscale(2);
    rcuResponse+= obj_Conc.APPR_CONC_PCT__c;
    CalcTranChanges();
}

//Calculate the Transaction calcualted fields with the change in the 
public void CalcTranChanges()
{
    Decimal decConcAmtDiff = 0.0;
    if(obj_Conc.CURR_DN_COST__c != null) obj_Conc.CURR_DN_COST__c-=decConcAmtDiff;
    if(obj_Conc.CURR_SELL_PRC__c != null) obj_Conc.CURR_SELL_PRC__c-=decConcAmtDiff;
    if(obj_Conc.CURR_TOT_SELL_PRC__c != null) obj_Conc.CURR_TOT_SELL_PRC__c-=decConcAmtDiff;
    if(obj_Conc.APPR_NET_CNTRB__c != null) obj_Conc.APPR_NET_CNTRB__c-=decConcAmtDiff;
}


}

Test Class

@isTest(SeeAllData=True)
public class DTNA_CUA_AddNewConcession_Test{
static testMethod void DTNA_RCU_Inquiry_Request_Test()
{

    Concession__c testConcession = new Concession__c(name = 'T12345');
    insert testConcession;
    testConcession = [select Id from concession__C where name ='T12345' limit 1];
    //   testConcession = [select Id from concession__C where name ='2H7795' limit 1];

    PageReference pageRef = Page.DTNAAddNewConcession;
    //pageRef.getParameters().put('id', String.valueOf(Concession__c.Id));
    pageRef.getParameters().put('id', String.valueOf(testConcession.Id));
    Test.setCurrentPage(pageRef);

    ApexPages.StandardController sc = new ApexPages.StandardController(testConcession);
    DTNA_CUA_AddNewConcession testRCUController = new DTNA_CUA_AddNewConcession(sc);

    // This causes a fake response to be generated
    Test.setMock(WebServiceMock.class, new DTNA_RCU_WebServiceMockImpl());

    testRCUController.obj_Conc.LEAD_CONC_NO__c = '5K9079';
    testRCUController.obj_Conc.EST_DLR_PROF__c = 100000;
    testRCUController.obj_Conc.Dealer_Code__c = 'BCFD';
    testRCUController.obj_Conc.CONT_PRSN_NAME_42__c = 'James KM';
    testRCUController.obj_Conc.CUST_NAME_50__c = 'Test Name';
    testRCUController.obj_Conc.VEH_MDL_NO__c = 'PT126SLP';
    testRCUController.obj_Conc.VEH_BASE_MDL_NO__c = 'PT126064S T';
    testRCUController.obj_Conc.CMPTR_CD__c = 'PET';
    testRCUController.obj_Conc.RQST_MIN_UNITS__c = 1;
    testRCUController.obj_Conc.RQST_MAX_UNITS__c = 2;
    testRCUController.obj_Conc.CURR_FREIGHT__c = 300000;
    testRCUController.obj_Conc.DLR_NET_PRC__c = 500000;
    testRCUController.obj_Conc.TOT_MIN__c = 1;
    testRCUController.obj_Conc.TOT_MAX__c = 2;
    testRCUController.obj_Conc.RQST_CONC_PCT__c = 10.00;
    testRCUController.obj_Conc.DATE_DELV_RQST__c = Date.Today();
    testRCUController.obj_Conc.DATE_LATEST_ACCEPT__c = Date.Today();
    testRCUController.obj_Conc.REQ_PRICELVL__c = '18k';
    testRCUController.obj_Conc.CURR_DN_COST__c = 600000;
    testRCUController.obj_Conc.CURR_SELL_PRC__c = 700000;
    testRCUController.obj_Conc.CURR_TOT_SELL_PRC__c = 100000;
    testRCUController.obj_Conc.APPR_NET_CNTRB__c = 2655500;
    testRCUController.obj_Conc.APPR_CONC_PCT__c = 10.00;
    testRCUController.CalcOnChangedConcPct();
    testRCUController.CalcOnChangedConcAmt();
    testRCUController.CalcOnChangedAprConcPct();
    testRCUController.CalcOnChangedAprConcAmt();
    testRCUController.CalcTranChanges();
    testRCUController.save();

}


}
1

On top of David's answer on improving your overall Unit Testing approach, your issue as the exception suggests is because you are trying to insert a record which already has an Id on it.

Your issues are on these lines across your Controller and Test class.

In the Test class, you are creating the record and then setting that to the controller:

Concession__c testConcession = new Concession__c(name = 'T12345');
insert testConcession;
...
ApexPages.StandardController sc = new ApexPages.StandardController(testConcession);

Then in your Controller's constructor, you are getting the record as below, which will return you the record which was set in your Test class, which already has an Id on it.

this.obj_Conc= (Concession__c)stdController.getRecord();

And then you are using the same record to make an insert call in the save() method, thus giving you the exception.

insert obj_Conc;

While not completely aware of your logic here, one of the approaches to resolve this error would be refactor your code, where before performing an insert call, you will need to verify if the record already consists of an Id, else make an update. Something as below:

if(obj_Conc.Id != null) {
    insert obj_Conc;
} else {
    update obj_Conc;
}

But, if you always expect to make an insert call, then you will need to accordingly set your test data and set that in the Controller.

1

Broader Issues

The first thing you're going to hear in this situation is "Stop using seeAllData=true". There's really no point debugging a unit test that uses that assertion; its results are dependent upon the org's data, and it is almost never good practice to use that annotation in new unit tests. Your unit tests should always create their own data.

The second thing you're going to hear is "write assertions". Your unit test is following a lot of very poor practices; one of them is trying to call every method to get code coverage without actually performing any tests of the functionality itself through making assertions about its behavior.

This is fundamentally not a unit test; it is at best a smoke test, which shows only that the code does not crash under a highly restricted set of circumstances.

The Exception Itself.

In this case, it looks like the proximate error is that you do

insert obj_Conc;

in save(), but obj_Conc is an existing record that you source from your standard controller:

this.obj_Conc= (Concession__c)stdController.getRecord();

You may want either update or upsert instead, here, since just like the error says you can't insert an object that already exists and hence has an Id.

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.