0

There are two Objects with master-detail relationship (Contact and Certification__c)

I need to copy a textfield(Name) value from the child object(Certification__c) and paste it on it's Parent's(Contact) textfield(Certification_Token), for every contact which has a certification instance. Please note that this is for existing record values.

code:

List<Certification__c> CertificationObjects = new List<Certification__c>();
CertificationObjects = [SELECT Name, Contact__r.Id, Contact__r.Certification_Token__c FROM Certification__c];

for(Certification__c c : CertificationObjects){
    if(c.Contact__r.Certification_Token__c == NULL){
        c.Contact__r.Certification_Token__c = c.Name;
    }
}
update CertificationObjects;

When I run this on the Execute Anonymous Window, it shows no error, but it is not updating it's parent. Should i map the fields using the Id and update it or can we fix this code? I do not require a workflow, as I need to update the existing values in the system, and not the future ones.

@UPDATE not every contact instance will have a certification instance. A contact can have multiple certification instances, but all the certification instances of a particular contact can have only one .Name

2 Answers 2

1

The update DML command on the CertificationObjects will not update the Contact lookup fields' data - it only updates fields on the Certification__c record itself.

Try the following (EDIT - using Map to avoid duplicate Contact Id updates):

List<Certification__c> CertificationObjects = new List<Certification__c>();
CertificationObjects = [SELECT Name, Contact__r.Id, Contact__r.Certification_Token__c FROM Certification__c];

Map<Id, Contact> toUpdate = new Map<Id, Contact>();

for (Certification__c cert : CertificationObjects){
    if (String.isBlank(cert.Contact__r.Certification_Token__c)){
        toUpdate.put(cert.Contact__c, new Contact(Id = cert.Contact__c, Certification_Token__c = cert.Name));
    }
}
update toUpdate.values();
9
  • not every contact instance will have a certification instance. A contact can have multiple certification instances, but all the certification instances of a particular contact can have only one .Name. How would this affect the code? @Brian_Miller Apr 2, 2019 at 8:21
  • Since you're querying Certification__c, you'll only be checking real Certification records. I think everything is okay here Apr 2, 2019 at 8:35
  • 1
    Line: 11, Column: 1 System.ListException: Duplicate id in list: 0035C00000FRacdQAD . Line 11 is update toUpdate; I have updated my question with the new code Apr 2, 2019 at 8:37
  • 1
    Ah, I hear. See the updated answer that uses a Map<Id, Contact>. Note that the certification token name will not represent every certification that the contact has, since Map.put() on the same Contact Id is just overwriting the value when the same contact shows up more than once. Apr 2, 2019 at 8:41
  • 1
    @NivethKumar new Contact() just creates an instance of the record in memory. Since we're setting the Id, we're basically saying "this contact already exists", and now any other fields I put in this record will overwrite whatever is actually in the database when we do the update command. Note that if we tried to do insert we would get an error because we set the Id field. Apr 2, 2019 at 9:58
-1

please try this -

if(c.Contact__r.Certification_Token__c == NULL || c.Contact__r.Certification_Token__c =='')

2
  • this is not the issue here, I believe it's what Brain Miller has said. Apr 2, 2019 at 8:28
  • 1
    String.isBlank(c.Contact__r.Certification_Token__c) should be used instead - it handles both null and empty string checks Apr 2, 2019 at 8:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .