+ Start a Discussion
suresh reddy.ax1069suresh reddy.ax1069 

I am strugguling from this code please help me

trigger udatetrig on contact (after update)


{list<lead> plist=new list<lead>();


for(contact a:trigger.new)


for(lead c:[select Name from lead where contactid=:a.id])







update plist;




I have an Error as "" Error: Compile Error: Invalid field contactid for SObject Lead at line 9 column 1""



please solve my problem


Thanks in Advance.


"Please solve my problem".     You have many problems here.


1. Your terrible formatting hides the fact that line 9 contains two statements:

9.      c.lastname=a.lastname;c.

10.    contactid=a.id;plist.add(c);


Most of the time programmers will write one statement on one line.  It's easier to read and makes more sense.


This makes it harder to see that the line that it's complaining about is:   c.contactid=....



2. You aren't reading your error message.   It's clear if you just read it.

Error:          -  Hey, you have an error.


Compile Error:   - Hey, it's something in the code that wrong with the way it's written... e.g. it can be evaluated right now.


Invalid field contactid for SObject Lead  -   Well, you're trying to use a field, "contactid", for the Lead object, and that's invalid.   Why is it invalid?  


A little looking at the documentation on the Lead object, or just looking at the New Lead screen would show you that there is no contactId field on the Lead object.    So, you need to figure out what you're actually trying to accomplish.



3. You have no comments in your code.   Bad.   I can't even figure out what you think you're trying to do.   Write it out in English (or your language of choice)  first and don't try to program it until it makes efficient sense in English.


4. In your For loop, the variable to refer to a Contact is the letter "a".  Why not use "c", or "con", or something that has some meaning.

5. In your next For loop, the variable "c" is used to refer to a Lead.  Why not "l", or "ld", or something. 

Those are poor choices which make your code harder to read and maintain.


6. You have a Select statement inside your For loop.  This is bad form in Apex.  Read about design patterns and Bulk Safety.


So, I'd be happy to help, but I don't know what you're really trying to do.   My suggestion is to start with #3 above and don't even try to just get this code working until you've thought it through.


You might also want to read about Leads and converting them to Contacts and Opportunities.


Best, Steve.





   Thank you sir.