+ Start a Discussion
dietcoladietcola 

"unexpected token"

I am completely new to this, so I'm sure I'm missing something simple.  I get the following error:

 

 Error: Compile Error: unexpected token: 'Trigger.new' at line 2 column 138

 

when I try to save the following code:

 

 

trigger updateAccountName on Account (before Insert){
  List<Lead> convertedLeads=[Select ID,ConvertedAccountId, Firstname__c, MiddleName__c, LastName__c FROM Lead WHERE ConvertedAccountId IN Trigger.new];
  For (Account a: Trigger.new){
    For (Lead l: convertedLeads){
      if (l.ConvertedAccountId=a.ID){
         a.Name=l.FirstName__c+l.MiddleName__c+l.LastName__c;
      }
    }
  }
}

 

 

Thank you!!

 

 
Best Answer chosen by Admin (Salesforce Developers) 
CyclebiffCyclebiff

The issue is with your IF statement using assignment operator instead of comparison :) Use double equals sign.

 

Try:

 

 trigger updateAccountName on Account (before Insert){
  List<Lead> convertedLeads=[Select ID,ConvertedAccountId, First_Name__c, Last_Name__c FROM Lead WHERE ConvertedAccountId IN :Trigger.newMap.keySet()];
  For (Account a: Trigger.new){
    For (Lead l: convertedLeads){
      if (l.ConvertedAccountId==a.ID){
         a.Name=l.First_Name__c+l.Last_Name__c;
      }
    }
  }
}

All Answers

David VPDavid VP

Try this :

 

List<Lead> convertedLeads=[Select ID,ConvertedAccountId, Firstname__c, MiddleName__c, LastName__c FROM Lead WHERE ConvertedAccountId IN :Trigger.newMap.keySet()];

 

Note the extra ' : ' and the use of the Map to get to the keySet (which is a Set if Id's in this case)

 

dietcoladietcola

Awesome!  That took care of that one--thank you very much!  One new one:

 

 Error: Compile Error: Field is not writeable: Lead.ConvertedAccountId at line 5 column 11

 

 (and here's how the code looks now):

 

 

 trigger updateAccountName on Account (before Insert){
  List<Lead> convertedLeads=[Select ID,ConvertedAccountId, First_Name__c, Last_Name__c FROM Lead WHERE ConvertedAccountId IN :Trigger.newMap.keySet()];
  For (Account a: Trigger.new){
    For (Lead l: convertedLeads){
      if (l.ConvertedAccountId=a.ID){
         a.Name=l.First_Name__c+l.Last_Name__c;
      }
    }
  }
}

 

 

 Thank you again!

CyclebiffCyclebiff

The issue is with your IF statement using assignment operator instead of comparison :) Use double equals sign.

 

Try:

 

 trigger updateAccountName on Account (before Insert){
  List<Lead> convertedLeads=[Select ID,ConvertedAccountId, First_Name__c, Last_Name__c FROM Lead WHERE ConvertedAccountId IN :Trigger.newMap.keySet()];
  For (Account a: Trigger.new){
    For (Lead l: convertedLeads){
      if (l.ConvertedAccountId==a.ID){
         a.Name=l.First_Name__c+l.Last_Name__c;
      }
    }
  }
}

This was selected as the best answer
dietcoladietcola
Oh man, even I should have figured that one out!  Thank you so much though; I would have been staring at it for hours!!  =)