+ Start a Discussion
vjaivjai 

Fetching Records into child object from another child object

Hi SF Community,

 

I have four objects PO_Header,PO_Detail,GRN_Header,GRN_Detail.

 

A Master Detail relationship exists between PO_Header and PO_Detail and a second aster Detail relationship exists between GRN_Header,GRN_Detail.

 

Lookup Relations exists between PO_Header,GRN_Header and PO_Detail,GRN_Detail.

 

A GRN(Goods Receipt Note) is based on the PO(Purchase Order)

 

In GRN_Header page when I enter the PO No,The rest of the details from PO_Header like Vendor name,Org_name,PO_date etc.should be auto populated and in the GRN_details a few columns like the items ordered,Qty ordred should also be populated.

 

Can you help me with this.

 

Thanks,

Vjai

 


Navatar_DbSupNavatar_DbSup

Hi,

 

If you are using the salesforce standard page then you have to write trigger on insertion of GRN_Header which bring the records of PO_Header by its ID and update the fields on GRN_Header page and also update fields GRN_details page. You have to make field of text type on GRN_Header page and GRN_details page which will be updated from trigger.

 

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

 

vjaivjai

Thanks Ankit for the suggestion

 

I've tried your suggestion and wrote a trigger,which I am very new to...but it generated an error.I'm not sure what the error means..Can you please help me with this...I'm pasting the trigger code and error message.

 

trigger GRN_HEADER_Trigger on GRN__c (before insert) {
GRN__c TheGRNHEADER = trigger.new[0];
Purchase_Order__c thePO= [Select Business_Unit__c, Vendor_name__c, PO_EXPIRY_DATE__c from Purchase_Order__c where
Name:=TheGRNHEADER.Purchase_Order_No__c];
TheGRNHEADER.Business_Unit__c=thePO.Business_Unit__c;
TheGRNHEADER.Vendor__c=thePO.Vendor_name__c;
TheGRNHEADER.PO_EXPIRY_DATE__c=thePO.PO_EXPIRY_DATE__c;
update theGRNHEADER;
}

 


Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger GRN_HEADER_Trigger caused an unexpected exception, contact your administrator: GRN_HEADER_Trigger: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.GRN_HEADER_Trigger: line 3, column 1

 

 

Thanks again for the support..

 

-VJai

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

trigger GRN_HEADER_Trigger on GRN__c (before insert)

{

                GRN__c TheGRNHEADER = trigger.new[0];

                if(TheGRNHEADER.Purchase_Order_No__c!=null)

                {

                                Purchase_Order__c thePO= [Select Business_Unit__c, Vendor_name__c, PO_EXPIRY_DATE__c from Purchase_Order__c where id:=TheGRNHEADER.Purchase_Order_No__c];

                                TheGRNHEADER.Business_Unit__c=thePO.Business_Unit__c;

                                TheGRNHEADER.Vendor__c=thePO.Vendor_name__c;

                                TheGRNHEADER.PO_EXPIRY_DATE__c=thePO.PO_EXPIRY_DATE__c;

                }

 

}

vjaivjai

Thanks Ankit....

 

It worked fine but for a small change....Donno if it has any relevance..

 

Changed id:=TheGRNHEADER.Purchase_Order_No__c to id=:TheGRNHEADER.Purchase_Order_No__c

 

Thanks Again...

 

-Vjai

 

vjaivjai

Hi Ankit,

Thanks for the help...

 

I wrote a similar Trigger to retrieve the fields of PO_DETAILS into GRN_Details...

 

But Appears like I missed some logic..Can you please look and help me rectify it.

 

I dont have a PO_NO in my GRN_DETAIL which I need to to get from my GRN_NO and PO_Details have a Master Detail relationship with PO_HEADER...

 

trigger GRN_DETAIL_Trigger on GRN_LINE_ITEMS_del__c (before insert) {
GRN_LINE_ITEMS_del__c TheGRNDETAIL = trigger.new[0];
if(GRN_NO__r.Purchase_Order_No__r.Name!=null)
{
PO_LINE_ITEM__c thePLI= [Select Item_Name__c, PO_Qty__c,Item_Unit_Price__c from PO_LINE_ITEM__c where id=: GRN_NO__r.Purchase_Order_No__r.Name ];
TheGRNDETAIL.Item_Name__c=thePLI.Item_Name__c;
TheGRNDETAIL.PO_Qty__c=thePLI.PO_Qty__c;
TheGRNDETAIL.Item_Unit_Price__c=thePLI.Item_Unit_Price__c;

}
}

vjaivjai

Ankit,

I hope we need to use either Lists or for loop since we're retrieving more than one records from the the parent object(PO_DETAILS). to the GRN_DETAILS object....I've written the below trigger code...can you please review and advice as where I'm going wrong...

 

trigger GRN_DETAIL_Trigger on GRN_Details__c (before insert) {
GRN_Details__c TheGRNDETAIL = trigger.new[0];
if(TheGRNDETAIL.PO_NO__c!=null)
{

list <PO_LINE_ITEM__c> polineitems =[Select Item_Name__c, PO_Qty__c,Item_Unit_Price__c from PO_LINE_ITEM__c where id=:TheGRNDETAIL.PO_NO__c];
list <GRN_Details__c> newgrnline = new list <GRN_Details__c> ();

if(polineitems.size()>0)
{
for(PO_LINE_ITEMS pli : polineitems)
{
GRN_Details__c n1=new GRN_Details__c(Item_Name__c=pli.Item_Name__c,PO_Qty__c=pli.PO_Qty__c,Unit_Price__c=pli.Item_Unit_Price__c);
newgrnline.add(n1);
}
}
return n1;
}}

 

Thanks again..

-Vjai

saleforce beesaleforce bee

are you able to get the line items by this Query ?

 

Select Item_Name__c, PO_Qty__c,Item_Unit_Price__c from PO_LINE_ITEM__c where id=:TheGRNDETAIL.PO_NO__c

vjaivjai

Nope...I'm not able to...