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
Abhishek Sharma 527Abhishek Sharma 527 

create look up relationship on child object

Hello there, I was given a task to -
i)create custom object invoice with field total amount
ii)create child object invoice line item with lookup relationship to invoice
iii)add price field into invoice line item

I have created 'invoice' object and field 'total amount'
now how to create lookup relation on child object
I'm confused whether we can create relationship on object, it can be created on fields only as I know or maybe something I don't know on this.
can anyone plz guide.
Best Answer chosen by Abhishek Sharma 527
AnkaiahAnkaiah (Salesforce Developers) 
Hi Abhishek,

You need to insert the id for the lookup fields.
//my code -
Invoice__c in = new invoice__c();
in.Name ='Hardware shop';
in.Price__c = 100;
insert in;
Invoice_Line_Item__c il = new Invoice_Line_Item__c(

    Name = 'Entry 1',
    Invoice__c = in.id
);
insert il;


or
 
//my code -
 
List<Invoice__c > invlist = [select id from Invoice__c  where Name='Hardware shop'];
Invoice_Line_Item__c il = new Invoice_Line_Item__c(

    Name = 'Entry 1',
    Invoice__c = invlist [0].id;
);
insert il;


If this helps, please mark it as best answer.

Thanks!!
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Abhishek,

1.Create another object called  invoice line item.
2. create price field.
3. create a lookup field, which is lookup to Invoice object.

If this helps, Please mark it as best answer.

Thanks!!
 
mukesh guptamukesh gupta
Hi Abhishek,

Now you need to create a new custom object InvoiceLineItem, and after creation this create a lookup type field and link with invoice object.

Now Invoice object will be parent and InvoiceLineItem will be child.

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Abhishek Sharma 527Abhishek Sharma 527
Thanks for reply Ankaiah and Mukesh, I created lookup relation and gave it name as Invoice (default)
now, when i'm inserting record using anonymous window with this code, it showing error  - System.StringException: Invalid id: Hardware shop

//my code -
Invoice_Line_Item__c il = new Invoice_Line_Item__c(

    Name = 'Entry 1',
    Invoice__c = 'Hardware shop'
);
insert il;

this is my invoice line item page (I can add records from here but I was trying to do from anonymous window, so can you plz guide )

User-added image
AnkaiahAnkaiah (Salesforce Developers) 
Hi Abhishek,

You need to insert the id for the lookup fields.
//my code -
Invoice__c in = new invoice__c();
in.Name ='Hardware shop';
in.Price__c = 100;
insert in;
Invoice_Line_Item__c il = new Invoice_Line_Item__c(

    Name = 'Entry 1',
    Invoice__c = in.id
);
insert il;


or
 
//my code -
 
List<Invoice__c > invlist = [select id from Invoice__c  where Name='Hardware shop'];
Invoice_Line_Item__c il = new Invoice_Line_Item__c(

    Name = 'Entry 1',
    Invoice__c = invlist [0].id;
);
insert il;


If this helps, please mark it as best answer.

Thanks!!
 
This was selected as the best answer