+ Start a Discussion
Mi NguyenMi Nguyen 

Trigger to insert custom object

Hi,

I'm a newbie in SF.
I try to create a simple application with some objects such as Employee, Contract, Payslip, Insurance History, Asset, Asset History.
Previously, I successfully insert Insurance History object in the Contract trigger (when new contract is created or updated). 
Now I would like to do similar thing - inserting Asset History object in the Asset trigger (when Asset is created or updated). 
However, the insertion of Asset History object does not work.

My observation is as follows:
- From Asset trigger
    --> cannot insert Asset History object
    --> can insert Insurance History object

- From Payslip trigger or Contract trigger
    --> cannot insert Asset object and Asset History object

Is there any difference between Asset/Asset History object and other objects?
Or is there any difference between Asset/Asset History trigger and object objects' trigger?
Could you please give me some advice?

Thanks,
Mi Nguyen.
Best Answer chosen by Mi Nguyen
Virendra ChouhanVirendra Chouhan
Hi Mi,

I dont know i am right or not. But as far as i know.
This record is not displayed in recent list because the list is shown Recently Viewed record by default.
so if you change it with Recently Created then it'll work as we want.

User-added image

Regards
Virendra

All Answers

Virendra ChouhanVirendra Chouhan
Hi Mi Nguyen,

I think there is no object name Asset History.

and if you create a custom object and want a history object also for  that, then make sure you checked on Track Field History option.


User-added image


Regards,
Virendra
Mi NguyenMi Nguyen
Hi Virendra,

The issue is that I cannot insert or update records of another objects in trigger.
The order of creating custom objects is as follows: Employee, Contract, Payslip, Insurance History, Asset, Asset History.
It not only happens with Asset and Asset History object, but with other custom objects created recently.
I'm really confused about this situation, wonder whether it is related with any limitation or security issue.

Regards,
Mi Nguyen
Virendra ChouhanVirendra Chouhan
Hi Mi Nguyen,

Is this all objects are Custom Object?

Make sure you use DML operation to insert another object record in trigger and if you send me your code snippt then i'll help you.

Kind regards
Virendra

Mi NguyenMi Nguyen
Hi Virendra,

Yes, all objects are custom objects.
Here is the code that I tried (just the sample code to confirm the insertion of Asset History object).
The strange thing to me is that I cannot insert both Asset and Asset History records in any trigger, but can do it in apex class. It happens to all custom records I created recently. 
However, previously I can insert Insurance History records in Contract or Payslip trigger.

---------------------------------------
trigger Store_Asset_History on Asset__c (before update) {

        Asset__c asset = Trigger.new[0];     

  
        Asset_History__c assetHistory = new Asset_History__c();
        assetHistory.Allocated_Date__c = asset.Allocated_Date__c;
        assetHistory.Asset_Name__c = asset.Name;
        assetHistory.Operation__c = asset.Operation__c;
        assetHistory.Status__c = asset.Status__c;
        assetHistory.Staff_ID__c = asset.Staff_ID__c;
        assetHistory.Updated_Date__c = Date.today();
  
        insert assetHistory;
  
}
--------------------------------------------

Thank you for your kind support.

Regards,
Mi Nguyen.
Virendra ChouhanVirendra Chouhan
Hi Mi Nguyen,

Use this code i hope it'll work
trigger Store_Asset_History on Asset__c (before update) {

list<Asset_History__c> AssetHistoryList= new list<Asset_History__c>();
     for(Asset__c asset: trigger.new)
      {
        Asset_History__c assetHistory = new Asset_History__c();

        assetHistory.Allocated_Date__c = asset.Allocated_Date__c;
        assetHistory.Asset_Name__c = asset.Name;
        assetHistory.Operation__c = asset.Operation__c;
        assetHistory.Status__c = asset.Status__c;
        assetHistory.Staff_ID__c = asset.Staff_ID__c;
        assetHistory.Updated_Date__c = Date.today();
 
        AssetHistoryList.add(assetHistory);
      }
    
     insert AssetHistoryList;
}


And this Trigger fire only when Asset__c object's record is update.


Regards
Virendra
version7.7@hotmail.com (mailto:version7.7@hotmail.com)
Mi NguyenMi Nguyen
Hi Virendra,

I tried but it does not work.

Regards,
Mi Nguyen.
Virendra ChouhanVirendra Chouhan
Hi Mi Nguyen

Are you clicking on Go button in Asset_History__c object's Tab?
User-added image


you can mail me anytime i'll help you.
Regards
Virendra
version7.7@hotmail.com
Mi NguyenMi Nguyen
Hi Virendra,

I see new records already.
I did not see it in the Recent list, so I think that record is not created.
I still don't understand why the newly created record is not displayed in the Recent list, but only displayed when clicking Go button.

Anyway, I would like to say thanks to your kind support.

Regards,
Mi Nguyen.

Virendra ChouhanVirendra Chouhan
Hi Mi,

I dont know i am right or not. But as far as i know.
This record is not displayed in recent list because the list is shown Recently Viewed record by default.
so if you change it with Recently Created then it'll work as we want.

User-added image

Regards
Virendra

This was selected as the best answer
Mi NguyenMi Nguyen
 Hi Viendra,

It's right. Your advice is correct.
Thank you so much for your kind support.
Wish you have a nice day.

Best regards,
Mi Nguyen