You need to sign in to do that
Don't have an account?
Mi 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.
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.
Can you post your Trigger code, so that we can analyse it accordingly.
Regards
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;
}
--------------------------------------------------------------------------
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
I tried but it does not work.
Regards,
Mi Nguyen
is that right?.
what is exact problem you are getting?
record is not inserted in assert__c or assertHistory__C?
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
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
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
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
It's my typo mistake.
It can be compiled. but the Asset_History__c object still cannot be inserted.
Regards,
Mi Nguyen.
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.
Between these AssertHistory__C and Assert__c have any relationship field.if so can give field details of these two objects
Currently I put no relationship between these objects.
Regards,
Mi Nguyen
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
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
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