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
Bobby Steele 9Bobby Steele 9 

Update Text Field, IF Statement, Error: Expression cannot be a statement, Please help Super Simple

I am fairly new to writing triggers, so I took a shot at this one because I thought it would be pretty simple. I have looked a tons of example and can't seem to figure out what I am doing wrong. I would like to update base address information from a field called Metro Area when address is null. However I keep getting the error "Expression cannot be a statement at line 4 column 14".
Code is listed below:

trigger UpdateMailingAddressMetro on Contact (Before Insert, Before Update) {
    for(contact c: trigger.new){
        If(c.Metro_Area__c.contains('Houston') && (c.mailingcity == NULL) && (c.mailingstate == NULL) && (c.mailingpostalcode == NULL)){
            (c.mailingcity == 'Houston');
            (c.mailingstate == 'TX');
            (c.mailingpostalcode == '77001');
            (c.mailingcountry == 'United States');
                }else {
                    c.mailingstate == NULL;
                    }
        }
}
Bhanu MaheshBhanu Mahesh
Hi Bobby,

==(Logical Operator) is used only when we are comparing. We should not use it when assigning a value. In this case '='(assignment operator)should be used 

instead of (c.mailingcity == 'Houston'); use c.mailingcity = 'Houston';

Try the below code
 
trigger UpdateMailingAddressMetro on Contact (Before Insert, Before Update) {
    for(contact c: trigger.new){
        If(c.Metro_Area__c.contains('Houston') && (c.mailingcity == NULL) && (c.mailingstate == NULL) && (c.mailingpostalcode == NULL)){
			c.mailingcity = 'Houston';
			c.mailingstate = 'TX';
			c.mailingpostalcode = '77001';
			c.mailingcountry = 'United States';
		}
		else {
			c.mailingstate = NULL;
		}
    }
}

Thanks & Regards,
Bhanu Mahesh Gadi
Bobby Steele 9Bobby Steele 9
Bhanu,

Awesome! It worked. Thank you very much!

Best Regards,

Bobby