You need to sign in to do that
Don't have an account?

Need help with trigger for cases involving products and assets
I am new to this, so please bear with me ...
I am trying to setup a trigger that will automatically populate the product of a case if a support tech enters a serial number, but if there is no serial number, leave the product field alone for the tech to use normally.
Within the Case object, I have the field Product (Product__c) which is a type Lookup(Product) and I have a field Serial Number (Serial_Number__c) which is a type Lookup(Asset). (Our assets' names are the same as the serial numbers)
Under the Asset object, I have a the standard field of Product (Product2) which is a type Lookup(Product).
I want the trigger to run after insert and after update. Essentially I want it to check the case to see if there is a Serial Number and if so, use the Product defined within the Asset (Serial Number). This way I will not have issues where we get the Serial Number later, enter it in and have the wrong product on the case.
Below is my meager code, I know I am not doing something right as it is giving me a compile error on the where clause that I put in. I don't think it is the right way to do it.
Thanks.
trigger updateProduct on Case (after insert, after update) { Case myCase = trigger.new[0]; if (myCase.Serial_Number__c) {myCase.Product__c = Asset.Product2.Name where Asset.Name = myCase.Serial_Number__c; update myCase;} }
To be honest, you have a long way to go with this. Honestly I suggest that you look for a developer within your organization. "Where" can not be used as you have here. As far as I know where can only be used in queries. Also you have not defined Asset at all. Salesforce has no way of knowing which asset you are refering to.
RustyThunder, You will want to do this in a beforeUpdate or BeforeInsert trigger. If you don't you will need to requery the case object then update it. This may cause you to run into govenor limits. Also you need to "bulkify" your triggers. SFDC like to batch records 200 at a time.
Try this trigger to see if it gets you what you needed.
best of luck
code - Thanks for the help. I was able to get the triger to save with your help. When I set a serial number by editing a case, it did not update the product. The trigger is active, so I am not sure what I am missing. Here is the updated code ... Thanks again.
RustyThunder, did you try putting some debug statements in to see if your getting results back from the assets query?