0

Hello friends i was updating ownerId records firstly when i give 'limit 1' i could change but after i wanted to change 10 record system gave error message

And i tell somethings about that am sending 2 parameter and my method making update in this way Accounts with Contacts Owners going the same

Below my error message:

System.DmlException: Update failed. First exception on row 0 with id 0031t00000BrpLpAAJ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MC4SF.Contact: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a2b1t000000ZChTAAW; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] (MC4SF): []

Anon Apex Page:

List<Account> acc = new List<Account>();
    acc = [SELECT OwnerId,Name From Account where Cari_Grup_Kodu__c IN('CO') Limit 2];
        if(acc.size() > 0)
        {
            //List <String> Names = new List <String>();
            //List<String> OwnerIds = new List <String>();
            for (Account acc_i : acc)
            {
                //Names.add(acc_i.Name);
                //OwnerIds.add(acc_i.OwnerId);
                //ee.UpdateContactOwner(Names[i]);

                EditRecordContacts.UpdateContactOwnerString(acc_i.Name,acc_i.OwnerId);

            }

        }

And Apex class page:

public class EditRecordContacts {

public static string UpdateContactOwnerString(String name,String OwnerIds)
{
    system.debug(name);
    system.debug(OwnerIds);
    List<Contact> contacts = new List<Contact>();
    contacts = [Select OwnerId,Account.Name From Contact WHERE Account.Name = :name];
    if(contacts.size() > 0)
    {
        system.debug(contacts);

        for(Contact coo_i : contacts)
        {
            coo_i.OwnerId = OwnerIds;
        }
    update contacts;
    }
    return name; 
}
}
1

There are couple of issue in your code. You are performing SOQL query as well as DML operation in the for loop.

Here is a guidance, though I have not compiled the code:

//create a map of Account records.
Map<Id,Account> accMap = new Map<Id,Account>();
accMap = new Map<Id,Account>([SELECT OwnerId,Name From Account 
                    where Cari_Grup_Kodu__c IN('CO') Limit 2]);

if(accMap.keyset().size()>0)
{
    EditRecordContacts.UpdateContactOwnerString(accMap);
}


public class EditRecordContacts {

    public static void UpdateContactOwnerString(Map<Id,Account> accMap)
    {
        List<Contact> contacts = new List<Contact>();
        contacts = [Select OwnerId,AccountId From Contact
                    WHERE AccountId = :accMap.keySet()];
        if(contacts.size() > 0)
        {
            system.debug(contacts);

            for(Contact coo_i : contacts)
            {
                coo_i.OwnerId = accMap.get(coo_i.AccountId).OwnerId; //retrieve from the map based on AccountId.
            }
            update contacts;
        }

    }
}

By the way, you can perform the above logic in a configurable way using Process Builder.

| improve this answer | |
  • ohh yes i got it this i'ill try and i know process builder i wanted to learn apex cause i wanna change suddenly and i didnt know good yet salesforce i learning already i couldnt take education about that :D but this site so helping me and in the future i'ill use apex in php i have to learn somemore :) thanks for your answer :) – Furkan Kuyular Jul 19 '19 at 8:26
  • Is that working for you? – Santanu Boral Jul 19 '19 at 8:26
  • When i check I'ill write here :) – Furkan Kuyular Jul 19 '19 at 8:28
  • its worked but no changed anything – Furkan Kuyular Jul 19 '19 at 8:39
  • by the way can i get Account.Name in keySet() ? – Furkan Kuyular Jul 19 '19 at 8:44
0

There are different issues here like SOQL query and DML statement in for loop. Apart from that I think you should also show your code in after update of your contact - in which you are getting error.

| improve this answer | |
  • but when i give limit 1 i can update ownerId's i guess i have another thing problem maybe its working asynchronous – Furkan Kuyular Jul 19 '19 at 8:17
  • can you try removing static from UpdateContactOwnerString and invoke it on instance? – salesforce-sas Jul 19 '19 at 8:53
  • i took that: Non static method cannot be referenced from a static context: void – Furkan Kuyular Jul 19 '19 at 8:56
  • it should be invoked like new EditRecordContacts().UpdateContactOwnerString(name,ownerid); – salesforce-sas Jul 19 '19 at 9:29
  • I found another problem in my soql command when i give limit 2 sending 2 id and my soql command have a WHERE = account.Id already writing there '3213','1321' two id i changed am using IN now – Furkan Kuyular Jul 19 '19 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.