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
RustyThunderRustyThunder 

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;}

}

 

sroberts_MEsroberts_ME

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.

myforcedotcommyforcedotcom

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.

trigger updateProduct on Case (before insert, before update) {
	Set<String> serialNums = new Set<String>();
	for(Case c : trigger.new){
		if(c.Serial_Number__c != null)
		{
			serialNums.add(c.Serial_Number__c);
		}
	}
	List<Asset> assets = [Select name, Product2 from Asset where name in: serialNums];
	Map<String, Asset> assetsMap = new Map<String, Asset>();
	for(Asset a : assets){
		assetsMap.put(a.Name, a);
	}
	
	for(Case c : trigger.new){
		if(c.Serial_Number__c != null){
			try{
				c.Product__s = assetsMap.get(c.Serial_Number__c).Product2;
			}
			catch(Exception ee){
				
			}
		}
	}
}

 

best of luck

 

RustyThunderRustyThunder

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.

 

trigger updateProduct on Case (before insert, before update) {
    Set<String> serialNums = new Set<String>();
    for(Case c : trigger.new){
        if(c.Serial_Number__c != null)
        {
            serialNums.add(c.Serial_Number__c);
        }
    }
    List<Asset> assets = [Select name, Product2id from Asset where name in: serialNums];
    Map<String, Asset> assetsMap = new Map<String, Asset>();
    for(Asset a : assets){
        assetsMap.put(a.Name, a);
    }
    
    for(Case c : trigger.new){
        if(c.Serial_Number__c != null){
            try{
                c.Product__c = assetsMap.get(c.Serial_Number__c).Product2id;
            }
            catch(Exception ee){
                
            }
        }
    }
}

 

myforcedotcommyforcedotcom

RustyThunder, did you try putting some debug statements in to see if your getting results back from the assets query?