You need to sign in to do that
Don't have an account?
damo2929
Problem building a naming trigger
I have a trigger that am trying to build a name from the names of two other objects that are linked by this object.
below is my code
trigger trgAssetsProducts on Asset_Products__c (before insert) { for (Asset_Products__c c : trigger.new) { List<Asset> Assets = [ SELECT Id, Name From Asset WHERE Asset.Id in c.Asset__c ]; List<Product2> Products = [ SELECT id, Name From Product2 WHERE product2.Id = c.Product__c ]; Trigger.new[c].Name = string.valueof(Assets[c].Name) + '_' +string.valueof(Products[c].Name); } }
but i get the following error
Description Resource Path Location Type
Save error: unexpected token: 'c.Asset__c' trgAssetsProducts.trigger /Damien's Toybox/src/triggers line 8 Force.com save problem
What am I doing wrong here ?
Hi,
[SELECT Id, Name From Asset WHERE Asset.Id IN c.Asset__c];
This query is not properly framed. you use IN only when you have to check inside a collection ( a set or a map). Here you are directly comparing it with an id so you should just check it with a "="
Also,you have 2 Queries inside a FOR loop, this isn't a good practice. if you perform bulk insert and if your trigger.new has more than 100 records, your no. of queries will be 200 which isn't allowed.
Changed it to '=' but the problem persists. with the unexpected token : c.Assest__c
binding uses a ':' - try ':c.asset__c'
yes i missed that.
He's right.
[SELECT Id, Name From Asset WHERE Asset.Id = :c.Asset__c];