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
RAJU_DEEPRAJU_DEEP 

Update the record

Hello,

           I am having two custom object (dml1__c and dml2__c). The relation between these two object is Lookup Relationship. I want to update the record within the click. Here is the code which i tried.

 

 

public void updateDml2(){
        List<dml2__c> d2 = [SELECT name, Last_Name__c, Other__c, dml1__r.name, dml1__r.Summary__c FROM dml2__c WHERE name =: dName];
        for(dml2__c dm2 : d2){
            dm2.Last_Name__c = lName;
            dm2.Other__c = sum;
            dm2.dml1__r.Summary__c = sum;
            update dm2;
        }
    }

 

 

In the above code when I click the update button the record is updated successfully in dml2__c custom object but the record of dml1__c custom object remains unchanged. Is there any way to do this correctly or guide where I am going wrong.

Any help will be highly appreciable.

 

Thanks

Raju

Best Answer chosen by Admin (Salesforce Developers) 
splitsplit
public void updateDml2(){
        List<dml2__c> d2 = [SELECT id,name, Last_Name__c, Other__c, dml1__r.name, dml1__c FROM dml2__c WHERE name =: dName];
        List<dml1__c> d1 = [SELECT Id, Summary__c FROM dml1__c];
        for(dml2__c dm2 : d2){
            dm2.Last_Name__c = lName;
            dm2.Other__c = sum;
            for(dml1__c dm1 : d1){
               if(dm1.Id == dm2.dml1__c)dm1.Summary__c = sum;
            }
        }
        update d2;
        update d1;
}

 

All Answers

splitsplit
public void updateDml2(){
        List<dml2__c> d2 = [SELECT id,name, Last_Name__c, Other__c, dml1__r.name, dml1__c FROM dml2__c WHERE name =: dName];
        List<dml1__c> d1 = [SELECT Id, Summary__c FROM dml1__c];
        for(dml2__c dm2 : d2){
            dm2.Last_Name__c = lName;
            dm2.Other__c = sum;
            for(dml1__c dm1 : d1){
               if(dm1.Id == dm2.dml1__c)dm1.Summary__c = sum;
            }
        }
        update d2;
        update d1;
}

 

This was selected as the best answer
RAJU_DEEPRAJU_DEEP

Hello,

            Thanks for your reply, I tried your suggested code and it is working successfully.

Regards & Thanks,

Raju