You need to sign in to do that
Don't have an account?
El.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:
my code so far looks like this:
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
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
Can you try using the below code and see if it works:
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
Can you try using the below code and see if it works:
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 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