You need to sign in to do that
Don't have an account?

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...
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...
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
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
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;
}
}
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.
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...
try this