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
Tim__mTim__m 

Update contact account

Am I losing my mind? The snippet below isn't updating my contacts, I don't get any errors or anything it just dosn't update the records. The number of DML rows: 197 out of 10000 is reported and expected. There are no triggers in the org that would get in the way.

 

 

Account a = [select id, name from account where name='New Name'];
List<Contact> cList = [Select id, name, account.name from contact where account.name = 'Old Name'];
for(contact i : cList) {
    i.account = a;
}
update cList;

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Tim__mTim__m

Yes I am losing my mind... Time to take a break.

 

 

Account a = [select id, name from account where name='New Name'];
List<Contact> cList = [Select id, name, account.name from contact where account.name = 'Old Name'];
for(contact i : cList) {
    i.accountId = a.Id; // Thank you id field!
}
update cList;

 

 

All Answers

Tim__mTim__m

Yes I am losing my mind... Time to take a break.

 

 

Account a = [select id, name from account where name='New Name'];
List<Contact> cList = [Select id, name, account.name from contact where account.name = 'Old Name'];
for(contact i : cList) {
    i.accountId = a.Id; // Thank you id field!
}
update cList;

 

 

This was selected as the best answer
Imran MohammedImran Mohammed

Mark this as Accepted, as it will be helpful to forum users in implementing the code.

Cloud-LingCloud-Ling

Sirs, I have a question,

could I use List and Set Collections at the same time?

I'm creating a trigger for Account (before insert, before update) that'll have to avoid the duplication of Account names. like,

 

trigger ErrorDuplicateOutput on Account (before insert, before update)
{
  List<Account> a =new List<Account>();
  Set<String> b = new Set<String>{'Express Logistics and Transport','Edge Communications'};
  for(Account c:[SELECT Id, Name FROM Account WHERE Name ='Edge Communications'])
   {
    if (a != c)
    {
     b.add(c);
    }
    else
    {
     a.addError('Account name already exists.');
    }
   }
}

 

Please clear my logic on my code.
Help will be gladly appreciated.
Thanks for your post Tim__m!