2

I tried to create a trigger to auto create a Contact when there is a new Account. I keep getting an error saying

variable c doesn't exist

within the following code:

trigger CreateAccountContact on Account (after insert){
   List<Contact> ct = new List <Contact>();
   for(Account acc : trigger.new){
   if(acc.recordTypeID=='01224000000Da23'||'01224000000CrTk')
      Contact c = new Contact(LastName = acc.name,
                AccountId=acc.id,
                Fax=acc.Fax,
                MailingStreet=acc.BillingStreet,
                MailingCity=acc.BillingCity,
                MailingState=acc.BillingState,
                MailingPostalCode=acc.BillingPostalCode,
                MailingCountry=acc.BillingCountry,
                Address__c=acc.Address__c,
                Phone=acc.Phone,
                Email=acc.Email_address__c);
    ct.add(c);
insert ct;
}
}
3

Enclose condition in brackets:

    if(acc.recordTypeID=='01224000000Da23 '|| acc.recordTypeID=='01224000000CrTk')
    {
        Contact c = new Contact(LastName = acc.name,
                    AccountId=acc.id,
                    Fax=acc.Fax,
                    MailingStreet=acc.BillingStreet,
                    MailingCity=acc.BillingCity,
                    MailingState=acc.BillingState,
                    MailingPostalCode=acc.BillingPostalCode,
                    MailingCountry=acc.BillingCountry,
                    Address__c=acc.Address__c,
                    Phone=acc.Phone,
                    Email=acc.Email_address__c);
        ct.add(c);
    }

if only takes first statment when not enclosed in bracket. So c becomes out of scope for ct.add(c) becasue it is in second line.

Also, you must put ct.add(c); out of the for loop. It is not needed in for

| improve this answer | |
  • Thanks! Although, now I am getting an error saying that OR operator can only be applied to Boolean expressions. – Inna Aug 18 '15 at 9:45
  • 3
    Use if(acc.recordTypeID=='01224000000Da23 '|| acc.recordTypeID=='01224000000CrTk') – Ruchi Aug 18 '15 at 9:46
1

ALSO: You cannot do insert record in for loop you need to have insert outside for loop. This issue may be of brackets. You also need to get record type id from date base rather than hardcode here. try this may help you:

 List < Contact > ct = new List < Contact > ();
 for (Account acc: trigger.new) {
     if (acc.recordTypeID == '01224000000Da23' || '01224000000CrTk') {
         Contact c = new Contact(LastName = acc.name,
             AccountId = acc.id,
             Fax = acc.Fax,
             MailingStreet = acc.BillingStreet,
             MailingCity = acc.BillingCity,
             MailingState = acc.BillingState,
             MailingPostalCode = acc.BillingPostalCode,
             MailingCountry = acc.BillingCountry,
             Address__c = acc.Address__c,
             Phone = acc.Phone,
             Email = acc.Email_address__c);
         ct.add(c);
     }
 }
 insert ct;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.