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
Justin GareyJustin Garey 

Tigger to populate serial number lookup

I would like to add a trigger to populate a serial number lookup field after a case is created.  We have a second serial number number field that I would like to use as a text field and when the customer saves the case, this information is used to populate the serial number lookup for entitlements.  I would also need to return a null value if the serial number is not found in the lookup. 


@LaceySnr - Matt Lacey@LaceySnr - Matt Lacey
This is reasonably simple trigger, you just need to gather the serial numbers from the text field, query for the appropriate objects and then populate the lookup field.

Pseudocode:

TRIGGER ON CASE BEFORE INSERT/UPDATE

Map<String, SN__c> serialNumbers = new Map<String, SN__c>();

for(Case c : trigger.new)
{
  serialNumbers.put(c.Serial_Text_Field__c, null);
}

// Assuming name holds the value that would match the text field
for(SN__c sn : [select Id, Name from SN__c where Name in :serialNumbers.keySet()])
{
  serialNumbers.put(sn.Name, sn);
}

for(Case c : trigger.new)
{
  c.SN__c = serialNumbers.get(c.Serial_Text_Field__c);
}

Running before update/insert means not having to do a manual update on the modified records. As for returning null, if the number isn't found then this will leave the lookup field blank.

Good luck!