+ Start a Discussion
Paul ZamskyPaul Zamsky 

Cross Object Field Update - Apex Trigger

Hi Everybody,

I'm pretty new to Apex and have been trying to do a cross object field update. I have two objects Lead and Candidate_Non_Member_c and I want to have the field Candidate_Non_Member__c.Called_C equal to 1 when the Lead.Disposition = ' Registered'.  Also the only way to find the corresponding candidate to the lead is by phone number. Lead.Phone and Candidate_Non_Member__c.Formated_Phone_Number__c. 

This is what I have so far but I've been getting "Error: Compile Error: Expression cannot be assigned at line -1 column -1"

Any help would be great. 
 

Thanks!

trigger Lead_To_Candidate_2 on Lead (after insert, after update)
{
    String objemail;
    String objfirstname;
    String objphone;
   
    for(Lead obj: Trigger.new)
    {
        if (Lead.disposition__c = 'Registered')
        {
            objemail = obj.Email;
            objfirstname = obj.Name;
            objphone = obj.Phone;
        }
    }
   
    List<Candidate_Non_Member__c> candidate = new List<Candidate_Non_Member__c>([SELECT Id, Formated_Phone_Number__c, Called__c FROM Candidate_Non_Member__c WHERE Formated_Phone_Number__c = :objphone]);
    List<Candidate_Non_Member__c> candidateUpdateList = new List<Candidate_Non_Member__c>();
   
    for(Candidate_Non_Member__c c: candidate)
    {
        Candidate_Non_Member__c.Called__c = 1;
    }
        if(candidateUpdateList.size() > 0)
    {
        update candidateUpdateList;
    }
    }

Best Answer chosen by Paul Zamsky
ShashForceShashForce
Hi,

Please try this:

trigger Lead_To_Candidate_2 on Lead (after insert, after update)
{
   	set<string> phoneNumbers = new set<string>;
   
    for(Lead obj: Trigger.new)
    {
        if (obj.disposition__c = 'Registered')
        {
			phoneNumbers.add(obj.Phone);
        }
    }
   
    List<Candidate_Non_Member__c> candidate = [SELECT Id, Formated_Phone_Number__c, Called__c FROM Candidate_Non_Member__c WHERE Formated_Phone_Number__c IN :phoneNumbers];
    List<Candidate_Non_Member__c> candidateUpdateList = new List<Candidate_Non_Member__c>();
   
    for(Candidate_Non_Member__c c: candidate)
    {
        c.Called__c = 1; //this is if called__c is a number field. If it is a text field, the line should be c.Called__c = '1';
		candidateUpdateList.add(c);
    }
    if(candidateUpdateList.size() > 0)
    {
        update candidateUpdateList;
    }
}

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank

All Answers

Eli Flores, SFDC DevEli Flores, SFDC Dev
I'm going to spitball here but I'll guess the problem is  with this line:

if (Lead.disposition__c = 'Registered')
 It probably should be 
if (obj.disposition__c = 'Registered')

you have the same type of error towards the bottom
Candidate_Non_Member__c.Called__c = 1;
should be 
c.Called__c = 1;

Also, you never add anything to candidateUpdateList so it's always empty. I think you want:
c.Called__c = 1;
candidateUpdateList.add(c); 
in that spot. 


ShashForceShashForce
Hi,

Please try this:

trigger Lead_To_Candidate_2 on Lead (after insert, after update)
{
   	set<string> phoneNumbers = new set<string>;
   
    for(Lead obj: Trigger.new)
    {
        if (obj.disposition__c = 'Registered')
        {
			phoneNumbers.add(obj.Phone);
        }
    }
   
    List<Candidate_Non_Member__c> candidate = [SELECT Id, Formated_Phone_Number__c, Called__c FROM Candidate_Non_Member__c WHERE Formated_Phone_Number__c IN :phoneNumbers];
    List<Candidate_Non_Member__c> candidateUpdateList = new List<Candidate_Non_Member__c>();
   
    for(Candidate_Non_Member__c c: candidate)
    {
        c.Called__c = 1; //this is if called__c is a number field. If it is a text field, the line should be c.Called__c = '1';
		candidateUpdateList.add(c);
    }
    if(candidateUpdateList.size() > 0)
    {
        update candidateUpdateList;
    }
}

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
This was selected as the best answer
Paul ZamskyPaul Zamsky
Thank you guys for the help!


Paul ZamskyPaul Zamsky
Another question, How should I create  a test class for this trigger?

ShashForceShashForce
These links should help:

http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods