You need to sign in to do that
Don't have an account?
Marlon 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;
}
}
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;
}
}
What error is being triggered?
I got this error
Error: Compile Error: Incorrect SObject type: Account should be Contact at line -1 column -1
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];
this is on the accounts page;
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
aa.Description = 'Hello I am new to Salesforce Account';
I have tried to chnage it but still no joy.
What is the error?
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.