You need to sign in to do that
Don't have an account?
Product2 APEX error
I created a field called Product2.contact__c which is a lookup against the contact record. I am trying to create a field update that pulls in the contact.consultant_region__C field into the Product2.consultant_region__C field by associating the reference ID in the contact__C field after a Product2 record is saved. But it keeps saying that the it's incompatible. Can we not reference IDs from a custom field in APEX?
Description Resource Path Location Type
Save error: Incompatible element type Schema.SObjectField for collection of Id Product_Automation.cls DLC Sandbox/src/classes line 12 Force.com save problem
public class Product_Automation
{
public static void UpdateProductContactInfo(Product2[] ProdRec)
{
Set<Id> ContIds = new Set<Id>();
for (Product2 prod : ProdRec)
{
ContIds.add(Product2.Contact__c);
}
Map<Id,String> ContactInfoMap = new Map<Id, String>();
for(Contact c: [select id, Consultant_Region__c from Contact where id IN :ContIds])
{
ContactInfoMap.put(c.id, c.Consultant_Region__c );
}
for (Product2 c:ProdRec)
{
if( c.IsActive = 'True' && c.consultant_region__c == null)
{
c.consultant_region__c = ContactInfoMap .get(c.Contact__c);
}
}
}
}
ContIds.add(Product2.Contact__c);
I guess this is the line in which you got error. If so, replace this line with "ContIds.add(Product2.Contact__r.Id);" This will work. But it may say an exception like "contact__r.Id is not retrieved form SOQL query".
Instead, I would suggest :-
- From trigger.new form a set of product2 ids.
- Write a query like :-Map<Id, String> product2Map = [Select contact__r.Consultant_Region__c from Product2 where ID in : product2IdSet];
- then, for (Product2 c:ProdRec)
{
if( c.IsActive = 'True' && c.consultant_region__c == null)
{
c.consultant_region__c = Product2Map .get(c.Id);
}
}
I hope this helps you.....