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
SteveT2121SteveT2121 

trigger to update lookup field value from picklist value

i figured this should be pretty simple. I have a custom object called ONSITE_PRODUCT__c

onsite_product__c has a lookup field called product__c which has a lookup relationship to the salesforce products object. 

 

ive created a new picklist field called New_products__c which also has a list of the same products as the product__c lookup field. I want a user to select a product from the New_products__c picklist and have that same product value saved as the product lookup field value.

 

heres my trigger

 

trigger UpdateOnsiteProduct on Onsite_Product__c (before insert,before update) {
    { 
        for (Onsite_Product__c a : Trigger.new) 
        { 
            a.Product__c = a.New_Products__c;
        }
    }
}

 

 

and this is the error i keep receiving

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger UpdateOnsiteProduct caused an unexpected exception, contact your administrator: UpdateOnsiteProduct: data changed by trigger for field Product: id value of incorrect type: XYZ PRODUCT

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
jbroquistjbroquist

When dealing with a lookup field, you are going to want to set the value of the field to the Id of the record you are attempting to relate it to. Your trigger should look something like this:

 

trigger UpdateOnsiteProduct on Onsite_Product__c (before insert,before update) 
{
	//create map of products by name
	Map<String, Product2> productMap = new Map<String, Product2>();
	for(Product2 p : [SELECT Id, Name FROM Product2])
	{
		productMap.put(p.Name, p);
	}

	
    for (Onsite_Product__c a : Trigger.new) 
    { 
    	//retrieve product id from product map
        a.Product__c = productMap.get(New_Products__c);
    }
}

 

 

All Answers

jbroquistjbroquist

When dealing with a lookup field, you are going to want to set the value of the field to the Id of the record you are attempting to relate it to. Your trigger should look something like this:

 

trigger UpdateOnsiteProduct on Onsite_Product__c (before insert,before update) 
{
	//create map of products by name
	Map<String, Product2> productMap = new Map<String, Product2>();
	for(Product2 p : [SELECT Id, Name FROM Product2])
	{
		productMap.put(p.Name, p);
	}

	
    for (Onsite_Product__c a : Trigger.new) 
    { 
    	//retrieve product id from product map
        a.Product__c = productMap.get(New_Products__c);
    }
}

 

 

This was selected as the best answer
SteveT2121SteveT2121

thanks for this. i think we're real close, im not sure which should be onsite_product__c and which should be product__c

 

onsite_product__c is the object, product__c is the lookup field

 

Error: Compile Error: sObject type 'Product__c' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 5 column 24

jbroquistjbroquist

I think I'm understanding you better now, try this...

 

trigger UpdateOnsiteProduct on Onsite_Product__c (before insert,before update) 
{
	//create map of products by name
	Map<String, Product2> productMap = new Map<String, Product2>();
	for(Product2 p : [SELECT Id, Name FROM Product2])
	{
		productMap.put(p.Name, p);
	}

	
    for (Onsite_Product__c a : Trigger.new) 
    { 
    	//retrieve product id from product map
        a.Product__c = productMap.get(New_Products__c);
    }
}

 

 

 

SteveT2121SteveT2121

the tigger code in the last 2 posts looks the same. is that right?

SteveT2121SteveT2121

im bombing out here

Compile error at line 12 column 39
Variable does not exist: New_Products__c

 

if i change line 12 to

 

	  for (Onsite_Product__c a : Trigger.new) 
    { 
    	//retrieve product id from product map
        a.Product__c = productMap.get(a.New_Products__c);
    }

 i get

 

Compile error at line 11 column 9

Illegal assignment from SOBJECT:Product2 to Id