+ Start a Discussion
Mi NguyenMi Nguyen 

Insert statement in trigger adds and delete records of other object

Hi guys,

I'm a newbie in salesforce.
I try to create 2 applications. The first application contains the Staff object, the second application contains the Asset object.
I created one more object named Asset History (ID is auto number).
I created a trigger on Asset object to add Asset History  data whenever Asset information is changed. However, no Asset History record is created. 
When I try to add Asset History record by clicking New button, the data is created and ID of new data is increased.

I guess that Asset History record was created an then deleted, but I don't understand the root cause of this issue.
Can anyone give me some hints?
Thanks.


Pramod_SFDCPramod_SFDC
Hi,

Can you post your Trigger code, so that we can analyse it accordingly.


Regards
Pramod
Mi NguyenMi Nguyen
Hi Pramod,

This is my trigger code.
I try to insert Asset History from the 1st application (which stores information about Staff, Contract) and it is ok, new Asset History record is created.
But from Asset trigger, it failed.
Is there any problem with object security or application permission?
Thanks for your support.
---------------------------------------------------------------------------
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;
   
}
--------------------------------------------------------------------------
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Trigger Store_Asset_History on Asset__c (after insert) {
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();
}
Database.insert( assetHistory);
}
try this it will work.mark it as best answer if so
Mi NguyenMi Nguyen
Hi Vidhyasagaran Muralidharan,

I tried but it does not work.

Regards,
Mi Nguyen
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
LIke my undestanding there are two objects like Asset__C and AssetHistory__c.if you insert a record in Assert__c a trigger should fire and  insert record on Asserthistory__C object. with some column name of assert__c
is that right?.
what is exact problem you are getting?
record is not inserted in assert__c or assertHistory__C?
lakshman169lakshman169
Hi Mi Nguyen,
Please use the below code to create AssetHistory__c records.

Trigger Store_Asset_History on Asset__c (after update) {

list<Asset_History__c> lstassethistory = new list<Asset_History__c>();

For(  Asset__c asset  : Trigger.new)
{
Asset_History__c assetHistory = new Asset_History__c();
assetHistory.Allocated_Date__c = trigger.oldmap.get(asset.id).Allocated_Date__c;
assetHistory.Asset_Name__c = trigger.oldmap.get(asset.id).Name;
assetHistory.Operation__c = trigger.oldmap.get(asset.id).oldmap.get(asset.id).Operation__c;
assetHistory.Status__c = trigger.oldmap.get(asset.id).Status__c;
assetHistory.Staff_ID__c = trigger.oldmap.get(asset.id).Staff_ID__c;
assetHistory.Updated_Date__c = Date.today();
lstassethistory.add(assetHistory);
}

if(lstassethistory.size() > 0)

INSERT lstassethistory;

}

after that you can check any other trigger is in the Asset_History__c object with DELETE operation.

Regards,
Lakshmanan
Mi NguyenMi Nguyen
Dear Lakshmanan,

I tried but got the following error.
Error: Compile Error: Invalid field Allocated_Date__c for SObject Asset_History__c at line 8 column 1

Regards, 
Mi Nguyen
lakshman169lakshman169
Hi Mi Nguyen,

Comment that line and check it. if its working well, check that Allocated_Date__c field data type in both object (Asset_History__c and Asset__c).

Regards,
lakshmanan
Mi NguyenMi Nguyen
Dear Vidhyasagaran Muralidharan, 

LIke my undestanding there are two objects like Asset__C and AssetHistory__c.if you insert a record in Assert__c a trigger should fire and  insert record on Asserthistory__C object. with some column name of assert__c
is that right?. 
==> This is right.
The problem is that the Asset_History__c record is not inserted.
I try to insert the Asset_History__c via GUI, using New button. The new Asset_History__c record can be created. I noticed the ID of Asset_History__c is increased for the new record.

So I guess that the insertion of Asset_History__c record in the Asset__c trigger can be done, but it seems that the newly inserted record is deleted right after created. So I cannot find it in the Attendance History list.

I also try to insert the Asset_History__c in the trigger of other object, the new Asset_History__c record can be created. 
I don't understand why the insertion of Asset_History__c is only failed in the Asset__c trigger.

Is there any problem with object security or application permission?
I'm very confused about this problem.
Could you please give me some hints?

Regards,
Mi Nguyen

Mi NguyenMi Nguyen
Hi Lakshmanan,

It's my typo mistake. 
It can be compiled. but the Asset_History__c object still cannot be inserted.

Regards,
Mi Nguyen.
lakshman169lakshman169
deactivate all the triggers in both object (Asset__c ,Asset_History__c) except this trigger and check this. if you didn't get any error the Asset_History__c record is create.
Mi NguyenMi Nguyen
Hi Lakshmanan, 

It still cannot be inserted hazza...
I try to insert the Asset_History__c via GUI, using New button. The new Asset_History__c record can be created. I noticed the ID of Asset_History__c is increased for the new record.

So I guess that the insertion of Asset_History__c record in the Asset__c trigger can be done, but it seems that the newly inserted record is deleted right after created. So I cannot find it in the Attendance History list.

I also try to insert the Asset_History__c in the trigger of other object, the new Asset_History__c record can be created.
I don't understand why the insertion of Asset_History__c is only failed in the Asset__c trigger.

Is there any problem with object security or application permission?
I'm very confused about this problem.
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Hi Mi Nguyen,

Between these AssertHistory__C and Assert__c have any relationship field.if so can give field details of these two objects

Mi NguyenMi Nguyen
Hi Vidhyasagaran Muralidharan,

Currently I put no relationship between these objects.

Regards,
Mi Nguyen
Mi NguyenMi Nguyen
Hi guys,

I see that the insertion can work in Apex controller but cannot work in trigger.
Do you have any advice for further checking?
Thanks.

Regards,
Mi Nguyen
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
HI MiNguyen,
your usecase can be acheived by trigger itself .
1)Like any delete triggers is there deactivate it in Assert_History__c and assert__c give a try.
2)Check the trigger you wrote is on object asset__c.
3)Trigger Store_Asset_History on Asset__c (after insert) {
Asset_History__c assetHistory = new Asset_History__c();
For(  Asset__c asset  : Trigger.new)
{
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();
}
Database.insert( assetHistory);
}
for this code what error you are getting if you insert a record in Asset__c.

Can you check this and update me
Mi NguyenMi Nguyen
Hi Vidhyasagaran Muralidharan,

I tried but it still failed.
Here are some phenomenon that I checked.
- Asset trigger cannot insert Asset History object
- Asset trigger can insert other objects
- Triggers of other objects can cannot insert Asset and Asset History object
- Apex classes can insert Asset and Asset History object.

I'm really confused in this case.
Actually, what is the difference between Asset and Asset History objects with other objects?
I have no other ways now for checking.
Could you please give me a hand?

Sincerely,
Mi Nguyen 

Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
I guess Assert_history  is a object which track the history of asset object(like any change in the object records) that is why you couldnot able to insert a record over that object