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
jkucera2jkucera2 

Does Update still fire if no data changes?

I'm pulling data from another system on a regular basis and wrote a method to match existing field values with the new field values.

 

If none of the fields are changed and I try to update in Apex, will that update actually do anything such as fire triggers, create chatter posts, etc?

 

Example to illustrate the question:

Campaign c=[select Id, Name from Campaign where Name = 'name'];
Campaign c2=new Campaign();

c2.Name='name';

        if(c.Name!=JSONCampaign.NAME&&c2.NAME!=NULL){
    		c.Name=c2.Name;
    	}//if 1

update c; // does this actually do anything?

 

Best Answer chosen by Admin (Salesforce Developers) 
jkucera2jkucera2

Thx guys - I confirmed that if no fields have changed, the udpate does fire triggers, but does NOT create chatter posts.

 

Techman97 - I was doing the updates in another method that returned the Campaign, and I couldn't think of a good way of determining if the returned campaign was any different than the input.

 

One idea I had was to create a class just to track the campaign + a boolean of whether it was updated, but I think I might create a dummy custom field and use that as the value can be passed in the returned campaign.  Its a bit funky, but I don't know of any customers at the custom field limit so I think it should be a workable solution.

All Answers

tes2tes2

Yes it will fire triggers, create chatter posts, etc?  assuming the criteria is met.

Andy BoettcherAndy Boettcher

What I'll do to avoid unnecessary updates like that is to declare an empty List object at the top of my method and add objects that need to be updated to that - and then update the list at the end.

 

Example:

 

List<Campaign> lstCampaignsToUpdate = new List<Campaign>();
		
Campaign c=[select Id, Name from Campaign where Name = 'name'];
Campaign c2=new Campaign();

c2.Name='name';

if(c.Name!=JSONCampaign.NAME&&c2.NAME!=NULL){
	c.Name=c2.Name;
	lstCampaignsToUpdate.add(c);
}//if 1

//update c; // does this actually do anything?
if(lstCampaignsToUpdate.size() > 0) { update lstCampaignsToUpdate; }

 Does that help?

 

-Andy

jkucera2jkucera2

Thx guys - I confirmed that if no fields have changed, the udpate does fire triggers, but does NOT create chatter posts.

 

Techman97 - I was doing the updates in another method that returned the Campaign, and I couldn't think of a good way of determining if the returned campaign was any different than the input.

 

One idea I had was to create a class just to track the campaign + a boolean of whether it was updated, but I think I might create a dummy custom field and use that as the value can be passed in the returned campaign.  Its a bit funky, but I don't know of any customers at the custom field limit so I think it should be a workable solution.

This was selected as the best answer