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
himanshu huske 7himanshu huske 7 

master detail trigger

1.customer__c(master obj) and address__c(detail obj).
address__c has has 2 checkbox field Nagpur__c and Pune__c and customer__c has TextField Hometown__c 
if Nagpur__c is checked, Hometown__c = 'nagpur';
if Pune__c is checked, Hometown__c = 'pune';
2. is it true, that master-detail relation trigger like above wont require query ?
 
Best Answer chosen by himanshu huske 7
MKRMKR
Hi,

It is true that this does not require query. I don't know your exact API names of the objects and relation field but anyway, here is a code sample:
trigger AddressTrigger on Address__c (after insert, after update) {
    List<Customer__c> updateCustomers = new List<Customer__c>();
    for(Address__c address : Trigger.New) {
        if(address.Nagpur__c) {
            Customer__c updateCust = new Customer__c();
            updateCust.Id = address.Customer__c; //Use correct API field here
            updateCust.Hometown__c = 'nagpur';
            updateCustomers.add(updateCust);
        } else if (address.Pune__c) {
            Customer__c updateCust = new Customer__c();
            updateCust.Id = address.Customer__c; //Use correct API field here
            updateCust.Hometown__c = 'pune';
            updateCustomers.add(updateCust);
        }
    }
    update updateCustomers;
}
Even if the above will probably fulfill you business need. It might be worth checking that you are not performing unnecessary updates by checking if in after update, the address__c.Nagpur__c or address__c.Pune__c field values have actually changed.

Regards,
Mkr

All Answers

MKRMKR
Hi,

It is true that this does not require query. I don't know your exact API names of the objects and relation field but anyway, here is a code sample:
trigger AddressTrigger on Address__c (after insert, after update) {
    List<Customer__c> updateCustomers = new List<Customer__c>();
    for(Address__c address : Trigger.New) {
        if(address.Nagpur__c) {
            Customer__c updateCust = new Customer__c();
            updateCust.Id = address.Customer__c; //Use correct API field here
            updateCust.Hometown__c = 'nagpur';
            updateCustomers.add(updateCust);
        } else if (address.Pune__c) {
            Customer__c updateCust = new Customer__c();
            updateCust.Id = address.Customer__c; //Use correct API field here
            updateCust.Hometown__c = 'pune';
            updateCustomers.add(updateCust);
        }
    }
    update updateCustomers;
}
Even if the above will probably fulfill you business need. It might be worth checking that you are not performing unnecessary updates by checking if in after update, the address__c.Nagpur__c or address__c.Pune__c field values have actually changed.

Regards,
Mkr
This was selected as the best answer
himanshu huske 7himanshu huske 7
it was that simple and i was writing some nonsense logic thanks for this.... it works