I am using the bulk api to upload a CSV file to a custom object with the same fields as my CSV file. I then use an 'after insert' trigger to insert the data from the CSV custom object into the proper objects. I have a field called MemberType__c in the CSV custom object that is essentially the Product.
My question is, how do I update my Custom Membership Object with the Product Id when it matches the MemberType__c field?
The values are already int he products table.. for example "Gold," "Commercial," ect.
So basically, if MemberType__c = Products.Name update Products lookup relationship in Membership__c object.
Here is my trigger.
trigger OnUploadInsert on CSVUpload__c (after insert) {
Account[] accounts = new Account[] {};
Contact[] contacts = new Contact[] {};
Boat__c[] boats = new Boat__c[] {};
Membership__c[] memberships = new Membership__c[]{};
Order[] orders = new Order[]{};
Promotion_Code__c[] promos = new Promotion_Code__c[]{};
for (CSVUpload__c u : Trigger.new) {
accounts.add(new Account(
Name = u.FirstName__c + ' ' + u.LastName__c
));
contacts.add(new Contact(
FirstName = u.FirstName__c,
LastName = u.LastName__c,
MailingStreet = u.Address__c,
MailingCity = u.City__c,
MailingStateCode = u.State__c,
MailingPostalCode = u.Zip__c,
HomePhone = u.HomePhone__c,
OtherPhone = u.BusinessPhone__c,
MobilePhone = u.MobilePhone__c,
Email = u.EmailAddress__C,
Title = u.Title__c,
Fax = u.FaxNo__c
));
boats.add(new Boat__c(
Address__c = u.BoatAddress__c,
City__c = u.BoatCity__c,
State__c = u.BoatState__c,
Zip__c = u.BoatZip__c,
Country__c = u.BoatCountry__c
));
memberships.add(new Membership__c(
Auto_Renew__c = u.AutoReNew__c,
MemberSince__c = u.MemberSince__c,
Expiration__c = u.ExpireDate__c,
MemberSource__c = u.MemberSource__c,
MemberStatus__c = u.MemberStatus__c,
Trailer_Care__c = u.TrailerCoverage__c
));
orders.add(new Order(
Status = 'Draft',
TypeAmt__c = u.TypeAmt__c,
PoDate = u.TransDate__c,
EffectiveDate = u.EffectiveDate__c
));
promos.add(new Promotion_Code__c(
Code__c = u.PromCode__c
));
}
insert accounts;
for (Integer i = 0; i < accounts.size(); i++) {
contacts[i].AccountId = accounts[i].Id;
boats[i].Account__c = accounts[i].Id;
memberships[i].Account__c = accounts[i].Id;
orders[i].AccountId = accounts[i].Id;
}
insert contacts;
insert boats;
insert memberships;
for(Integer m = 0; m < memberships.size(); m++)
{
orders[m].Membership__c = memberships[m].Id;
}
insert orders;
for(Integer o = 0; o < orders.size(); o++)
{
promos[o].Order__c = orders[o].Id;
}
insert promos;
}
I'm sorry if this seems trivial as I am new to APEX/Salesforce/Bulk API.