+ Start a Discussion
Stephanie_ArceStephanie_Arce 

Apex Trigger on Custom Object - Update Lookup Account

Hi, I was wondering if someone could help me with an Apex trigger I'm trying to write? I am completely new to coding, please bear with me -

 

I am working with a custom object and Accounts. In the custom object, when a checkbox = true, values in that record should update the corresponding Account's fields. Every record in this custom object is related to an Account so I tried at first creating a master-detail relationship and then using SF's workflows, but we will also need to see history on these custom object records (particularly which of our users checked the checkbox), which I've read you can't get using master-detail relationships so it's a lookup currently.

 

This is what I have so far - a record in this custom object will never have the checkbox ("Approved__c") selected upon insert, only upon update:

 

trigger CQApprovedUpsertChurch on Church_Questionnaire__c (before update) {
    List<Account> a = new List<Account> ();
    for (Church_Questionnaire__c ApprovedCQ: Trigger.New)
    if (ApprovedCQ.Approved__c = True)
        {
            a.add (new Account (
            CORE_Yearbook_ID__c = ApprovedCQ.Church_Yearbook_ID__c,
            Mailing_Street_1__c = ApprovedCQ.Mailing_Street_1__c,
            Mailing_Street_2__c = ApprovedCQ.Mailing_Street_2__c,
            Mailing_Street_3__c = ApprovedCQ.Mailing_Street_3__c,
            Mailing_City__c = ApprovedCQ.Mailing_City__c,
            Mailing_State_Province__c = ApprovedCQ.Mailing_State_Province__c,
            Mailing_Zip_Postal_Code__c = ApprovedCQ.Mailing_Zip_Postal_Code__c
            ));
        }
    upsert a Account.CORE_Yearbook_ID__c;
}

 

It is working to update the Account's address when the custom object record is saved and Approved__c = True, however when I edit that same record to uncheck Approved__c and save, it becomes checked again.

 

I tried changing my "before update" statement to "before insert, before update", and when that's in place, if I insert a record Approved__c is checked regardless of whether I actually checked it or not.

 

Can someone tell me what I'm doing wrong/what's wrong with my logic?

Best Answer chosen by Admin (Salesforce Developers) 
wiseguywiseguy

Hi Stephanie

 

change line 4 from

if (ApprovedCQ.Approved__c = True)

to

if (ApprovedCQ.Approved__c == True)

Note that the '=' operator is not the same as the '==' operator.

'=' assigns values to variables, '==' compares values.

 

The '=' operator assigns the value 'True' to the variable 'Approved__c'.

The result of this action is 'true' if it succeds (e.g. the field is not 'read only', therefore the if statement evaluates to true (so I suspect this works every time, also when the checkbox is unticked!).

 

The '==' operator will perfrom the comparison you are trying to perfrom.

 

For more reading, see here: http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_if_else.htm

and here (explanation for adobe code is the same for apex,Java, javascript etc): http://helpx.adobe.com/flash/kb/differences-equality-operator-assignment-operator.html

All Answers

wiseguywiseguy

Hi Stephanie

 

change line 4 from

if (ApprovedCQ.Approved__c = True)

to

if (ApprovedCQ.Approved__c == True)

Note that the '=' operator is not the same as the '==' operator.

'=' assigns values to variables, '==' compares values.

 

The '=' operator assigns the value 'True' to the variable 'Approved__c'.

The result of this action is 'true' if it succeds (e.g. the field is not 'read only', therefore the if statement evaluates to true (so I suspect this works every time, also when the checkbox is unticked!).

 

The '==' operator will perfrom the comparison you are trying to perfrom.

 

For more reading, see here: http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_if_else.htm

and here (explanation for adobe code is the same for apex,Java, javascript etc): http://helpx.adobe.com/flash/kb/differences-equality-operator-assignment-operator.html

This was selected as the best answer
Stephanie_ArceStephanie_Arce

Wow, I can't believe I didn't think to check that. Thank you so much!