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
BehzadBehzad 

How to update a field using the visualforce?

Hey all,

 

I wanted to update a field in a custom object using Visualforce.

 

The custom object that I am working on is called "Parts". And, the field that I'm looking to update in this custom object is called "Part Name."

 

The field "Part Name" is consisted of two fields "Part Model Name" and "Location Address." The "Part Model Name" is a field in the Parts object which stores the part model. The "Location Address" is a field in the Location obejct which stores location address.The Parts custom object looks up to a Location custom object- has a child relationship with the Location object.

 

Custom Object Relationship:

Location (Has a field called Location Address) --> Part (Has a field called Part Model Name)

 

Part Name Field in the Parts Object to be updated:

Part Name = Part Model Name @ Location Address

 

I currently use an extra field in the part object to grab the location address information, and I currently update the Part Name field via a Field update. However, I would like to get rid of that extra field and perform the field update through a Visualforce.

 

I don't have sufficient Visualforce knowledge to do this. Any help or comment is greatly appreciated.

 

Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
BehzadBehzad

Thank you so much for your input.

 

I think you're completely right.

 

This will be my first experience with the Triggers. 

 

Your help is greatly appreciated.

All Answers

Force2b_MikeForce2b_Mike

The VisualForce page is used to display information in a custom format or in something different than what a standard SalesForce.com page can do. To update a field value when a user submits a form on a VisualForce page you'd need to write Apex code that links to the page as a 'Controller'. However, from your description it sounds like what you're probably looking for is a Trigger (also Apex) - something that will update fields based on custom business logic after a record has been saved.

 

The example below may be something along the lines of what you need, though I just came up with this code so it hasn't been tested.

 

 

trigger Parts_Update on Parts (after insert, after update) { List<Parts> partsUpdate = New List<Parts>(); for (Parts p : Trigger.new) { Parts part = p.clone(); part.Part_Name__c = {some business logic goes here}; partsUpdate.add(part); } // Update the Property Objects Database.SaveResult[] srs = Database.Update(partsUpdate); for(Database.SaveResult sr : srs){ if(!sr.isSuccess()) Database.Error err = sr.getErrors()[0]; } }

 

There are lots of good examples at http://developer.force.com/. I'd be happy to help and can even write this for you on a contract basis if you'd like.

 

 

Best Regards,

 

Mike

 

BehzadBehzad

Thank you so much for your input.

 

I think you're completely right.

 

This will be my first experience with the Triggers. 

 

Your help is greatly appreciated.

This was selected as the best answer