Skip to main content
added 80 characters in body
Source Link
Santanu Boral
  • 36.2k
  • 8
  • 45
  • 72

Approach 1

Since Address in Compound field, you can simply fetch each Address attributes from Account object from the SOQL query and update invoice records like this:

trigger udtInvAddress on Account (After Update) {
List<Order2__c> invoices;
if(Trigger.isAfter && Trigger.isUpdate){            
     invoices = [SELECT Id, Account__r.BillingStreet, Account__r.BillingCity, 
     Billing_City__c, Billing_State__c, Billing_Street__c, 
     Billing_zip__c, Billing_Country__c FROM Order2__c 
     WHERE Account_Name__c =:Trigger.newMap.keySet()];
    for(Order2__c invoice: invoices)
    {
        System.debug(invoice.ID);
        //update all of the fields with the updated information only if the new information is filled in.
        if(invoice.Account__r.BillingStreet <> null){
            invoice.Billing_Street__c = invoice.Account__r.BillingStreet;
        }
        //update other fields

    }
    update invoices;
}

Moreover, your query needs to be bulkified and would be outside of for loop.

Approach 2

The use case can be simply be implemented by Process Builder. In this below picture I have shown you how Invoice Billing Street can be mapped with Account's Billing Street via Reference.

Update Invoice

The Account Address attributes can be picked up in the PB during field mapping like this:

Account Address fields

Since, without writing code, this functionality can be achieved, I will go by Process Builder.

Since Address in Compound field, you can simply fetch each Address attributes from Account object from the SOQL query and update invoice records like this:

trigger udtInvAddress on Account (After Update) {
List<Order2__c> invoices;
if(Trigger.isAfter && Trigger.isUpdate){            
     invoices = [SELECT Id, Account__r.BillingStreet, Account__r.BillingCity, 
     Billing_City__c, Billing_State__c, Billing_Street__c, 
     Billing_zip__c, Billing_Country__c FROM Order2__c 
     WHERE Account_Name__c =:Trigger.newMap.keySet()];
    for(Order2__c invoice: invoices)
    {
        System.debug(invoice.ID);
        //update all of the fields with the updated information only if the new information is filled in.
        if(invoice.Account__r.BillingStreet <> null){
            invoice.Billing_Street__c = invoice.Account__r.BillingStreet;
        }
        //update other fields

    }
    update invoices;
}

Moreover, your query needs to be bulkified and would be outside of for loop.

Approach 1

Since Address in Compound field, you can simply fetch each Address attributes from Account object from the SOQL query and update invoice records like this:

trigger udtInvAddress on Account (After Update) {
List<Order2__c> invoices;
if(Trigger.isAfter && Trigger.isUpdate){            
     invoices = [SELECT Id, Account__r.BillingStreet, Account__r.BillingCity, 
     Billing_City__c, Billing_State__c, Billing_Street__c, 
     Billing_zip__c, Billing_Country__c FROM Order2__c 
     WHERE Account_Name__c =:Trigger.newMap.keySet()];
    for(Order2__c invoice: invoices)
    {
        System.debug(invoice.ID);
        //update all of the fields with the updated information only if the new information is filled in.
        if(invoice.Account__r.BillingStreet <> null){
            invoice.Billing_Street__c = invoice.Account__r.BillingStreet;
        }
        //update other fields

    }
    update invoices;
}

Moreover, your query needs to be bulkified and would be outside of for loop.

Approach 2

The use case can be simply be implemented by Process Builder. In this below picture I have shown you how Invoice Billing Street can be mapped with Account's Billing Street via Reference.

Update Invoice

The Account Address attributes can be picked up in the PB during field mapping like this:

Account Address fields

Since, without writing code, this functionality can be achieved, I will go by Process Builder.

added 80 characters in body
Source Link
Santanu Boral
  • 36.2k
  • 8
  • 45
  • 72

Since Address in Compound field, you can simply fetch each Address attributes from Account object from the SOQL query and update invoice records like this:

trigger udtInvAddress on Account (After Update) {
List<Order2__c> invoices;
if(Trigger.isAfter && Trigger.isUpdate){            
     invoices = [SELECT Id, Account__r.BillingStreet, Account__r.BillingCity, 
     Billing_City__c, Billing_State__c, Billing_Street__c, 
     Billing_zip__c, Billing_Country__c FROM Order2__c 
     WHERE Account_Name__c =:Trigger.newMap.keySet()];
    for(Order2__c invoice: invoices)
    {
        System.debug(invoice.ID);
        //update all of the fields with the updated information only if the new information is filled in.
        if(invoice.Account__r.BillingStreet <> null){
            invoice.Billing_Street__c = invoice.Account__r.BillingStreet;
        }
        //update other fields

    }
    update invoices;
}

Moreover, your query needs to be bulkified and would be outside of for loop.

Since Address in Compound field, you can simply fetch each Address attributes from Account object from the SOQL query and update invoice records like this:

trigger udtInvAddress on Account (After Update) {
List<Order2__c> invoices;
if(Trigger.isAfter && Trigger.isUpdate){            
     invoices = [SELECT Id, Account__r.BillingStreet, Account__r.BillingCity, 
     Billing_City__c, Billing_State__c, Billing_Street__c, 
     Billing_zip__c, Billing_Country__c FROM Order2__c 
     WHERE Account_Name__c =:Trigger.newMap.keySet()];
    for(Order2__c invoice: invoices)
    {
        System.debug(invoice.ID);
        //update all of the fields with the updated information only if the new information is filled in.
        if(invoice.Account__r.BillingStreet <> null){
            invoice.Billing_Street__c = invoice.Account__r.BillingStreet;
        }
        //update other fields

    }
    update invoices;
}

Since Address in Compound field, you can simply fetch each Address attributes from Account object from the SOQL query and update invoice records like this:

trigger udtInvAddress on Account (After Update) {
List<Order2__c> invoices;
if(Trigger.isAfter && Trigger.isUpdate){            
     invoices = [SELECT Id, Account__r.BillingStreet, Account__r.BillingCity, 
     Billing_City__c, Billing_State__c, Billing_Street__c, 
     Billing_zip__c, Billing_Country__c FROM Order2__c 
     WHERE Account_Name__c =:Trigger.newMap.keySet()];
    for(Order2__c invoice: invoices)
    {
        System.debug(invoice.ID);
        //update all of the fields with the updated information only if the new information is filled in.
        if(invoice.Account__r.BillingStreet <> null){
            invoice.Billing_Street__c = invoice.Account__r.BillingStreet;
        }
        //update other fields

    }
    update invoices;
}

Moreover, your query needs to be bulkified and would be outside of for loop.

Source Link
Santanu Boral
  • 36.2k
  • 8
  • 45
  • 72

Since Address in Compound field, you can simply fetch each Address attributes from Account object from the SOQL query and update invoice records like this:

trigger udtInvAddress on Account (After Update) {
List<Order2__c> invoices;
if(Trigger.isAfter && Trigger.isUpdate){            
     invoices = [SELECT Id, Account__r.BillingStreet, Account__r.BillingCity, 
     Billing_City__c, Billing_State__c, Billing_Street__c, 
     Billing_zip__c, Billing_Country__c FROM Order2__c 
     WHERE Account_Name__c =:Trigger.newMap.keySet()];
    for(Order2__c invoice: invoices)
    {
        System.debug(invoice.ID);
        //update all of the fields with the updated information only if the new information is filled in.
        if(invoice.Account__r.BillingStreet <> null){
            invoice.Billing_Street__c = invoice.Account__r.BillingStreet;
        }
        //update other fields

    }
    update invoices;
}