HI, You are refering two different fields of account in code
trigger statuscontact on Contact (After insert,After Update) { //sts__c(account) status__c(contact) public static Boolean isFirstTime = true; if(isFirstTime){ isFirstTime = False; List<Account> accList = new List<Account>(); List<Contact> conList = new List<Contact>();
For(Contact con:[SELECT id,status__c,Account.Id,Account.status__c FROM Contact Where Id In:Trigger.newMap.keyset()]){ //SELECT id,Status__c,Account.Id,Account.Status__c FROM Contact Where Id In:Trigger.newMap.keyset()
You can try by modifying below sample code which is for contact phone number change.
Best Regards,
Sandhya
Please Try below code
//TRIGGER TO UPDATE ACCOUNT IF CONTACT STATUS CHANGES
trigger UpdateAccount on Contact (After insert,After Update) {
public static Boolean isFirstTime = true;
if(isFirstTime){
isFirstTime = False;
List<Account> accList = new List<Account>();
For(Contact con:[SELECT id,Status__c,Account.Id,Account.Status__c FROM Contact Where Id In:Trigger.new]){
if(con.Status__c == con.Account.Status__c || trigger.oldmap.get(con.Id).Status__c == con.Status__c){
System.debug('==========================');
}
else{
System.debug('Updating status on Account');
con.Account.Status__c = con.Status__c;
accList.add(con.Account);
}
}
System.debug('Account List'+accList);
update accList;
System.debug('UPdated Account Map'+accList);
}
}
//TRIGGER TO UPDATE CONTACT IF ACCOUNT STATUS CHANGES
trigger UpdateContact on Account (After insert,After Update) {
public static Boolean isFirstTime = true;
List<Contact> conList = new List<Contact>();
if(isFirstTime){
For(Account acc:[SELECT id,Status__c,(SELECT id,Status__c FROM Contacts) FROM Account WHERE ID IN:Trigger.new]){
for(Contact con:acc.Contacts){
if(trigger.oldmap.get(acc.Id).Status__c == acc.Status__c || con.Status__c == acc.Status__c){
System.debug('==========================');
}
else{
System.debug('Updating status on Contact');
con.Status__c = acc.Status__c;
conList.add(con);
}
}
System.debug('Contact List'+conList);
update conList;
System.debug('UPdated Contact Map'+conList);
}
}
}
Requirement is : whenever status field in account is updated to open itd associted contact also updated with the same value and vice versa Right?
Code Covers this scenario..
I have updated the code please try this one..
trigger UpdateAccount on Contact (After insert,After Update) {
public static Boolean isFirstTime = true;
if(isFirstTime){
isFirstTime = False;
List<Account> accList = new List<Account>();
List<Contact> conList = new List<Contact>();
For(Contact con:[SELECT id,Status__c,Account.Id,Account.Status__c FROM Contact Where Id In:Trigger.newMap.keyset()]){
if(Account.Status__c != NULL && con.Status__c == con.Account.Status__c ){
if(Trigger.isupdate && trigger.oldmap.get(con.Id).Status__c == con.Status__c){
System.debug('===========UPDATE===============');
}
System.debug('=========INSERT=================');
}
else{
System.debug('Updating status on Account');
if(con.Status__c != NULL){
con.Account.Status__c = con.Status__c;
accList.add(con.Account);
}
else{
Contact conObj = new Contact(id=con.id);
conObj.Status__c = con.Account.Status__c;
conList.add(conObj);
}
}
}
System.debug('Account List'+accList);
update accList;
update conList;
System.debug('UPdated Account Map'+accList);
}
}
trigger UpdateContact on Account (After insert,After Update) {
public static Boolean isFirstTime = true;
List<Contact> conList = new List<Contact>();
if(isFirstTime){
For(Account acc:[SELECT id,Status__c,(SELECT id,Status__c FROM Contacts) FROM Account WHERE ID IN:Trigger.newMAp.keyset()]){
if(acc!=NULL){
for(Contact con:acc.Contacts){
if(acc.Status__c != NULL && con.Status__c == acc.Status__c){
if(Trigger.isupdate && trigger.oldmap.get(acc.Id).Status__c == acc.Status__c){
System.debug('==========================');
}
System.debug('==========================');
}
else{
System.debug('Updating status on Contact');
con.Status__c = acc.Status__c;
conList.add(con);
}
}
System.debug('Contact List'+conList);
update conList;
System.debug('UPdated Contact Map'+conList);
}
}
}
}
Tahnk you!!
Try to identify any other possibilities in your org like flows or multiple triggers and turn them off
Or analyze code and try to idenify problem.
Here attached tested screenshots of my code.
Account with status completed
Created new contact associated with this account
Save contact .Status populated from account.
Thank you
Account with status open
Contact status updated to completed
Account status automatically updated to completed
Thank you
trigger statuscontact on Contact (After insert,After Update) { //sts__c(account) status__c(contact)
public static Boolean isFirstTime = true;
if(isFirstTime){
isFirstTime = False;
List<Account> accList = new List<Account>();
List<Contact> conList = new List<Contact>();
For(Contact con:[SELECT id,status__c,Account.Id,Account.status__c FROM Contact Where Id In:Trigger.newMap.keyset()]){
//SELECT id,Status__c,Account.Id,Account.Status__c FROM Contact Where Id In:Trigger.newMap.keyset()
if(Account.sts__c != NULL && con.status__c == con.Account.sts__c ){
if(Trigger.isupdate && trigger.oldmap.get(con.Id).Status__c == con.status__c){
System.debug('===========UPDATE===============');
}
System.debug('=========INSERT=================');
}
else{
System.debug('Updating status on Account');
if(con.status__c != NULL){
con.Account.sts__c = con.status__c;
accList.add(con.Account);
}
else{
Contact conObj = new Contact(id=con.id);
conObj.Status__c = con.Account.Status__c;
conList.add(conObj);
}
}
}
System.debug('Account List'+accList);
update accList;
update conList;
System.debug('UPdated Account Map'+accList);
}
}
Plese make Correct Change.
Thanks!!!!