Accreditation__c[] accr = [SELECT Id,Business_Plan__c,Account__r.parentId FROM Accreditation__c WHERE isActive__c = true AND is_branch__c = true AND            Business_Plan__c = null limit];
 Set<id> setid = new set<id>();
for(Accreditation__c accrd : accr){   
 Business_Plan__c[] busnpln = [select Name,Account__c from Business_Plan__c where Year__c = '2016' AND Partner_Account__c IN : setid];
 for(Accreditation__c accrdt : accr){
 for(Business_Plan__c busnp :busnpln){
 if(accrdt.Account__r.parentId == busnp.Account__c){
        accrdt.Business_Plan__c = busnp.Name;  

In Above code I'm assigning busnp.Name to accrdt.Business_Plan__c. But it is not accepting as accrdt.Business_Plan__c is a lookup field. where the error is

Invalid id: BP-19652. Here BP-19652 is Name. It is auto number.

If i give accrdt.Business_Plan__c = busnp.Name.id;

Error: Invalid foreign key relationship: Business_Plan__c.Name

How to assign the Name field to that lookup field?

1 Answer 1


After looking at your question yesterday and your additional info today. I think you need to approach your problem with code that should look like this. You need to create maps from your first query to use the values of to run the 2nd query after which you can use those maps to find your matches and do your updates. I'd recommend you run this in some kind of class structure rather than as anonymous apex.

Accreditation__c[] accr = [SELECT Id,Business_Plan__c,Account__c, Account__c.ParentId FROM Accreditation__c WHERE isActive__c = true AND is_branch__c = true];

map<Id,Id>acct2accred = new map<Id,Id>();
map<Id,Id>acct2parent = new map<Id,Id>();
map<Id,Id>acct2bizplan = new map<Id,Id>();
for(Accreditation__c accrdt : accr){
list<id>parentAccts = acct2parent.values();

map<Id,Business_Plan__c>BizPlnMap = new map<Id,Business_Plan__c>([select Id, Name, Account__c, Partner_Account__c, from Business_Plan__c where Year__c = '2016' AND Partner_Account__c IN: parentAccts]);

for(Accreditation__c acc : accr){
   // test below is superfluous, but may save time if not all biz plans are returned
   if(BizPlnMap.values().contains(acct2bizplan.get(acc)) ){ 
      for(Business_Plan__c bp:BizPln){
         if(acct2parent.get(acc.Account__c) == bp.Parent_Acount__c){
            acc.Business_Plan__c = bp;
update acc;
  • You used the above code and received that error? How many results do you receive when you run the 2nd query when the 1st has a limit of 1? Is there other code you're running at the same time? This code should be bulk safe, so are you running it inside of other code?
    – crmprogdev
    Commented Mar 30, 2016 at 13:04
  • Try using the code I provided above. None of the SOQL is inside of a for loop. I specifically wrote it that way to solve your issues. Did you even bother to try it?
    – crmprogdev
    Commented Mar 30, 2016 at 13:46
  • Hi i updated my question. Can you please give me suggestion. I don't want to run class or trigger anywhere. Executing in anonymous would be better to me. So kindly give me suggestion please. I appreciate for your help Commented Mar 30, 2016 at 18:51
  • In your code the line ` acc.Business_Plan__c = bp;` is not accepting. It firing Error: **Illegal assignment from Business_Plan__c to Id ** If i give ` accrdt.Business_Plan__c = busnp.id;` It is executing but records are not updating with name. If i check debug logs, logs showing assigning but records are not updating with name field. Commented Mar 31, 2016 at 7:37

You must log in to answer this question.

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