You need to sign in to do that
Don't have an account?
Apex trigger to update fields(lookup and date) at asset object from custom object
I have a custom lookup filed Asset__c at custom object Service_Report__c. Service_Report__c is a child object of standard case object i.e. case is parent of Service_Report__c
Now I want to write a apex trigger to update InstallDate and Site__c fields at asset when Asset__c!=null, when I create and update record at custom object Service_Report__c.
InstallDate(asset) = Date_of_Site_Visit_To__c(custom lookup field at Service_Report__c)
Site__c(custom lookup field at asset) = Site__c(custom lookup field at case)
Asset object fields: InstallDate(Standard field), Site__c(lookup)
Service_Report__c object fields: Case__c(lookup), Asset__c(lookup), Date_of_Site_Visit_To__c(date field)
Case object fields: Site__c(lookup)
Now I want to write a apex trigger to update InstallDate and Site__c fields at asset when Asset__c!=null, when I create and update record at custom object Service_Report__c.
InstallDate(asset) = Date_of_Site_Visit_To__c(custom lookup field at Service_Report__c)
Site__c(custom lookup field at asset) = Site__c(custom lookup field at case)
Asset object fields: InstallDate(Standard field), Site__c(lookup)
Service_Report__c object fields: Case__c(lookup), Asset__c(lookup), Date_of_Site_Visit_To__c(date field)
Case object fields: Site__c(lookup)
There are two ways to achieve this:
1) You can use process builder
2) trigger
If you are using trigger then your code would look sth like this:
trigger ServiceReportTrigger on Service_Report__c (after insert, after update) {
set<id> assetIds = new set<Id>();
map<id, date> mapAssetInstallDate = new map<id, date>();
map<id, string> mapAssetSite = new map<id,string>();
List<Asset__c> newAssetList = new List<Asset__c>();
for (service_report__c srv: trigger.new) {
if(srv.asset__c != null) {
assetIds.add(srv.asset__c);
mapAssetInstallDate.put(srv.asset__c, srv.Date_of_Site_Visit_To__c);
mapAssetSite.put(srv.asset__c, srv.CaseId);
}
}
List<Asset__c> assetList = [Select id, installdate__c, site__c from Asset__c where Id IN: assetIds];
for (Asset__c ast: assetList) {
ast.installdate__c = mapAssetInstallDate.get(ast.id);
ast.site__c = mapAssetSite.get(ast.id);
newAssetList.add(ast);
}
if (newAssetList.size() > 0) {
update newAssetList;
}
}
Hope this helped!
All Answers
There are two ways to achieve this:
1) You can use process builder
2) trigger
If you are using trigger then your code would look sth like this:
trigger ServiceReportTrigger on Service_Report__c (after insert, after update) {
set<id> assetIds = new set<Id>();
map<id, date> mapAssetInstallDate = new map<id, date>();
map<id, string> mapAssetSite = new map<id,string>();
List<Asset__c> newAssetList = new List<Asset__c>();
for (service_report__c srv: trigger.new) {
if(srv.asset__c != null) {
assetIds.add(srv.asset__c);
mapAssetInstallDate.put(srv.asset__c, srv.Date_of_Site_Visit_To__c);
mapAssetSite.put(srv.asset__c, srv.CaseId);
}
}
List<Asset__c> assetList = [Select id, installdate__c, site__c from Asset__c where Id IN: assetIds];
for (Asset__c ast: assetList) {
ast.installdate__c = mapAssetInstallDate.get(ast.id);
ast.site__c = mapAssetSite.get(ast.id);
newAssetList.add(ast);
}
if (newAssetList.size() > 0) {
update newAssetList;
}
}
Hope this helped!
Thanks for prompt reply, both the solution is important for me to learn apex and customization in salesforce. Thanks once agains.