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
El.HEl.H 

Apex copy data from related object

Hello,

I am trying to write something small for execute anonymous wherein I can access a field on a related object and copy data there to a new field on the current object. I am struggling to write the correct syntax for this one though so any help or guidance would be appreciated.

The objects I am using are Contact and Cases.
In this scenario, I am trying to copy data from a field on the Case object, to a new one on the related Contact object.
To specify:
There is a field called Health__c, which exists on the Cases, and I have created a custom field named Health_and_support__c which resides on the Contact objects.  I am trying to write a script which will:
  • get a list of all contacts that have a related case and have nothing filled out in health and support already.
  • loop through each contact and copy the information from Health__c, from the related Case record, into Health_and_support__c on the Contact record.

my code so far looks like this:
list<Contact> needHealth = new list <Contact>();
needHealth = [SELECT Id, Name, Health_and_support__c FROM Contact where Id in (select ContactId from Case) AND Health_and_support__c=''];

for (Contact c :needHealth){
    c.Health_and_support__c=c.Case.Health__c;
}

update needHealth;
Currently this code will display error: Line: 6, Column: 31
Variable does not exist: Case

I am fairly confident the answer would be similar to this (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SObjects_field_relationships.htm)
however I can't seem to figure out how to do this with a list of objects.

Thank you for any assistance.

El
 
Best Answer chosen by El.H
ANUTEJANUTEJ (Salesforce Developers) 
Hi Ei,

Can you try using the below code and see if it works:
 
list<Contact> needHealth = new list <Contact>();
set<Id> needHealthset = new set <Id>();
needHealth = [SELECT Id, Name, Health_and_support__c FROM Contact Where Health_and_support__c=''];
for(Contact c:needHealth){needHealthset.add(c.id);}
list<Case> caselist=new list<case>();
caselist=[select Health__c,ContactId from Case Where ContactId in :needHealthset];
Map<id,string> casemap=new MAP<id,string>();
for(Case c: caselist)
{
	casemap.put(c.contactid, c.Health__C);
}
List<Contact> toupdatelist=new List<Contact>();

for (Contact c :needHealth){
    c.Health_and_support__c=casemap.get(c.id);
    toupdatelist.add(c);

}

update needHealth;

Also, In case if this is something you intend to repeat manually I would recommend you try checking the below link that indicates an implementation using flow to copy details from child record to parent record.

>>https://help.salesforce.com/articleView?id=000339886&type=1&mode=1

Let me know in case if you need any assistance.

Regards,
Anutej

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Ei,

Can you try using the below code and see if it works:
 
list<Contact> needHealth = new list <Contact>();
set<Id> needHealthset = new set <Id>();
needHealth = [SELECT Id, Name, Health_and_support__c FROM Contact Where Health_and_support__c=''];
for(Contact c:needHealth){needHealthset.add(c.id);}
list<Case> caselist=new list<case>();
caselist=[select Health__c,ContactId from Case Where ContactId in :needHealthset];
Map<id,string> casemap=new MAP<id,string>();
for(Case c: caselist)
{
	casemap.put(c.contactid, c.Health__C);
}
List<Contact> toupdatelist=new List<Contact>();

for (Contact c :needHealth){
    c.Health_and_support__c=casemap.get(c.id);
    toupdatelist.add(c);

}

update needHealth;

Also, In case if this is something you intend to repeat manually I would recommend you try checking the below link that indicates an implementation using flow to copy details from child record to parent record.

>>https://help.salesforce.com/articleView?id=000339886&type=1&mode=1

Let me know in case if you need any assistance.

Regards,
Anutej
This was selected as the best answer
ANUTEJANUTEJ (Salesforce Developers) 
Also, can you mention if there is only one case per contact if not then can you check the second soql with the limit word.
El.HEl.H
Hello,

This appears to work in Sandbox thank you,

There may also be more than 1 case per contact yes but in all cases the health should be the same, I plan to remove this field from cases once they are all moved over so as long as each contact has at least something copied over it doesnt matter really.

Thanks,
El