+ Start a Discussion
MeerMeer 

Master Detail insert new record

Hi,

 

I have two custom objects 'Fin_Journal__c' and 'Line__c'  they both have a master detial relationship, I am not able to insert new line.  Can anybody tell me where I am making mistake?

 

public class Fin_LineManager
{
public Line__c newLine{get; set;}
public Fin_Journal__c journal ;

public Fin_LineManager(ApexPages.standardController stdn)
{
newLine = new Line__c();
journal = [SELECT id FROM Fin_Journal__c WHERE id = :ApexPages.currentPage().getParameters().get('id')];
newLine.Journal__c = journal.id;
}

public PageReference save()
{
try
{
insert newline;
}
catch (DMLException e)
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new contact.'));
}
return null;
}
}

 

 

Regards,

 

Meer

Best Answer chosen by Admin (Salesforce Developers) 
MeerMeer

Sorry guys I was calling the save function inappropriately.. Beside I write the constructor as below:

 

public Fin_LineManager(ApexPages.standardController stdn)
    {
       newLine  = new Line__c(Journal__c = ApexPages.currentPage().getParameters().get('id'));
    }

 

//where Name is Auto Number

 

thanks.

 

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

Why are you query the Id from the Fin_Journal__c as you already have the id in

 

ApexPages.currentPage().getParameters().get('id');


You also not enter the name field of Line__c.

 

Try below code snippet as reference:

 

public class Fin_LineManager
{
public Line__c newLine{get; set;}
public Fin_Journal__c journal ;

public Fin_LineManager(ApexPages.standardController stdn)
{
newLine = new Line__c();
string strId=ApexPages.currentPage().getParameters().get('id')
if(strId != null || strId != '')
newLine.Journal__c = journal.id;
newLine.Name='Test';
}

public PageReference save()
{
try
{
insert newline;
}
catch (DMLException e)
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new contact.'));
}
return null;
}
}

 

 

 Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

SRIADISRIADI

If I am not wrong, please replace journal.id with strId at the below lines:


if(strId != null || strId != '')
newLine.Journal__c = journal.id;

newLine.Name='Test';

MeerMeer

@Navatar,  I have tried your snippet as well but its still not working.. I am getting the following error:

 

" java.lang.IllegalArgumentException: Illegal view ID save()?inline=1. The ID must begin with /  "

 

I get your point and yeah you are right there.

 

 

Regards,

 

Meer

 


MeerMeer

Sorry guys I was calling the save function inappropriately.. Beside I write the constructor as below:

 

public Fin_LineManager(ApexPages.standardController stdn)
    {
       newLine  = new Line__c(Journal__c = ApexPages.currentPage().getParameters().get('id'));
    }

 

//where Name is Auto Number

 

thanks.

 

 

This was selected as the best answer