+ Start a Discussion
VDid.ax1020VDid.ax1020 

Trigger issues, need help

trigger ContactNameUpdate on account(before insert, before update)
{    if(trigger.isupdate){       
        for(account a:trigger.new);
        {           
            if((contact.Title = 'Accounts')){               
             contact.FirstName = account.Contact_First_Name__c ;
             contact.LastName = account.Contact_Last_Name__c ;           
            
}}}}

 

Error Message: Error: Compile Error: Expression cannot be assigned at line -1 column -1

 

What I am trying to accomplish here is to Pull the Contact1st and last names from the contact object if Title = Accounts and bring over to the Account object into two custom fields Contact_First_Name__c and Contact_Last_Name__c

 

I know I am cross referencing objects, but honestly have no idea how to do this right....

 

Thanks for the help!

Shashikant SharmaShashikant Sharma

Write this like this

Removed semicolon from : for(account a:trigger.new);

 

And  changed this

contact.Title = 'Accounts'  to contact.Title == 'Accounts'

as condition can not have = ( assignment operator)

 

trigger ContactNameUpdate on account(before insert, before update) 
{    if(trigger.isupdate){        
        for(account a:trigger.new)
        {            
            if((contact.Title == 'Accounts')){                
             contact.FirstName = account.Contact_First_Name__c ;
             contact.LastName = account.Contact_Last_Name__c ;            
             
}}}}

 

VDid.ax1020VDid.ax1020

Hi Shashi,

 

I orignally had the double = as in == I get the same exact error message. I just changed it and tried too.

 

Thanks

Shashikant SharmaShashikant Sharma

Ok you are using contact where you ahve not fetched contact try this

 

trigger ContactNameUpdate on account(before insert, before update) 
{    if(trigger.isupdate){        
        for(account a:trigger.new)
        {        
            Contact contact = [Select Title from Contact where AccountId =: a.id limit 1];    
            if((contact.Title == 'Accounts')){                
             contact.FirstName = account.Contact_First_Name__c ;
             contact.LastName = account.Contact_Last_Name__c ;            
             
}}}}

 

VDid.ax1020VDid.ax1020

I tried this and I get the error

 

Error: Compile Error: Variable does not exist: a.id at line 4 column 73

Shashikant SharmaShashikant Sharma

ok

try this 

 

trigger ContactNameUpdate on account(after insert, before update) 
{    if(trigger.isupdate){        
        List<Contact> lc = new list<Contact>();
        for(account a:trigger.new)
        {        
            Contact contact = [Select Title from Contact where AccountId =: a.id limit 1];    
            if((contact.Title == 'Accounts')){                
             contact.FirstName = account.Contact_First_Name__c ;
             contact.LastName = account.Contact_Last_Name__c ; 
             lc.add(contact);           
             
}

}update lc;}}

 

VDid.ax1020VDid.ax1020

Still not working.

 

Thank you for all your help! Really appreciate it. error is below

 

Error: Compile Error: Illegal assignment from Schema.SObjectField to String at line 9 column 14

Shashikant SharmaShashikant Sharma

ohh this is so bad I am missing such things

 

try this

trigger ContactNameUpdate on account(after insert, before update) 
{    if(trigger.isupdate){        
        List<Contact> lc = new list<Contact>();
        for(account a:trigger.new)
        {        
            Contact contact = [Select Title from Contact where AccountId =: a.id limit 1];    
            if((contact.Title == 'Accounts')){                
             contact.FirstName = a.Contact_First_Name__c ;
             contact.LastName = a.Contact_Last_Name__c ; 
             lc.add(contact);           
             
}

}update lc;}}

 

VDid.ax1020VDid.ax1020

So the trigger was saved successfully, but upon trying to edit an Account record to see if it works correctly I get this error....

 

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger ContactNameUpdate caused an unexpected exception, contact your administrator: ContactNameUpdate: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 003R000000MkaAlIAJ; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Last Name]: [Last Name]: Trigger.ContactNameUpdate: line 14, column 2".
Shashikant SharmaShashikant Sharma

do this change

trigger ContactNameUpdate on account(after insert, before update) 
{    if(trigger.isupdate){        
        List<Contact> lc = new list<Contact>();
        for(account a:trigger.new)
        {        
            Contact contact = [Select Title from Contact where AccountId =: a.id limit 1];    
            if((contact.Title == 'Accounts')){                
             contact.FirstName = a.Contact_First_Name__c;
             if(a.Contact_Last_Name__c != null)
            {
             contact.LastName = a.Contact_Last_Name__c; 
             }
         lc.add(contact);           
             
}

}update lc;}}

 

Shashikant SharmaShashikant Sharma

Did it worked for you?

VDid.ax1020VDid.ax1020

The trigger saves and no error on saving the account record. But the Contact First Name and Contact Last Name fields are blank....not populating

Shashikant SharmaShashikant Sharma

You must be having more than one contact on the account on which you are testing try this an then test

 

trigger ContactNameUpdate on account(after insert, before update) 
{    if(trigger.isupdate){        
        List<Contact> lc = new list<Contact>();
        for(account a:trigger.new)
        {        
            List<Contact> contactList = [Select Title from Contact where AccountId =: a.id ];
         for(Contact contact : contactList)
          {    
            if((contact.Title == 'Accounts')){                
             contact.FirstName = a.Contact_First_Name__c;
             if(a.Contact_Last_Name__c != null)
                     {
                        contact.LastName = a.Contact_Last_Name__c; 
                      }
       
               lc.add(contact);           
              }      
          }

}update lc;}}

 

VDid.ax1020VDid.ax1020

Hi Shashi,

 

I have given up on this trigger. It acts erratically, wont let me delete contact records and it does not update the fields on the account object. My solution was to use workflow rules instead.

 

I appreciate all your help.

 

Thanks,

V