function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Marlon Agard 2Marlon Agard 2 

Trigger to update account field based on a contact field

Hi,

I am trying to update an account field based on a contact field. I have discovered I need a trigger.

here is my trigger but keep getting an error;

trigger ContactUpdate on Account (after update) {
    set<Id> accountaccList = new set<Id>();
    List<Contact> listContact = new List<Contact>();
    
    for(Account acct : trigger.new){
        if(acct.Account_Has_MQL__c){
            accountaccList.add(acct.id);
        }
    }
    listContact = [ SELECT lastname, AccountId FROM Contact WHERE AccountId IN :accountaccList];
    
    if ( listContact.size() > 0 ) {
        for ( Contact con : listContact ) {            
                con.Became_a_Marketo_Qualifying_Lead__c=true;
                
           }
         if(listContact.size()>0)   
           update listContact;
    }
}
Tiago Armando CoelhoTiago Armando Coelho
Hi Marion,

What error is being triggered?
Marlon Agard 2Marlon Agard 2
Hi Tiago

I got this error 

Error: Compile Error: Incorrect SObject type: Account should be Contact at line -1 column -1
Tiago Armando CoelhoTiago Armando Coelho
Hi Marion, 

I found your mistake in your query:

listContact = [ SELECT lastname, AccountId FROM Contact WHERE AccountId IN :accountaccList];

you need to have the Id because without Id Salesforce doesn't recognize what to update.

Update your code to :

listContact = [ SELECT Id FROM Contact WHERE AccountId IN :accountaccList];
Tiago Armando CoelhoTiago Armando Coelho
Can you please post a print screen how are you trying to create\Update the trigger 
Marlon Agard 2Marlon Agard 2
This is on contact page;

User-added image

this is on the accounts page;

User-added image
I need the contact field to update the account field.
Tiago Armando CoelhoTiago Armando Coelho
Can you show me where are you creating the trigger?
Srikanth sfdc 32Srikanth sfdc 32
Hello  Marlon Agard

You should write trigger on the Contact Object not on Account Object okay. here i am sharing code for you .. check it and if my solution solves the problem make it as a  best Answer.

trigger Accountfieldupdation on Contact (after update) 
{
     set<id> setids = new set<id>();
       
     list<schema.Account> aclist = new list<schema.Account>();
     
     list<schema.Account> acupdate = new list<schema.Account>();
     
     
     for(schema.contact ct : trigger.old)
     {
        setids.add(ct.Accountid);
        
     }
     
     
    aclist = [select id,name,Description from Account where id IN:setids]; 
     
    
    
     for(schema.contact ctt : trigger.new)
     {
       if(ctt.Accountid!=null)
       {
         for(schema.Account aa : aclist) 
         {
       
           if(ctt.Type__c == 'Positive')
           {
              aa.Description = 'Hello I am new to Salesforce Account';
       
       
           }
           
           acupdate.add(aa);
       
         }   
       }
     
       
    }  
     
    update acupdate ; 
  

}

Thanks & Regards
Srikanth
Salesforce Developer
Marlon Agard 2Marlon Agard 2
an error is coming up with this line.

 aa.Description = 'Hello I am new to Salesforce Account';

I have tried to chnage it but still no joy.
Tiago Armando CoelhoTiago Armando Coelho
Hi Marlon.

What is the error?
Marlon Agard 2Marlon Agard 2
Error: Compile Error: Comparison arguments must be compatible types: Boolean, String at line 28 column 15
Tiago Armando CoelhoTiago Armando Coelho
This means that in this line ,if(ctt.Type__c == 'Positive'), the  Type__c is a boolen and not a string or a picklist can you please confirm?
Srikanth sfdc 32Srikanth sfdc 32
Hi  Marlon Agard 

Type is a custom field in my contact object and i am comparing this field with one of my picklist value i.e Positive.
I am not getting any Error. Can u please provide the correct error if possible please provide the screen shot. 
Marlon Agard 2Marlon Agard 2
please see screenshot
 User-added image