+ Start a Discussion

How to make a custom filed as a primary key filed using Apex trigger

Hello Friends. I have created a  custom field and i need to make it as a primary  key field using Trigger.


Please help[ me in solving this problem.





Simply creaed a custom fields
set it as        


RequiredAlways require a value in this field in order to save a record
UniqueDo not allow duplicate values
  • Treat "ABC" and "abc" as duplicate values (case insensitive)
  • Treat "ABC" and "abc" as different values (case sensitive)
External IDSet this field as the unique record identifier from an external system



or u can write a after triige so u can copy the ID of the record in to u r custom fields






thanks , i need a smple code for this can u help .






Sample code for after trigger ??


trigger testtrg on testobj (After Insert)


 for(integer i = 0 ;i <trigger.new.sixe(); i++)

      trigger.new[i].primarykeycustomfield__c =  trigger.new[i].Id;