You need to sign in to do that
Don't have an account?
Update Lookup Record from isUpdate Trigger
trigger Sales_Invoice_Custom_Trigger on c2g__codaInvoice__c (before insert, before update) { // if this SIN is getting inserted if( Trigger.isInsert ) { for(c2g__codaInvoice__c SIN : Trigger.new) {} } // if this SIN is getting updated if( Trigger.isUpdate ) { for(c2g__codaInvoice__c SIN : Trigger.new) //check that it has a Sales Order* if(SIN.Sales_Order__c != null) { //get the SIN Outstanding Value decimal SINov = SIN.c2g__OutstandingValue__c; //query and update the Sales Order* Sales_Order__c SalesO = [SELECT Id FROM Sales_Order__c WHERE Id = :SIN.Sales_Order__r].Id; SalesO.Outstanding_Value__c = SINov; } } }
I am having a problem with the acutal update of the Sales_Order__c record. Any advice is appreciated, still learning APEX.
Try using: update SalesO;
As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.
Thanks
Typically, in a before trigger you would not need to us a DML statement (e.g. insert, update), but because you're altering a different object (Sales_Order__c) rather than the c2g__codaInvoice__c records that fired the trigger, the DML is necessary, as William states above.
You should be careful with this trigger as it breaks a cardinal rule of Apex: Don't put DML or SOQL inside a FOR loop. You're better off gathering a list of Sales_Order__r ID's and using that list in a separate SOQL outside of the for loop.
-Chris
16| Invalid bind expression type of Sales_Order__c for column of type Id
And I've heard of this before but never know what the risks of putting a DML or SOQL inside a FOR loop are?
As you are updating a different object, so I would suggest to do that in after trigger, and using DML or SOQL in for loop will results in salesforce limits exceptions, when performing bulk insertion/updation/deletion. In your code, your are getting only the Id, that's why you are getting exception. Use the below code: Let me know, if you need any other help.
Thanks,
Neetu
Think of how the flow would be for 200 records. For every iteration you would check if statement, do the SOQL query and apply the DML (after this DML, other triggers may run, which may fire other DML and SOQL). Taking the DML and SOQL out of the loop turns potentially 200 queries (or more) and 200 DMLs into 1 query (or none at all if you look at Neetu's code above) and 1 DML. The idea around all of this is that SFDC's servers aren't constantly being slammed by unnecessary round trips.
Also in you code below you were trying to query Id field value and assign to Sales Order object which is why salesorce throws an error.
Sales_Order__c SalesO = [SELECT Id FROM Sales_Order__c WHERE Id = :SIN.Sales_Order__r].Id;
Since you are trying to udpate another and you have the Id of those records, you can simply instantiate the custom object and udpate the value.