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
Alfia Quadri 11Alfia Quadri 11 

how to update contact record when salesforce id is available


I am new to apex, I am writing an apex class and I have the salesforce id, how do I update the contact record if I have the salesforce id.

Thanks
visraelfsvisraelfs
Hi,

You first have to retrive the recordo with a SOQL query and storing in a variable after that, you can use this variable to update record.
 
Contact myContact = [Select id, name, field__c from Contact where id= '0000dadfadf0013'];

MyContact.field__c = 'A random value';

update MyContact;

Regards
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

For the above scenerio you can just use the code as below. You may not need to query the contact 
 
CONTACT CON= NEW CONTACT();
CON.ID='0035j00000mtELqAAM';
CON.LASTNAME='CONUPDATE';
UPDATE CON;
just replace the id above with your id and fields as per your requirement.

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
 
Vishnu YaraVishnu Yara
Hi,Alfia Quadri 11
As you have contact record id get that record from database using SOQL,Then update the required field value
 
contact con = [select id,LastName,fieldName__c from contact where id = 'Enter the contact id you have'];
con.fieldName__c = 'Enter your required value';
update con;


Thanks,
denis banksdenis banks
No, Record Id is system generated and cannot be edited or deleted. It is generated every time a new record is inserted into the application. You can read more about Id here. Save this answer.

https://www.krowd-darden.com/
Tilottama Deore 5Tilottama Deore 5
public class Updatecontact{
         public static Contact updateContactDetails(String conId){
                for(Contact con:[SELECT Id, FirstName, LastName, Email FROM Contact WHERE Id=conId]){
                     con.FirstName='Your FirstName';
                     con.LasttName='Your LasttName';
                     con.Email='Your Name';
                }
             update con;
             return con;
}
}
Siddharth KhambeSiddharth Khambe
contact contactObject=new contact();
contactObject.id='your id';
contactObject.lastname='Test';
update contactObject;
(Please include all mandatory fileds for DML)
seo denizliseo denizli
SEO Denizli (https://seodenizli.com.tr/)'de kendi websitenizin ihtiyacı olan tüm SEO yöntemlerini doğru bir şekilde uygulanır.
Arun Kumar 1141Arun Kumar 1141

Hi Alfia,

If you already know the contact ID, you can use it in the query to retrieve the contact and then update it.

You can use the code below as a refrence.

id conId = 'contact id';  //put contact id here

Contact con = [SELECT id, Last_Name FROM Contact WHERE id = : conId];

con.Last_Name = 'Updated Value';

Update con;

Please mark it best answer if it helps.

Thanks