1

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.

1

You will be needing to use Sets and Maps to query Product having name as MemberType field in CSV object and update the Membership Object with the Product Id.

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[]{};
    // Start - Added for Product mapping
    Set<String> setMemberType = new Set<String>(); // Used to store MemberType from CSVUpload__c object
    Map<String,Id> mapMemberTypeProductId = new Map<String,Id>(); // Used to store map of MemberType to Product Id
    // End - Added for Product mapping
    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
        ));
        // Start - Added for Product Mapping
        setMemberType.add(u.MemberType__c );
        // End - Added for Product Mapping
    }
    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;

    // Start - Added for Product Mapping
    // Iterate over Product object and populate map with Product Name and Product Id
    for(Product2 p : [Select Id,Name From Product2 Where Name IN : setMemberType]) {
        mapMemberTypeProductId.put(p.Name,p.Id);
    }
    // Iterate over memberships and populate Product__c field from the map
    for(Integer j = 0; j < memberships.size(); j++)
    {
        memberships[j].Product__c = mapMemberTypeProductId.get(Trigger.new[j].MemberType__c);
    }

    update memberships;
    // End - Added for Product Mapping
}

I have modified the trigger you wrote. I added comments which should be self explanatory. The modified code will be within // Start and // End comments.

Hope it helps.

  • Dude, awesome solution! I had to fix a few things, which wasn't a big deal. When setting it, u. was missing.. so changed to from setMemberType.add(MemberType__c); to setMemberType.add(u.MemberType__c).. also changed memberships[m].Product__c to memberships[j].Product__c... but other than that.. it was perfect.. especially without testing it! Thanks again! – Humpy Sep 3 '15 at 17:38
  • 1
    Updated it. Sorry for that. Glad it helped you. – Vigneshwaran G Sep 3 '15 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.