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
The WalrusThe Walrus 

Adding lookup information to a Select statement on a before insert trigger

Hello,

 

I have a Master-Detail relationship between an Invoice custom object and a set of Invoice Balance records.  I've written

a select statement that allows me to look up the latest balance record - but uses a literal to identify the master record - and would can run it from a trigger when before a new entry for this master is inserted.  Here is the class code:

public class InvoicePayment {
public static void calculatePayment(InvoiceBalance__c[] invoicepayments) {
invoice__c[] a = [SELECT Name, (SELECT Invoice__c, InvoiceBalanceDate__c, Invoice_Balance_Amount__c 
FROM Invoice_Balances__r) FROM Invoice__c];
System.debug('a: '+a);

Invoicebalance__c[] b = [SELECT InvoiceBalanceDate__c, Invoice__r.Name 
FROM InvoiceBalance__c WHERE Invoice__r.Name LIKE 'xxx'
ORDER BY InvoiceBalanceDate__c DESC NULLS LAST LIMIT 1];

System.debug('b: '+b);
for (InvoiceBalance__c i :invoicepayments) { 
i.Paid_Since_Last_Report__c = i.Invoice_Balance_Amount__c * 0.9;
}
}
}

 The class runs fine, but I can't figure out how to substitute 'xxx' with the invoice value from the form.  Is there a pointer to

the field value of the record you are trying to insert?  Or do i i have to access visualforce in some way?   Any assistance is appreciated.

Navatar_DbSupNavatar_DbSup

Hi,

 

You can use something like this:

 

trigger Touch_Imp on User (after insert)

{

 

    Set<String> usercontactid = new Set<String>();

    for (User u : Trigger.new) {

        usercontactid.add(u.contactid);

   }

  

    if([SELECT COUNT() FROM implementation__c WHERE implementation__c.implementation_contact__c IN :usercontactid]>0)                

         UpdateNonSetupObjects.updateContact(usercontactid); 

}

 

public class UpdateNonSetupObjects

{

 

@future

public static void updateContact(Set<String> usercontactid ?)

{

    Implementation__c[] updateimp = [SELECT id from Implementation__c WHERE implementation__c.implementation_contact__c IN :usercontactid];

    Database.update(updateimp);

}

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.