-7

I am new to the world of triggers.I have a custom object customer and if some condition is satisfied, i would like to insert fields from customer to accounts. how do i do that?i did the following but it says

Error: Compile Error: unexpected token: 'for' at line 6 column 6 :

trigger CustomerTrigger on Customer__c (before insert) {

List<Account> acct = new List<Account>
//Trigger.new Represents a list of Customer records you are trying to insert.
//So iterate the list in a for loop     
      for (Customer__c newCustomerRecord: Trigger.new) {  

            //Check if the Customer belongs to a Key Account
            if(newCustomerRecord.Key_Account__c){

            acct.add(new Account(
                                    Customer_Name_c = newCustomerRecord.Customer_Name_c,
                                    Customer_Address_c = newCustomerRecord.Customer_Address_c));

            }
      }  

insert acct;
}
1
  • Custom object or field suffixes always have a double underscore i.e. __c rather than _c so check that.
    – Keith C
    Commented Mar 12, 2014 at 12:35

4 Answers 4

2

Please change :

List<Account> acct = new List<Account>

to

List<Account> accts = new List<Account>();
4
  • Thankyou Artur Kępczyński. I have changed. but now it shows the following error: Error: Compile Error: Invalid field Customer_Name_c for SObject Account at line 12 column 55
    – user7165
    Commented Mar 12, 2014 at 12:10
  • the best place that you could start with is the platform documentation which have lot of examples will give you an idea of syntax, semantics and their usage...
    – Bforce
    Commented Mar 12, 2014 at 12:13
  • I am always happy to help but you need also try harder by yourself - this comunicat means that there is no such field on object account, please check that. Also when you creating record you have to put all required fields Commented Mar 12, 2014 at 12:14
  • Thankyou, which is that one document which has clearly mentioned abt syntacs and semantics?can you post the link?
    – user7165
    Commented Mar 12, 2014 at 13:11
0

I want to help you to understand how programming works:

Apex is similar to Java (a programming language from oracle). Each instruction should be closed by a semicolon. ;. In order to call a method use brackets () inside of these you can send parameters to your method.

The issue in your case was caused by missing brackets and semicolon.

List<Account> accts = new List<Account>();

List<Account> indicates that you want to declare a variable. You variable is called accts. The variable is initialized (=) by a method called new List<Account>(). It creates a new object from type List.

In order to learn more about basic programming concepts please visit http://docs.oracle.com/javase/tutorial/java/concepts/

0
trigger CustomerTrigger on Customer__c (before insert){
    List<Account> accts = new List<Account>();
         for (Customer__c newCustomerRecord: Trigger.new) {  
            //Check if the Customer belongs to a Key Account
            if(newCustomerRecord.Key_Account__c!=Null)
            {
                Account ac=new Account();
                ac.Name= newCustomerRecord.Name;
                ac.BillingStreet= newCustomerRecord.Customer_Street__c;
                ac.BillingCity=newCustomerRecord.Customer_City__c;
                ac.BillingCountry=newCustomerRecord.Customer_Country__c;
                accts.add(ac);
            }
      }  
    insert accts;
}
2
  • Please edit your post to explain why this code will help. What did you change?
    – Adrian Larson
    Commented Mar 4, 2017 at 13:41
  • 1
    To add to @AdrianLarson comments, are you aware you've answered a 2 year old question that already has an answer that although not accepted, explains the problem with the code posted in the question?
    – crmprogdev
    Commented Mar 4, 2017 at 14:20
0

First of all,
I think you have not noticed that there is no such field on account object like Customer_Name_c and Customer_Address_c (that’s why this error occurred “Invalid field Customer_Name_c for SObject Account “). So you have to create these custom field in Account to map with customer object or you should use standard fields of account object. And one other point that you can’t map address field directly. So you should use these account fields(BillingCity,BillingStreet).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .