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
iyappan kandasamy 4iyappan kandasamy 4 

test class for my code

Hi ,

This is my test class for insert a record

Parent Object :Album__c
Child object :Track__c(which has loopup relation with Album__C)

I have written the class and the test class for inserting a record in the child object...but both are giving me error....

In main program
----------------------

public class instrack 
{
track__c al = new track__c(); 
public track__c merch(String name,string name1)  
{
al.Name=name;                             
al.Album__c=name1;
insert al;                 
system.debug('the inserted record is:'+name);
return al;
}
}

Executed as :

instrack mc=new instrack();
mc.merch('Container','GullyBoy');

Error as: Method does not exist  or incorrect signature :void merch(string) from the type instrack....

Test class
-------------
@istest
public class instracktest
{
static testmethod void testmerch()
{
track__c al = new track__c(Name='Surgical'); 
 insert al;   
 Test.startTest();
         al = new track__c (Name = 'Surgical',Album__c='GullyBoy');
         Database.SaveResult result = Database.insert(al, false);
         System.assert(result.getErrors()[0].getMessage().contains('Track already exist with this'));
        Test.stopTest();              

}
}

Error as : System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Album__c]: [Album__c]

Kindly help out on the issue..Thanks in advance...
Chris SalgadoChris Salgado
Hello iyappan,

The return type of merch() is track__c.
Either change it to Void or assign the decleration "mc.merch('Container','GullyBoy')" to a track__c variable.

Thanks,
Chris
iyappan kandasamy 4iyappan kandasamy 4
Actually Im trying to insert the record in the child object which is having the MD-relation with parent...

I changed the code 
public class instrack 
{
track__c al = new track__c(); 
public void merch1(string name,string name1)
{
al.Name=name;                             
al.Album__c=name1;
insert al;                 
system.debug('the inserted record is:'+name);
return al;
}
}

but after executing the code I'm getting the error as below 
The error is : System.string exception:Invalid id:GullyBoy

kindly help out...thanks
iyappan kandasamy 4iyappan kandasamy 4
Hi...I have update the below comments.. kindly help out .. thanks Sent from Yahoo Mail on Android
Martha VMartha V
your variable in the merch method name1 needs to be the id of a record in Album__c  'GullyBoy' is not a record id.
Martha VMartha V
you can query for the record id of the Album__c record:

id AlbumId = [select id FROM Album__c WHERE Name = :name1 LIMIT 1];
and then change your method

public class instrack 
{
track__c al = new track__c(); 
public void merch1(string name,string name1)
{
al.Name=name; 
Album__c album = [select id FROM Album__c WHERE Name = :name1 LIMIT 1];                           
al.Album__c=album.Id;
insert al;             
system.debug('the inserted record is: '+ al.name + ' with an ID of ' + al.Id );
//  if the method is not returning a value, you don't need the return clause at the end.
//return al;
}
}
Abdul KhatriAbdul Khatri
HI Iyappan,

Please don't take it wrong but I see a fundamental conceptual issue from how to work in Apex World where bulkify your code is a mandatory. Can you please clarify what is the trigger context you are trying to achieve your requirement. You mentioned the relationship between the two custom object is MD, but you are mentioning lookup relationship.

Please share the answer and if possible the schema.

Fixing the above code will no do any good in a long run. 

Thanks.
Iyappan Kandasamy 8Iyappan Kandasamy 8
Thanks Martha Vance...for your code...It is working fine...Thanks Abdul Khatri...It is MD - Relation only...
But the thing is I have written the Test class for my code...which is not showing as 100%...but there is no error...it is showing as completed.

My code
public class instrack 
{
    public string name;
    public string name1;
track__c al = new track__c(); 
public void merch1(string name,string name1)  
{
al.Name=name;                             
al.Album__c=name1;
insert al;                 
system.debug('the inserted record is:'+name);
//return al;
}
}

The test class for the above code:

@isTest
private class testmerch11
{
   @isTest static void merch1() 
   {
track__c al1 = new track__c(Name='Marketing',Album__c='a037F00001OAzws'); 
//al1.Name=name;                            
insert al1;
System.assertEquals('Marketing', al1.Name);
}
}


Kindly help out on the issue..Thanks a lot for all the input given...Thanks...
Martha VMartha V
Your test is not testing the class or the method. You need to insantiate the class you are testing in the test and after inserting the Track__c record you must query the object to see if the record is there. However, because this is a MD relationship, you need to first insert an Album__c record, once you insert that then you can use the id to create the Track__c record.

try this
@isTest
private class testmerch11
{
   @isTest static void test_merch1() {
       //insert new album record
        Album__c = new Album__c(Name = 'album name');
        insert Album__c;
        //instantiate class to test
        instrack myTrack = new instrack();
        //call method to test with values needed
        myTrack.merch1('Marketing','album name');
        //retrieve the record you just inserted to check that it was inserted correctly
        //test classes don't see any records that have not been created in this class, so the track__c object only has one record
        track__c testAl = [select Name, Album__c from track__c Limit 1];  
        System.assertEquals('Marketing', testAl.Name);
    }
}
iyappan kandasamy 4iyappan kandasamy 4
Great thanks Martha Vance...sure will try.. thanks Sent from Yahoo Mail on Android