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
kevin.chileskevin.chiles 

Adding product into a custom objects lookup field

I am trying to add a product into a lookup field on my custom object Category_Item__c.  The lookup field is Item__c.  I am trying to match on the Names of the records, for instance.  Producta is the name of the Category Item and Producta is the name of my product, if these 2 are the same the product lookup should populate with the product id, and tie the two records together.  Here is what I have so far.

 

trigger ProductAssociation on Category_Item__c (after insert, after update) { for(Category_Item__c cid:[select Id, CatName__c from Category_Item__c]);{ Product2 prod = [Select Id From Product2 Where Product_Name__c = cid.CatName__c]; for(Category_Item__c cid: trigger.new) if(cid.Item__c==null) cid.Item__c== prod; update Category_Item__c; } }

 

I keep getting unexpected token errors on line 4 for my field CatName__c.  this is stating that the variable does not exist even though I am stating it in the 2nd line.  What am I doing wrong here?

Best Answer chosen by Admin (Salesforce Developers) 
kevin.chileskevin.chiles

Thanks for the suggestions guys!  I went ahead and rewrote this to be a lot simpler that I was running it before.  Here is the end product for Product lookup population in a custom object

 

trigger ProductAssociation on //my object// Category_Item__c (before insert, before update) {
        

  for (Category_Item__c ci : Trigger.new) {

//calling product table & mapping the two object fields 
    Product2 p3 = [Select Id,Product_Name__c From Product2 Where Product_Name__c=:ci.Name ];

//placing the product Id in the custom lookup field 
    try {
      ci.Item__c = p3.Id;
    } catch (Exception e) {
      system.debug ('Exception : ' + e);
    } 
  }
}

 

 

 

All Answers

k_bentsenk_bentsen

You're forgetting the colon in your SOQL query; any time you're doing a comparison against an field name and not a hard-coded value, you need to preceed it with a colon. So,

 

Product2 prod = [Select Id From Product2 Where Product_Name__c = :cid.CatName__c];

 

Also, you update line should read update cid; and not update Category_Item__c;

 

Thirdly,  you have an SOQL query inside a loop, which is not in-line with Apex best practices. I would recommend you try to move it if you know how to.

k_bentsenk_bentsen

Also, you're not retrieving the Product_Name__c value for Product2 but you're using it for a comparison, which will throw an error:

 

Product2 prod = [Select Id, Product_Name__c From Product2 Where Product_Name__c = :cid.CatName__c];

kevin.chileskevin.chiles

Thanks for the suggestions guys!  I went ahead and rewrote this to be a lot simpler that I was running it before.  Here is the end product for Product lookup population in a custom object

 

trigger ProductAssociation on //my object// Category_Item__c (before insert, before update) {
        

  for (Category_Item__c ci : Trigger.new) {

//calling product table & mapping the two object fields 
    Product2 p3 = [Select Id,Product_Name__c From Product2 Where Product_Name__c=:ci.Name ];

//placing the product Id in the custom lookup field 
    try {
      ci.Item__c = p3.Id;
    } catch (Exception e) {
      system.debug ('Exception : ' + e);
    } 
  }
}

 

 

 

This was selected as the best answer