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; 
}
}

2 Answers 2

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.

7
  • 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 :) Commented Jul 19, 2019 at 8:26
  • Is that working for you? Commented Jul 19, 2019 at 8:26
  • When i check I'ill write here :) Commented Jul 19, 2019 at 8:28
  • its worked but no changed anything Commented Jul 19, 2019 at 8:39
  • by the way can i get Account.Name in keySet() ? Commented Jul 19, 2019 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.

5
  • but when i give limit 1 i can update ownerId's i guess i have another thing problem maybe its working asynchronous Commented Jul 19, 2019 at 8:17
  • can you try removing static from UpdateContactOwnerString and invoke it on instance? Commented Jul 19, 2019 at 8:53
  • i took that: Non static method cannot be referenced from a static context: void Commented Jul 19, 2019 at 8:56
  • it should be invoked like new EditRecordContacts().UpdateContactOwnerString(name,ownerid); Commented Jul 19, 2019 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 Commented Jul 19, 2019 at 11:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .