I am using dataloader to import data into salesforce. As it is live synchronization, there is a problem with direct insertion of lookup field values. For this reason I am writing trigger before insertion to validate and insert.
I have a invoice__c custom object and it has accounts lookup field. For lookup field, receiving input value as account name. I need to get id of the account name from accounts object and insert into invoice__c account lookup field.
I am new to the Apex code and tried few lines of code. I am able to validate and stuck in id retrievel and insertion into lookup field.
Could someone help me out. And correct me if this approach is correct for multi trigger operation.
Trigger AccountInsert on invoice__c(before insert) {
for(invoice__c invoiceVal : Trigger.new) {
List<invoice__c> oldAcc = [select id, Name from Account where Name
= invoiceVal.Account__c];
if(oldAcc.size()>0) {
invoiceVal.name.addError('Account is existing');
}
else {
invoiceVal.name.addError('Account is not existing');
}
}
}