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
BennettTranBennettTran 

Button to pull data from fields in parent case

Hello,

I am new to APEX and was hoping someone could help me.  I am looking to create a class for a button on the case object.  I would like the button to be placed on child cases and retrieve data from certain fields from the parent case.  

I recently created a class for a button on the parent case that will create new child cases and pre-populate certain fields but am not sure how to reverse-engineer my solution for the use case described above.

Can someone please help me write this class or help me write the foundation?

Thanks in advanced.
Best Answer chosen by BennettTran
Vinit_KumarVinit_Kumar
I have already incorated that it would only fire when an user selects Parent case.Now,I have updated it to run only for Record Type= Integration.

Try below code :-

trigger populateChildcase(before update)

List<Id> parentCaseIds = new List<Id>();

RecordType rt = [select id from RecordType where DeveloperName='Integration' and SobjectType='Case'];

for(Case c : Trigger.new)
{
if(c.RecordTypeId==rt.id)
	{
		parentCaseIds.add(c.ParentId);
	}
}

//Query the parentcase Values,yoou need to query all fields which you want to be populated in yiur child case
Map<Id,Case> caseMap =new Map<Id,Case> ([select id,CaseNumber,Origin from Case where Id in:parentCaseIds]);

//populate the child case with related Parentcase values.

for(Case c : Trigger.new)
{
	if(!caseMap.IsEmpty() && c.RecordTypeId==rt.id)
	{
		c.Origin = caseMap.get(c.ParentId).Origin;
	}
}


All Answers

Vinit_KumarVinit_Kumar
You can query your parent case details based on ParentId and then populate your child case.Go through below sample code

//Query the parentcase Values
Case parentCase = [select id,CaseNumber,Origin from Case where parentId=<Id of parentCase> limit 1};

then,populate the child case 

childCase.Origin = parentcase.Origin;
update childcase;

If this helps,please mark it as best answer to help others :)
Karan Khanna 6Karan Khanna 6
Do you need fields to be populated on child case creation page? or it will be fine if fields are populated after hitting Save on child case?
Dane KimDane Kim
Just a quick thought. Having no real detail on what you're trying to accomplish, I'm not sure if this is applicable, but one thing that I've been successful with is to use a trigger on parent objects to update all related child objects. This would eliminate the need for a button to update...
BennettTranBennettTran
Sorry if I was not clear.  There will be two cases created to begin with that are not associate to each other.  Then one of our user will populate the "parent case" field in one of the cases.  At that time I will need a button or trigger to retrieve fields from the parent case and populate the corresponding fields on the parent case.  Can someone help me write this class or trigger?
Vinit_KumarVinit_Kumar
Bennet,

I gave you the sample code earlier.I have wrapped it in a Trigger,please try below code for Trigger :-
trigger populateChildcase(before update)

List<Id> parentCaseIds = new List<Id>();

for(Case c : Triiger.new)
{
	parentCaseIds.add(c.PareentId);
}

//Query the parentcase Values,yoou need to query all fields which you want to be populated in yiur child case
Map<Id,Case> caseMap =new Map<Id,Case> ([select id,CaseNumber,Origin from Case where Id in:parentCaseIds]);

//populate the child case with related Parentcase values.

for(Case c : Triiger.new)
{
	if(!caseMap.IsEmpty())
	{
		c.Origin = caseMap.get(c.ParentId).Origin;
	}
}

If this helps,please mark it as best answer to help others :)

BennettTranBennettTran
Thanks for the response.  Sorry to keep bothering you but I have a couple more criteria.  I need this update to only happen to a certain case record type("Integration") and it should only update it once after the user decides who the parent case is.  Can you incorporate that into this trigger or should it be a class since the case needs to be updated only after a parent case is chosen?

Sorry if my questions are rudementary.

Thanks!
Vinit_KumarVinit_Kumar
I have already incorated that it would only fire when an user selects Parent case.Now,I have updated it to run only for Record Type= Integration.

Try below code :-

trigger populateChildcase(before update)

List<Id> parentCaseIds = new List<Id>();

RecordType rt = [select id from RecordType where DeveloperName='Integration' and SobjectType='Case'];

for(Case c : Trigger.new)
{
if(c.RecordTypeId==rt.id)
	{
		parentCaseIds.add(c.ParentId);
	}
}

//Query the parentcase Values,yoou need to query all fields which you want to be populated in yiur child case
Map<Id,Case> caseMap =new Map<Id,Case> ([select id,CaseNumber,Origin from Case where Id in:parentCaseIds]);

//populate the child case with related Parentcase values.

for(Case c : Trigger.new)
{
	if(!caseMap.IsEmpty() && c.RecordTypeId==rt.id)
	{
		c.Origin = caseMap.get(c.ParentId).Origin;
	}
}


This was selected as the best answer
BennettTranBennettTran
Awesome! Thank you so much for the help.

The code works well except it updates the child cases everytime it is edited if any of the field values change on the parent case.  That is why I was looking for a button so it would only update when the user selected it on the child case.  Is that something you could accomplish? 

Thanks!