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
Force.platformForce.platform 

how to update parent record of custom object using DML

Child name-Childobject__C;
Parent name-Parentobjectname__c;
i want query to update related parent record.
Lokesh KumarLokesh Kumar
Kindly Check the below link.

https://success.salesforce.com/answers?id=90630000000ZggwAAC

Thanks
Lokesh
Raj VakatiRaj Vakati
Childobject__C chaildObj = [SELECT Name , Parentobjectname__r.Name ,Parentobjectname__c  from 
Childobject__C  LIMIT 1];

    chaildObj.Name = 'Hello';
    chaildObj.Parentobjectname__r.Name = 'Exmaples';

    // Make two separate calls 
    // 1. This call is to update the chaildObject Name
    update chaildObj;
    // 2. This call is to update the related Parentobjectname__rName
	
    update chaildObj.Parentobjectname__r;

 
Akshay_DhimanAkshay_Dhiman
Hi Arati,

Please go through the below code 
DML Code for Execute Anonymous

Childobject__c childRecord = [select Id, Name, Parentobjectname__c from Childobject__c where Parentobjectname__c != null limit 1];// Parentobjectname__c here is a Parent Object lookup field on Child Object.
Parentobjectname__c parentRecord = [select Id, Name from Parentobjectname__c where Id =: childRecord.Parentobjectname__c];
// BEFORE UPDATE //
system.debug('Child Name --- '+childRecord.Name);
system.debug('Parent Name --- '+parentRecord.Name);
parentRecord.Name = 'Updated Parent Name';
update parentRecord;
update childRecord;
// AFTER UPDATE //
system.debug('Parent Name After Updation --- '+parentRecord.Name);
system.debug('Child Name After Updation --- '+childRecord.Name);  
User-added imageUser-added image

Regards,
Akshay