• Jonathan Sheehan
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 0
    Replies
I have a class that converts a JSON object (stored in a field) into an APEX List.  Once I convert it, I am having a hard time getting the data out of the APEX List.  Can someone please point me in the right direction?

My class is here:
public class JSON2Apex {

    public List<LineItems> lineItems;

    public class LineItems {
        public Double UnitPrice;
        public Double Quantity;
        public String ProductName;
    }

    
    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
    
    }

And my Trigger is here:
trigger parseJSON on COrders__c (after insert) {

          List<COrder_Item__c> itemsToAdd = new List<COrder_Item__c>();
    
            for (COrders__c c: Trigger.new) {
                     if(c.TempJSONOrderInfo__c != null) {
                        JSON2Apex JSONL = JSON2Apex.parse(c.TempJSONOrderInfo__c);
    
                        for(List < LineItems > l:  JSONL){
                        COrder_Item__c i = new COrder_Item__c(COrder__c=c.Id, Price__c=l.UnitPrice, Product__c=l.ProductName, Quantity__c=l.Quantity);
                        itemsToAdd.add(i);
                        } 
                                       
                    }
                   
            }
    database.insert (itemsToAdd, false);
    
}

Sample JSON Data is:
{"lineItems":[{"UnitPrice":139.99,"Quantity":5.0,"ProductName":"Leader"},{"UnitPrice":139,"Quantity":1.0,"ProductName":"Student"}]}

 

I am having a hard time figuring out how to organize some contacts. A few facts:
- We host events for college students.
- Over the life of a person, they may 1) attend in a group where someone else purchases their ticket and provides us their information 2) purchase a ticket for them self 3) purchase a ticket for them self and someone else
- I am trying to figure out how to keep track of the relationships between people. IE, it is important to know if someone purchased 200 tickets (they would be a large customer), but I want to be able to know if someone attended in the past with a group, and now is attending by them self.

Any ideas/direction to point me in?
My test code is only covering 36% of my Apex trigger and I'm stuck on how to get it up...Can someone please point me in the right direction?

TRIGGER:
trigger TempFieldsToCampaignAndEvent on Contact (after insert) {

        List<CampaignMember> membersToAdd = new List<CampaignMember>();
        List<Event> NewEvents = new List<Event>();
    
            for (Contact c: Trigger.new) {
                     if(c.TempCampaign__c != null) {
                        CampaignMember cm = new CampaignMember(CampaignId=c.TempCampaign__c, ContactId=c.Id, Status=c.TempCampaignStatus__c);
                        membersToAdd.add(cm);
                    }
                    system.debug(c.TempEventDescription__c);
                    if(c.TempEventDescription__c != null){
                        Event e         = new Event();
                        e.StartDateTime       = c.TempEventTime__c;
                        e.ActivityDateTime    = c.TempEventTime__c;
                        e.Subject           = c.TempEventSubject__c;
                        e.WhatId              = c.TempCampaign__c;
                        e.Description       = c.TempEventDescription__c;
                        e.WhoId             = c.Id;
                        e.OwnerId           = '0051a000000hmuN';
                        e.DurationInMinutes           = 0;
                        NewEvents.add(e);
                        }           
                    }
    database.insert (membersToAdd, false);
    insert NewEvents;    
    }

TEST CLASS
@IsTest
public class AAAllTest {

    public testMethod static void contactInsertion() {

        // Consider setting some address fields as required by copyAddressFields
        Contact c = new Contact(lastname='testing', firstname='apex');
        insert c;

        //Make some assertions based on the results of the copyAddressFields call
    }

    public testMethod static void contactInsertionFails() {

        // Set some fields that will cause an Exception in copyAddressFields
        Contact c = new Contact(lastname='testing', firstname='apex');
        insert c;

        //Make some assertions that errors were added to the Contact
    }

    public testMethod static void bulkifedInsertion() {

        Contact[] contactsToCreate = new Contact[]{};

        for(Integer x=0; x<200;x++){
            Contact ct = new Contact(lastname='testing',firstname='apex');
            contactsToCreate.add(ct);
        }
         Test.startTest();
            insert contactsToCreate;
            Test.stopTest();
    }

    public testMethod static void CampaignInsertion() {

        // Consider setting some address fields as required by copyAddressFields
        Campaign m = new Campaign(Name='Test Campaign');
        insert m;

        //Make some assertions based on the results of the copyAddressFields call
    }
 public testMethod static void EventInsertion() {

        // Consider setting some address fields as required by copyAddressFields
        date ctd = Date.today(); // will set the todays date
        Event e = new Event(Subject='Test', StartDateTime=ctd, EndDateTime=ctd, WhoId='0031700000AU6P9', DurationInMinutes=0);
        insert e;
        }

     public testMethod static void CampaignMembers() {

            // Consider setting some address fields as required by copyAddressFields
            date ctd = Date.today(); // will set the todays date
            CampaignMember cm = new CampaignMember(CampaignId='70117000001ARcU', ContactId='0031700000AU6P4', Status='Responded');
            database.insert (cm, false);



        //Make some assertions based on the results of the copyAddressFields call
    }


       
    
}