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
AshishyadavAshishyadav 

How to pull salesforce Id

Hi,

 

Iam trying to pull Salesforce id of record in Account on the basis of a record value (field: Duns number) from the contact 
Iam new to Triggers and Apex ..
please help me how to go about this,...  

Best Answer chosen by Admin (Salesforce Developers) 
craigmhcraigmh

If you're not setting the field of records affected by the original update that fired the trigger, you do need a DML statement. In this case:

 

List<Account> accts = [select id,name,Update_Value__c from account where id in : ids];
for(account a:accts)
{
	a.Update_Value__c = a.name;
}

update accts;

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

In salesforce id field is used for salesforce record id. So you have to simply make the SOQL on account to get the id. Try the below code as reference:

Trigger GetAccountId on Conatct(before insert)

{

List<id> ids=new list<id>();

For(contact c: trigger.new)

{

Ids.add(c.accountid);

}

For(account a:[select id,name from account where id in : ids])

{

// Do your manipulation here.

}

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

AshishyadavAshishyadav

Thanks for help..

For(account a:[select id,name from account where id in : ids])

{

   Here i tried to update a field that is in account but still field is not updating

}

SamuelDeRyckeSamuelDeRycke

It can help if you could post your code here so that we can have a look at it.   (And use the insert code feature for nice code readability)

AshishyadavAshishyadav

 

Trigger GetAccountId on Contact(before insert, before update)
{
   List<id> ids=new list<id>();
 For(contact c: trigger.new)
{
Ids.add(c.accountid);
}

For(account a:[select id,name,Update_Value__c from account where id in : ids])
{
  System.debug('@@@@@@'+a.id+a.name);
a.Update_Value__c = a.name;
}

}

 

 

 

//here update_value__c is the field in Account object

craigmhcraigmh

If you're not setting the field of records affected by the original update that fired the trigger, you do need a DML statement. In this case:

 

List<Account> accts = [select id,name,Update_Value__c from account where id in : ids];
for(account a:accts)
{
	a.Update_Value__c = a.name;
}

update accts;

 

This was selected as the best answer