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
sHollersHoller 

trigger field update in related object

Hi All,

 

I'm trying to have a trigger run when I create a new Tickets object.  When you create a Tickets object you define a related Contact object, and I'd like to change the value of a field (ticketsTest__c) of the Contact.

 

This is all done on the front-end, and only one Tickets obj will ever be created at a time so I didn't use a list.  I'm sure that's bad form, just a heads-up.  I'm trying to select the Contact record where the ID is equal to the related Contact object ID of the newly created Tickets obj.  In my test class I create a Contact and set the value of ticketsTest__c to 'blah', and the system.assert result says that the actual result of the field is still 'blah' rather than 'testPopulated'.  Also, I'm getting a "Save error: unexpected token: 'tixID'".  Can anyone please help?  It would be much appreciated.

 

Here's the code I've got right now for the trigger:

 

trigger TicketCreatePopulateContact on Tickets__c (before insert, before update) {

 

for(Tickets__c ticket: Trigger.New) {

    Id tixID = ticket.Contact__c;

    Contact s = [select Id, ticketsTest__c from Contact where Id = tixID];

    s.ticketsTest__c = 'testPopulated';

    update s;

    }

}

Best Answer chosen by Admin (Salesforce Developers) 
EJWEJW

It's because you need to requery that record before checking it's value.  Since the value was updated in a trigger when you inserted it, the new value for that field is in the database but not in your local variable.  So if you add this line before your assertation it should succeed:

 

c = [SELECT ticketsTest__c FROM Contact WHERE Id = :c.Id LIMIT 1];

 

When you insert a record the only field that is automatically updated in your local variable(s) is the Id field.

All Answers

EJWEJW

The variable in your contact query needs a colon in front of it, that's causing your error and preventing your update.  Try this:

 

for(Tickets__c ticket: Trigger.New) {
    Id tixID = ticket.Contact__c;
    Contact s = [select Id, ticketsTest__c from Contact where Id = :tixID];
    s.ticketsTest__c = 'testPopulated';
    update s;
    }
}

 All variables inside a SOQL query like that need to be prepended with a colon to indicate they're variables and not a literal part of the query.

sHollersHoller

Thanks very much, that helps get ride of that error.  Sorry if I'm being thick- do you have any idea why the system.assertEquals('testPopulated', c.ticketsTest__c); still fails?

 

Here's a snippet of the test code:

 

test.startTest();

Account a = new Account (Name = 'Company');

insert a;

Contact c = new Contact (Account = a, LastName = 'LName', ticketsTest__c = 'blah');

insert c;

 

System.assertEquals('LName', c.LastName);

Tickets__c ticket = new Tickets__c (Contact__c = c.Id);

insert ticket;

 

System.assertEquals(c.Id, ticket.Contact__c);

System.assertEquals('testPopulated', c.ticketsTest__c);

 

test.stopTest();

EJWEJW

It's because you need to requery that record before checking it's value.  Since the value was updated in a trigger when you inserted it, the new value for that field is in the database but not in your local variable.  So if you add this line before your assertation it should succeed:

 

c = [SELECT ticketsTest__c FROM Contact WHERE Id = :c.Id LIMIT 1];

 

When you insert a record the only field that is automatically updated in your local variable(s) is the Id field.

This was selected as the best answer
sHollersHoller

Ah, you are the man, I owe you a beer whenever you are in NYC.