function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
davidjbbdavidjbb 

Trigger Update

Hello,

 

I have the following trigger to update the Visit object based off of the Workorder object. There is a Visit lookup on the Workorder object. Thus, ..the code checks if the Id from Visit and Workorder matches - the trigger will update. 

 

However, the trigger doesn't seem to fire. 

 

The changes on the Salesforce side is sent from a mobile that updates a field in the Workorder object. This has been tested. The field gets updated. (Date/time) I'm not sure what i'm doing wrong or why the trigger isn't being fired. 

 

trigger WorkorderToVisit on jbbfc2__Workorder__c (before update, before insert) {
set <Id> setWoId = new set <Id> ();
for(jbbfc2__Workorder__c wo: trigger.new){
setWoId.add(wo.Visit__c);
}
map <Id, Visit__c> mapVisit = new map <Id, Visit__c> ();
for(Visit__c v : [Select Id from Visit__c where Id in :setWoId]){
mapVisit.put(v.id, v);
}

list <Visit__c> UpdVisit = new list <Visit__c> ();
for(jbbfc2__Workorder__c wo: trigger.new){
if(mapVisit.ContainsKey(wo.Visit__c) == true){// if WO Id is true then update the visit object

mapVisit.get(wo.Visit__c).Customer_Notes__c = 'trigger works';
mapVisit.get(wo.Visit__c).Field_Notes__c = 'trigger works'; 
}
else{
mapVisit.get(wo.Visit__c).Customer_Notes__c = 'trigger works';
mapVisit.get(wo.Visit__c).Field_Notes__c = 'trigger works'; 
}
}
if(UpdVisit.isEmpty()==false){
update UpdVisit;
}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Srikant JoshiSrikant Joshi

Hi David,

 

From the code Visit__c won't get updated since 'UpdVisit ' is not being assigned with any items/records.Pls try the code.

 

trigger WorkorderToVisit on jbbfc2__Workorder__c (before update, before insert) {
set <Id> setWoId = new set <Id> ();
for(jbbfc2__Workorder__c wo: trigger.new){
setWoId.add(wo.Visit__c);
}
map <Id, Visit__c> mapVisit = new map <Id, Visit__c> ();
for(Visit__c v : [Select Id from Visit__c where Id in :setWoId]){
mapVisit.put(v.id, v);
}

list <Visit__c> UpdVisit = new list <Visit__c> ();
for(jbbfc2__Workorder__c wo: trigger.new){
if(mapVisit.ContainsKey(wo.Visit__c) == true){// if WO Id is true then update the visit object

mapVisit.get(wo.Visit__c).Customer_Notes__c = 'trigger works';
mapVisit.get(wo.Visit__c).Field_Notes__c = 'trigger works';
UpdVisit.add(mapVisit.get(wo.Visit__c));//Updating the List
}
else{
mapVisit.get(wo.Visit__c).Customer_Notes__c = 'trigger works';
mapVisit.get(wo.Visit__c).Field_Notes__c = 'trigger works';
UpdVisit.add(mapVisit.get(wo.Visit__c));
}
}
if(UpdVisit.isEmpty()==false){
update UpdVisit;
}
}

 

Joshisrik@gmail.com

http://in.linkedin.com/pub/srikant-joshi/49/807/336