0
@isTest
private class MyExt_Test{
  static testMethod void test_save_UseCase1(){
    test.startTest();
    Case case_Obj = new Case(SuppliedName = 'Suppl906', Product__c='TimeSimplicity',SuppliedEmail = '[email protected]', SuppliedPhone = '801-887-6655', SuppliedCompany = 'Suppl588', Subject = 'Subject408', Description = '20', Client_Company_Name__c = 'Clien959');
    Insert case_Obj; 
    test.stopTest();
    List<Case> case_Obj1  =  [SELECT parentId,SuppliedName,product__c,SuppliedEmail,SuppliedPhone,SuppliedCompany,Subject,Description,Client_Company_Name__c,Hname__c from Case];
    System.assertEquals(true,case_Obj1.size()>0);
    MyExt obj01 = new MyExt(new ApexPages.StandardController(case_Obj1[0]));
        attachment a = new attachment();
        blob b = blob.valueof('Test Data');
        a.Name = 'Cat.csv';
        a.body=b;
        a.ParentId = case_obj1[0].id;
        insert a;
    obj01.save();
  }

static testMethod void test_save_UseCase2(){
    test.startTest();
    Case case_Obj9 = new Case(SuppliedName = 'Suppl906', Product__c='TimeSimplicity',SuppliedEmail = '[email protected]', SuppliedPhone = '801-887-6655', SuppliedCompany = 'Suppl588', Subject = 'Subject408', Description = '20', Client_Company_Name__c = 'Clien959');
    Insert case_Obj9; 
    test.stopTest();
    List<Case> case_Obj2  =  [SELECT ParentId,SuppliedName,Product__c,SuppliedEmail,SuppliedPhone,SuppliedCompany,Subject,Description,Client_Company_Name__c,Hname__c from Case];  
    System.assertEquals(true,case_Obj2.size()>0);
    MyExt obj02 = new MyExt(new ApexPages.StandardController(case_Obj2[0]));
        attachment a1 = new attachment();
        blob b1 = blob.valueof('Test Data');
        a1.Name = 'Cat.csv';
        a1.body=b1;
        a1.ParentId = case_obj2[0].id;
        insert a1;
    case_Obj2[0].SuppliedName='Bill Smith';
    case_Obj2[0].SuppliedEmail = '[email protected]';
    case_Obj2[0].SuppliedPhone='801-996-5632';
    case_Obj2[0].SuppliedCompany='Billy joes Crab';
    case_Obj2[0].Subject='we need crabs';
    case_Obj2[0].Description = 'I need more crab!';
    case_Obj2[0].Client_Company_Name__c='Lemon Law Aid';
    case_Obj2[0].Product__c='TimeWorksPlus';
    Update case_Obj2[0];
    obj02.save();
  }
}

What am I doing wrong?

I get an error about not being able to do an insert for an ID. the actual code works fine, it's just this d***n test class that is driving me to drink!

this is what I'm trying to test:

public with sharing class MyExt{
    public case c {get;set;}
    public Id recordid {get; set;}
    public Case record  {get;set;}
    public Attachment myAttachment{get;set;}
    public string fileName{get;set;} 
    public Blob fileBody{get;set;}
    public MyExt(ApexPages.StandardController stdController){
        //When you use the id parameter, the standard controller / extension gets populated automatically
        c= new case();
        this.recordid=stdController.getid();
        c=(case)stdController.getRecord();
        c.OwnerId='00G0P000003XFOk';
        myAttachment = new attachment();
        } 
    public void save() {
        list<Contact> ContactList = new list<Contact>();
        ContactList=[select id from contact where email = :c.SuppliedEmail limit 1];  
        if(c.SuppliedEmail !=null)
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.confirm,'Your case has been created.'));
        if(!ContactList.isEmpty())
        {
            for(Contact Con : ContactList) 
            {
            c.contactid=con.id;
            }
        insert c;
        c.SuppliedCompany=null;
        c.SuppliedPhone=null;
        c.Product__c=null;
        c.Site_ID__c=null;
        c.Client_Company_Name__c=null;
        c.Subject=null;
        c.Description=null;
        c.suppliedname=null;
        c.SuppliedEmail=null;
        if(filename!=null)
            {   
        System.debug('@@@@@fileBody'+fileBody);
        myAttachment  = new Attachment();
        Integer i=0;
        myAttachment.clear();
        myAttachment.Body = fileBody; 
        myAttachment.Name =filename;
        myAttachment.ParentId = c.id;
        insert myAttachment;
            }
        }
        else
        {
        insert c;
        c.SuppliedCompany=null;
        c.SuppliedPhone=null;
        c.Product__c=null;
        c.Site_ID__c=null;
        c.Client_Company_Name__c=null;
        c.Subject=null;
        c.Description=null;
        c.suppliedname=null;
        c.SuppliedEmail=null;
        if(filename!=null)
            {   
        System.debug('@@@@@fileBody'+fileBody);
        myAttachment  = new Attachment();
        Integer i=0;
        myAttachment.clear();
        myAttachment.Body = fileBody; 
        myAttachment.Name =filename;
        myAttachment.ParentId = c.id;
        insert myAttachment;
            }    
        }  
            }   
}

1 Answer 1

0

Your unit test creates a Case

Case case_Obj = new Case(SuppliedName = 'Suppl906', Product__c='TimeSimplicity',SuppliedEmail = '[email protected]', SuppliedPhone = '801-887-6655', SuppliedCompany = 'Suppl588', Subject = 'Subject408', Description = '20', Client_Company_Name__c = 'Clien959');

Insert case_Obj; 

which you pass to a controller instance

MyExt obj01 = new MyExt(new ApexPages.StandardController(case_Obj1[0]));

and then ask to save():

obj01.save();

save() does

    insert c;

but in your controller,

    c=(case)stdController.getRecord();

that is, c is the Case your test class inserted, and which already has an Id.

So that's the proximate cause here. You can't insert an object that's already been inserted.

It looks to me like your code is not meant to take an existing Case, but to work on a new Case. But to be quite honest, it's hard to follow and quite messy, so I'm not sure what your overall intent is. I'd recommend completing Unit Testing on the Lightning Platform to learn best practices in unit testing, and if possible, working with a colleague to get a comprehensive code review, as there's a number of things you can do to streamline and clarify this code.

6
  • Yeah I'm an admin mainly. The test class was made with a tool called test generator. Which doesn't work, or at least I don't know how to make it work. The visual force page is used on a community for people to submit cases. So, yes, it's new cases. Sorry if it looks like I don't know what I'm doing it's because I don't. But we all have to learn at some point. :) Thank you! Commented Jun 11, 2020 at 22:32
  • In that case it's possible you can make it work just by removing the Insert statement in your Test class. Good luck!
    – David Reed
    Commented Jun 11, 2020 at 22:33
  • And hey, if the code works, you've won more than half the battle, so kudos - if you want to dive deeper, check out the canonical-qa tag for more unit test resources.
    – David Reed
    Commented Jun 11, 2020 at 22:34
  • Thanks again for taking the time to help! It's very much appreciated. I would ask other people at work but we are only two admins and no developers here. Thanks! Commented Jun 11, 2020 at 22:40
  • @ShayneHudson If there's a trailblazer group in your locality, you might be able to connect with some developers who could do a code review with you as a newbie dev.
    – David Reed
    Commented Jun 11, 2020 at 22:49

You must log in to answer this question.

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