+ Start a Discussion
NasirNasir 

i want to update a fields from one object to another object

Hi

 

I am facing problem in updating a field from TMS__Time__c  to Sales_Order_Item__c

 

I had written this code,but this is not working.

trigger updatesalesorderitem on TMS__Time__c (before insert, before update)
{List<Sales_Order_Item__c> updateList = new List<Sales_Order_Item__c>();
for(TMS__Time__c co : Trigger.new){
Sales_Order_Item__c sa = [SELECT Comments__c FROM Sales_Order_Item__c WHERE Id = :co.TMS__Comments__c];

if(sa.Comments__c == null || sa.Comments__c ==''){
sa.Comments__c = co.TMS__Comments__c;

updateList.add(sa);
}
}
if (updateList.size() > 0) update updateList;
}

 As this code worked for to update field.But i am facing problem in SOQL query .Plz check and update me.

 

Thanks

 

Nasir

 

DharmeshDharmesh

try this

trigger updatesalesorderitem on TMS__Time__c (before insert, before update){
List<Sales_Order_Item__c> updateList = new List<Sales_Order_Item__c>();
Set<Id> commentIds = new Set<Id>();
for(TMS__Time__c co : Trigger.new){
commentIds.add(co.TMS__Comments__r.id);
}
for(Sales_Order_Item__c sa = [SELECT Comments__c FROM Sales_Order_Item__c WHERE Id in :commentIds]){
if(sa.Comments__c == null || sa.Comments__c ==''){
sa.Comments__c = co.TMS__Comments__c;
updateList.add(sa);
}
}
if (updateList.size() > 0) {
update updateList;
}
}