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
NikithaNikitha 

hey I'm new to Trigger. I am creating a trigger , whenever iam creating in customer_Detail__c record customer_Detail__c Address Should be copy Account Billing Address. here I have lookup for both account and customer_Detail__c. But im geting err.

trigger AccountCustomerDetail on Customer_Detail__c (before insert) {
    set<id>accountId = new set<id>();
    for(Customer_Detail__c customer : trigger.new){
        accountId.add(customer.id);
    }
List<Account> acclist = [Select id,BillingStreet,BillingCity from Account 
                        where id in: accountId];
    Map<id,Account>accMap = new Map<id,Account>();
    for(Account acc: trigger.new){
        accMap.put(acc.id, acc);
    }
    for(Customer_Detail__c customer : Trigger.new){
        customer.APShipping_Street__c = accMap.get(customer.Id).BillingStreet;
        customer.AP_Shipping_City__c = accMap.get(customer.Id).BillingCity;
    }
}

Err is  Line 9 Invalid loop variable type expected Customer_Detail__c was Account. Please help me to solve this.
Best Answer chosen by Nikitha
Saad J 5Saad J 5
You are getting the record of Customer_Detail__c type object in a trigger.new context. So you can't assign trigger.new to Account type object in line number 9.
Compare your line number 3 and 9. You will understand why the error is. 
If it is the correct answer, please mark it as the best answer, it will help others.

And see the trigger tutorial here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm

 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Swathi,

I think you can have a simple formula field of text type that can hold the address as text, the details are mentioned in the below help article:

>> https://help.salesforce.com/articleView?id=000334673&type=1&mode=1

Below are the steps mentioned for quick reference:

1. Go to Contact page from within Setup.
    In Salesforce Classic: Setup | Customize | Contacts | Fields.
    In Lightning Experience: Gear Icon | Setup | Object Manager | Contact | 2. Fields & Relationships
3. Click New.
4. Select Formula for the Data Type.
5. Name the field and select Text for the Formula Return Type.
6. Click Next.
7. Enter your formula syntax.
 
For the Account Billing Address use this formula syntax:  
Account.BillingStreet & BR() &
Account.BillingCity & ", " &
Account.BillingState & " " &
Account.BillingPostalCode & BR() &
Account.BillingCountry
For the Account Shipping Address use this formula syntax:
Account.ShippingStreet & BR() &
Account.ShippingCity & ", " &
Account.ShippingState & " " &
Account.ShippingPostalCode & BR() &
Account.ShippingCountry
 
8. Click Next.
9. Add the desired field level security and page layouts in the respective formulas above.
10. Click on Save.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Saad J 5Saad J 5
You are getting the record of Customer_Detail__c type object in a trigger.new context. So you can't assign trigger.new to Account type object in line number 9.
Compare your line number 3 and 9. You will understand why the error is. 
If it is the correct answer, please mark it as the best answer, it will help others.

And see the trigger tutorial here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm

 
This was selected as the best answer
NikithaNikitha
Hello Saad J 5,
      Thanks for the help. I have compared my Line 3 and 9 . and changed trigger.new as accList now I'm err free but as apart of output.after filling my required details in Customer_Detail__c object. I didn't get any output.(Account address). I don't know what to do. can you please recheck my above code.