[SalesForce] APEX Error on the test case

I am getting the error like this on the test case can any body help me to reslove it

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, quoteUpdateopportunity: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

Trigger.quoteUpdateopportunity: line 10, column 1: []

This is my trigger wrote on quote

trigger quoteUpdateopportunity on quote__c( after insert) {
 List<opportunity> oppToUpdate = new List<opportunity>();

for(quote__c quo : trigger.new){
    if (quo.Quote_Number__c!= Null )
        oppToUpdate.add(new opportunity(id= quo.Opportunity_Name__c, StageName = 'offer'));
}

if (oppToUpdate != null && !oppToUpdate.isEmpty())
    update(oppToUpdate);
}

this is the test class i wrote

@isTest
private class quoteupdateopportunity{
    static testMethod void testquoteupdateopportunity(){
    quote__c qo=new quote__c(Author_Email_New__c='manoj.jena@kvpcorp.com');
       insert qo;

  opportunity op= new opportunity(name='test 002');
  insert op;

op= [SELECT stageName FROM opportunity Where Id = :op.Id];
System.assertEquals ('offer',op.stagename); 
   }   
   }

Help me to find out get riid of this error

Best Answer

Updated your test class. you're inserting the quote object without adding any opportunity Id in the opportunity_name__c field which is causing the error message. I think instead of storing the id of the opporutniy in the quote object you should create a lookup relationship with opportunity

@isTest
private class quoteupdateopportunity{
    static testMethod void testquoteupdateopportunity(){
    opportunity op= new opportunity(name='test 002');
    insert op;
    quote__c qo=new quote__c(Author_Email_New__c='manoj.jena@kvpcorp.com', opportunity_name__c =op.id);
       insert qo;



op= [SELECT stageName FROM opportunity Where Id = :op.Id];
System.assertEquals ('offer',op.stagename); 
   } 

updated your trigger, i.e., if the opporutniy_name__c is null then do not enter the if condition.

trigger quoteUpdateopportunity on quote__c( after insert) {
 List<opportunity> oppToUpdate = new List<opportunity>();

for(quote__c quo : trigger.new){
    if (quo.Quote_Number__c!= Null && quo.Opportunity_Name__c!='' && quo.Opportunity_Name__c!=null )
        oppToUpdate.add(new opportunity(id= (Id)quo.Opportunity_Name__c, StageName = 'offer'));
}

if (oppToUpdate != null && !oppToUpdate.isEmpty())
    update(oppToUpdate);
}  
Related Topic