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
Chris - ARIChris - ARI 

Expression cannot be a Statement

Im sure this is a simple mistake as I do not have much coding experience. It says my error is on line 5.

I am trying to update the field GUI on the account if it is blank. I want it to update with the Candian GUI if there is an accout populated in the related field "Canada_Account".
 
trigger Update_GUI on Account (before insert, before update) {
    for(Account acc : Trigger.new){
        if(acc.GUI__c = null){
            if(acc.Canada_Account__c != null){
                acc.GUI__c == acc.Canada_Account__r.GUI__c;
            }
        }
    }
}

 
Best Answer chosen by Chris - ARI
Amit Chaudhary 8Amit Chaudhary 8
PLease update your apex class like below. I just added field in query
public with sharing class AccountTriggerHandler 
{
	public static Boolean isFirstTime = true;
    
    public void OnAfterInsert(List<Account> newAccount)
    {
		Set<Id> setAccId = new Set<ID>();
		for(Account acc : newAccount)
		{
			if(acc.GUI__c == null)
			{
				setAccId.add(acc.id);
			}
		}
		
		List<Account> lstAccount = [select id,GUI__c,Canada_Account__r.GUI__c, UK_Account__r.GUI__c from account where id in :setAccId  ];
		List<Account> lstOfAccountToUpdate = new List<Account>();	
		
		for( Account acc : lstAccount )
		{
			if(acc.GUI__c == null)
			{
				if(acc.Canada_Account__c != null  )
				{
					acc.GUI__c = acc.Canada_Account__r.GUI__c;
					lstOfAccountToUpdate.add(acc);
				}
				else if(acc.UK_Account__c != null )
				{
					acc.GUI__c = acc.UK_Account__r.GUI__c;
					lstOfAccountToUpdate.add(acc);
				}
			}
		}
		
		// After insert we need to do DML to update the record.	
		if(lstOfAccountToUpdate.size() > 0 )
		{
			update lstOfAccountToUpdate;
		}
		
    }
	
    public void OnBeforeUpdate(List<Account> newAccount)
    {
	
		Set<Id> setAccId = new Set<ID>();
		for(Account acc : newAccount)
		{
			if(acc.GUI__c == null)
			{
				setAccId.add(acc.id);
			}
		}
		
		Map<Id,Account> mapAccount = new Map<Id,Account> ([select id,GUI__c,Canada_Account__r.GUI__c, UK_Account__r.GUI__c from account where id in :setAccId  ]);
		
		for( Account acc : newAccount )
		{
			if(acc.GUI__c == null)
			{
				if(acc.Canada_Account__c != null && acc.id != null && mapAccount.containsKey(acc.id) )
				{
					acc.GUI__c = mapAccount.get(acc.id).Canada_Account__r.GUI__c;
				}
				else if(acc.UK_Account__c != null && acc.id != null && mapAccount.containsKey(acc.id) )
				{
					acc.GUI__c = mapAccount.get(acc.id).UK_Account__r.GUI__c;
				}
			}
		}
    }

}

Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
please remove == and use =
trigger Update_GUI on Account (before insert, before update) {
    for(Account acc : Trigger.new){
        if(acc.GUI__c = null){
            if(acc.Canada_Account__c != null){
                acc.GUI__c = acc.Canada_Account__r.GUI__c;
            }
        }
    }
}

Let us know if this will help you
 
Chris - ARIChris - ARI
Also need to change if(acc.GUI__c = null) to if(acc.GUI__c == null). Got my operators mixed up. Thanks
Chris - ARIChris - ARI
Unfortunately the GUI feild is not updating as expected. Any thoughts?
Amit Chaudhary 8Amit Chaudhary 8
To get the value from related object you need to query the field. Without query you can get ID only.

Please try to update your code like below
trigger Update_GUI on Account (before insert, before update) 
{
	
	Set<Id> setAccId = new Set<ID>();
	for(Account acc : Trigger.new)
	{
        if(acc.GUI__c == null)
		{
			setAccId.add(acc.id);
		}
	}
	
	Map<Id,Account> mapAccount = new Map<Id,Account> ([select id,Canada_Account__r.GUI__c from account id in :setAccId  ]);
	
	for(Account acc : Trigger.new)
	{
        if(acc.GUI__c == null)
		{
            if(acc.Canada_Account__c != null && acc.id && mapAccount.containsKey(acc.id) )
			{
                acc.GUI__c = mapAccount.get(acc.id).Canada_Account__r.GUI__c;
            }
        }
    }
}
Let us know if this will help you
 
SnarfSnarf
Trigger.New does not have acces to the parent object data.
U should make a query to fetch the GUI__c field on the Canada_Account__c.
Chris - ARIChris - ARI
This line is throwing an error.
Map<Id,Account> mapAccount = new Map<Id,Account> ([select id,Canada_Account__r.GUI__c from account id in :setAccId  ]);

 
Chris - ARIChris - ARI
Was "in" a typo? That is what is throwing the error "expecting right square bracket, found 'in'"
 
Map<Id,Account> mapAccount = new Map<Id,Account> ([select id,Canada_Account__r.GUI__c from account id in :setAccId  ]);

 
Stephanie DodsonStephanie Dodson
You are missing "where" I believe.

should be .... from account where id in :setaccid.....
Chris - ARIChris - ARI
Thanks Stephanie. So now I am getting another error furthe down the line.
"AND operator can only be applied to Boolean expressions:
 
if(acc.Canada_Account__c != null && acc.id && mapAccount.containsKey(acc.id) )
			{


 
Amit Chaudhary 8Amit Chaudhary 8

Please update your code like below
trigger Update_GUI on Account (before insert, before update) 
{
	
	Set<Id> setAccId = new Set<ID>();
	for(Account acc : Trigger.new)
	{
        if(acc.GUI__c == null)
		{
			setAccId.add(acc.id);
		}
	}
	
	Map<Id,Account> mapAccount = new Map<Id,Account> ([select id,Canada_Account__r.GUI__c from account where id in :setAccId  ]);
	
	for(Account acc : Trigger.new)
	{
        if(acc.GUI__c == null)
		{
            if(acc.Canada_Account__c != null && acc.id != null && mapAccount.containsKey(acc.id) )
			{
                acc.GUI__c = mapAccount.get(acc.id).Canada_Account__r.GUI__c;
            }
        }
    }
}

Please let us know if this will help you

Thanks
Amit Chaudhary
 
Chris - ARIChris - ARI
Thanks Amit. That solves all the errors and does work only if the Canada Account field is already populated.

Sorry to be a bother, but now I have 2 more questions.

1. I believe I need this code to fire after update and after insert to ensure the GUI field always populates. Simply changing up the first line of the code doesn't work. How do I accomplish this?

2. I want to add more countries. UK, Mexico, Gemany etc... I want the code the first check Canada and if there is nothing in the field to go to the UK etc.... How do I scale this code?
Chris - ARIChris - ARI
Ok I was able to scale it with an else if statement. I just need to know how to update after inser now.
 
trigger Update_GUI on Account (before insert, before update) 
{
	
	Set<Id> setAccId = new Set<ID>();
	for(Account acc : Trigger.new)
	{
        if(acc.GUI__c == null)
		{
			setAccId.add(acc.id);
		}
	}
	
	Map<Id,Account> mapAccount = new Map<Id,Account> ([select id,Canada_Account__r.GUI__c, UK_Account__r.GUI__c from account where id in :setAccId  ]);
	
	for(Account acc : Trigger.new)
	{
        if(acc.GUI__c == null)
		{
            if(acc.Canada_Account__c != null && acc.id != null && mapAccount.containsKey(acc.id) )
			{
                acc.GUI__c = mapAccount.get(acc.id).Canada_Account__r.GUI__c;
            }
            else if(acc.UK_Account__c != null && acc.id != null && mapAccount.containsKey(acc.id) )
            {
                acc.GUI__c = mapAccount.get(acc.id).UK_Account__r.GUI__c;
            }
        }
    }
}

 
SnarfSnarf
If u use after insert and after update u will need to commit your changes to the database.
Before triggers will do this for you if u change any fields in trigger.new.


 
Amit Chaudhary 8Amit Chaudhary 8
Hi Christopher Marzella 2,

You can implement the trigger framwork like below

Please create one handler class like below
public with sharing class AccountTriggerHandler 
{
	public static Boolean isFirstTime = true;
    
    public void OnAfterInsert(List<Account> newAccount)
    {
		Set<Id> setAccId = new Set<ID>();
		for(Account acc : newAccount)
		{
			if(acc.GUI__c == null)
			{
				setAccId.add(acc.id);
			}
		}
		
		List<Account> lstAccount = [select id,Canada_Account__r.GUI__c, UK_Account__r.GUI__c from account where id in :setAccId  ];
		List<Account> lstOfAccountToUpdate = new List<Account>();	
		
		for( Account acc : lstAccount )
		{
			if(acc.GUI__c == null)
			{
				if(acc.Canada_Account__c != null  )
				{
					acc.GUI__c = acc.Canada_Account__r.GUI__c;
					lstOfAccountToUpdate.add(acc);
				}
				else if(acc.UK_Account__c != null )
				{
					acc.GUI__c = acc.UK_Account__r.GUI__c;
					lstOfAccountToUpdate.add(acc);
				}
			}
		}
		
		// After insert we need to do DML to update the record.	
		if(lstOfAccountToUpdate.size() > 0 )
		{
			update lstOfAccountToUpdate;
		}
		
    }
	
    public void OnBeforeUpdate(List<Account> newAccount)
    {
	
		Set<Id> setAccId = new Set<ID>();
		for(Account acc : newAccount)
		{
			if(acc.GUI__c == null)
			{
				setAccId.add(acc.id);
			}
		}
		
		Map<Id,Account> mapAccount = new Map<Id,Account> ([select id,Canada_Account__r.GUI__c, UK_Account__r.GUI__c from account where id in :setAccId  ]);
		
		for( Account acc : newAccount )
		{
			if(acc.GUI__c == null)
			{
				if(acc.Canada_Account__c != null && acc.id != null && mapAccount.containsKey(acc.id) )
				{
					acc.GUI__c = mapAccount.get(acc.id).Canada_Account__r.GUI__c;
				}
				else if(acc.UK_Account__c != null && acc.id != null && mapAccount.containsKey(acc.id) )
				{
					acc.GUI__c = mapAccount.get(acc.id).UK_Account__r.GUI__c;
				}
			}
		}
    }

}

Update your Trigger like below
trigger Update_GUI on Account (After insert, before update) 
{
	AccountTriggerHandler handler = new AccountTriggerHandler();

	If(Trigger.isAfter)
	{
		
		handler.OnAfterInsert(trigger.New);
		AccountTriggerHandler.isFirstTime = false; // This line i added to avoid multipe execution
	}
	else if (Trigger.isBefore)
	{
		if( AccountTriggerHandler.isFirstTime )
		{
			handler.OnBeforeUpdate(trigger.New);
		}	
	}
}
Please check below post for more information
1) http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

NOTE:- code is not tested may be you will get some syntex error

Let us know if this will help you

Thanks
Amit Chaudhary
Chris - ARIChris - ARI
The code works with no errors however the GUI field still updates only if there is a change made to a record after Canada Account or UK Account has been populated.

When creating a new account it throws this error: 
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger Update_GUI caused an unexpected exception, contact your administrator: Update_GUI: execution of AfterInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.GUI__c: Class.AccountTriggerHandler.OnAfterInsert: line 21, column 1
Stephanie DodsonStephanie Dodson
I think if you just add in the GUI__c as a field being retrieved to your query, that should fix it.

List<Account> lstAccount = [select id,Canada_Account__r.GUI__c, UK_Account__r.GUI__c fromaccount where id in :setAccId  ];
Stephanie DodsonStephanie Dodson
so that should be List<Account> lstAccount = [select id, GUI__c, Canada_Account__r.GUI__c, UK_Account__r.GUI__c fromaccount where id in :setAccId  ];
Amit Chaudhary 8Amit Chaudhary 8
PLease update your apex class like below. I just added field in query
public with sharing class AccountTriggerHandler 
{
	public static Boolean isFirstTime = true;
    
    public void OnAfterInsert(List<Account> newAccount)
    {
		Set<Id> setAccId = new Set<ID>();
		for(Account acc : newAccount)
		{
			if(acc.GUI__c == null)
			{
				setAccId.add(acc.id);
			}
		}
		
		List<Account> lstAccount = [select id,GUI__c,Canada_Account__r.GUI__c, UK_Account__r.GUI__c from account where id in :setAccId  ];
		List<Account> lstOfAccountToUpdate = new List<Account>();	
		
		for( Account acc : lstAccount )
		{
			if(acc.GUI__c == null)
			{
				if(acc.Canada_Account__c != null  )
				{
					acc.GUI__c = acc.Canada_Account__r.GUI__c;
					lstOfAccountToUpdate.add(acc);
				}
				else if(acc.UK_Account__c != null )
				{
					acc.GUI__c = acc.UK_Account__r.GUI__c;
					lstOfAccountToUpdate.add(acc);
				}
			}
		}
		
		// After insert we need to do DML to update the record.	
		if(lstOfAccountToUpdate.size() > 0 )
		{
			update lstOfAccountToUpdate;
		}
		
    }
	
    public void OnBeforeUpdate(List<Account> newAccount)
    {
	
		Set<Id> setAccId = new Set<ID>();
		for(Account acc : newAccount)
		{
			if(acc.GUI__c == null)
			{
				setAccId.add(acc.id);
			}
		}
		
		Map<Id,Account> mapAccount = new Map<Id,Account> ([select id,GUI__c,Canada_Account__r.GUI__c, UK_Account__r.GUI__c from account where id in :setAccId  ]);
		
		for( Account acc : newAccount )
		{
			if(acc.GUI__c == null)
			{
				if(acc.Canada_Account__c != null && acc.id != null && mapAccount.containsKey(acc.id) )
				{
					acc.GUI__c = mapAccount.get(acc.id).Canada_Account__r.GUI__c;
				}
				else if(acc.UK_Account__c != null && acc.id != null && mapAccount.containsKey(acc.id) )
				{
					acc.GUI__c = mapAccount.get(acc.id).UK_Account__r.GUI__c;
				}
			}
		}
    }

}

Let us know if this will help you
 
This was selected as the best answer
Chris - ARIChris - ARI
This seems to work so far with testing. Thank you all so much for your help!