• CodeHeartsSFDC
  • NEWBIE
  • 20 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 12
    Replies
Hi,

I have the below class for updating contact and account when a custom object record is created/updated. My test class coverage is not going past 65%. Can someone help me with this. This is an urgent request, appreciate some quick help. The lines highlighted in bold are not covered

Class:

public without sharing class SubscriptionService {
    public static void updateContact(List<OrderApi__Subscription__c> records){
        Set<Id> contactIds = new Set<Id>();
        List<Contact> contactList = new List<Contact>();
        Map<Id,Contact> conIdVSContactMap=new Map<Id,Contact>();
        List<Contact> contactList1 = new List<Contact>();
        Integer count = 0;

        for(OrderApi__Subscription__c sub: records) {
        contactIds.add(sub.OrderApi__Contact__c);
        }
        
        if(contactIds.size()>0){
        contactList = [Select id, Chapter_Member_Status__c, Chapter_Member_Activated_Date__c, Member_Paid_Through_Date__c, Member_Term_End_Date__c, Member_Term_Start_Date__c from Contact where id IN : contactIds LIMIT 10000];
        }

        if(contactList.size()>0){
        for(Contact con:contactList){
        conIdVSContactMap.put(con.Id,con);
        }
        }

        if(conIdVSContactMap.size()>0){
        Subscription__c mhc = Subscription__c.getInstance();
        string ItemCollID = mhc.Collegiate_Item_Id__c;
        for(OrderApi__Subscription__c sub:records){
        if(conIdVSContactMap.containsKey(sub.OrderApi__Contact__c)){
        if(sub.OrderApi__Item__c != NULL && sub.OrderApi__Item__c == ItemCollID){
        Contact newCon=conIdVSContactMap.get(sub.OrderApi__Contact__c);
        newCon.Chapter_Member_Status__c=sub.OrderApi__Status__c;
        newCon.Chapter_Member_Activated_Date__c=string.valueof(sub.OrderApi__Activated_Date__c);
        newCon.Member_Paid_Through_Date__c=string.valueof(sub.OrderApi__Paid_Through_Date__c);
        newCon.Member_Term_End_Date__c=string.valueof(sub.OrderApi__Current_Term_End_Date__c);
        newCon.Member_Term_Start_Date__c=string.valueof(sub.OrderApi__Current_Term_Start_Date__c);
        contactList1.add(newCon);

        }
        }
        }
        }

        if(contactList1.size()>0) {
        update contactList1;
        }   
        
     }
     
     public static void updateAccount(List<OrderApi__Subscription__c> records){
     
     map<id,Account> updateMap =new map<id,Account>();  
     set<ID> subnameset=new set<ID>();
     Map<ID, Account> parentAcc;
     List<Account> accountList = new List<Account>();
     List<Account> chapAccountList = new List<Account>();
    
     Subscription__c mhc = Subscription__c.getInstance();
     string ItemCollID = mhc.Collegiate_Item_Id__c; 
     
     if(Trigger.isInsert||Trigger.isUpdate){ 
        for(OrderApi__Subscription__c sub:records){ 
            if(sub.OrderApi__Account__c != null && sub.OrderApi__Item__c != NULL && sub.OrderApi__Item__c == ItemCollID){
                subnameset.add(sub.OrderApi__Account__c); 
            }    
        } 
     } 
    
     List<AggregateResult> AggregateResultList = [select OrderApi__Account__c,OrderApi__Status__c, OrderApi__Subscription_Plan__c, OrderApi__Activated_Date__c, OrderApi__Paid_Through_Date__c, OrderApi__Term_Start_Date__c, OrderApi__Contact__c, max(OrderApi__Term_End_Date__c) maxDate from OrderApi__Subscription__c where OrderApi__Account__c in:subnameset group by OrderApi__Account__c, OrderApi__Status__c, OrderApi__Subscription_Plan__c, OrderApi__Activated_Date__c, OrderApi__Paid_Through_Date__c, OrderApi__Term_Start_Date__c, OrderApi__Contact__c]; 
     if(AggregateResultList != null && AggregateResultList.size() > 0){ 
        for(AggregateResult aggr:AggregateResultList){ 
        if(aggr.get('OrderApi__Status__c') == 'Active') {          
            Account acc=new Account(); 
            acc.Id=(id)aggr.get('OrderApi__Account__c');
            acc.Chapter_Term_End_Date__c=string.valueOf(aggr.get('maxDate')); 
            acc.Status__c=string.valueOf(aggr.get('OrderApi__Status__c'));
            acc.Chapter_Activated_Date__c=string.valueOf(aggr.get('OrderApi__Activated_Date__c'));
            acc.Chapter_Paid_Through_Date__c=string.valueOf(aggr.get('OrderApi__Paid_Through_Date__c'));
            acc.Chapter_Term_Start_Date__c=string.valueOf(aggr.get('OrderApi__Term_Start_Date__c'));
            updateMap.put(acc.Id, acc); 

        } 
        
        } 
    
     }   
    
     update updateMap.values(); 
    
     parentAcc = new Map<Id, Account>([SELECT id, Member_Count__c from Account WHERE Id IN :subnameset]);
    
     Integer soql = [Select count() FROM OrderApi__Subscription__c WHERE OrderApi__Account__r.Id =:subnameset AND OrderApi__Is_Active__c != FALSE];
    
     for(OrderApi__Subscription__c subs:records){
     if(subs.OrderApi__Item__c != NULL && subs.OrderApi__Item__c == ItemCollID) {
        Account newAcc = parentAcc.get(subs.OrderApi__Account__c);
        newAcc.Member_Count__c = soql;
        accountList.add(newAcc);
       }   
     }
    
     if(accountList.size()>0) {
     update accountList;
     } 
     }
     
     public static void updateChapter(List<OrderApi__Subscription__c> records){
     
     Set<Id> accountIds = new Set<Id>();
     List<Account> accountList = new List<Account>();
     List<OrderApi__Business_Group__c> bg;
     Map<Id,Account> accIdVSAccountMap=new Map<Id,Account>();
     List<Account> accountList1 = new List<Account>();

     for(OrderApi__Subscription__c sub: records) {
     accountIds.add(sub.OrderApi__Account__c);
     }

     if(accountIds.size()>0){
     accountList = [Select id, Chapter__c from Account where id IN : accountIds LIMIT 10000];
     }

     if(accountList.size()>0){
     for(Account acc:accountList){
     accIdVSAccountMap.put(acc.Id,acc);
     }
     }
    
     bg=[Select id, Collegiate_Chapter_Number__c, Collegiate_Chapter_Number_Text__c from OrderApi__Business_Group__c where Name = 'NAfME Business Group'];
     String chapNum;
     if(bg!=null && bg.size() > 0){
     chapNum = bg[0].Collegiate_Chapter_Number_Text__c;
     }
    
     if(accIdVSAccountMap.size()>0){
     Subscription__c mhc = Subscription__c.getInstance();
     string ItemCollID = mhc.Collegiate_Item_Id__c;
    
     for(OrderApi__Subscription__c sub:records){
     if(accIdVSAccountMap.containsKey(sub.OrderApi__Account__c)){
       if(sub.OrderApi__Item__c != NULL && sub.OrderApi__Item__c == ItemCollID){
       Account newAcc=accIdVSAccountMap.get(sub.OrderApi__Account__c);
       if(newAcc.Chapter__c == NULL){
       newAcc.Chapter__c='C-'+chapNum;
       accountList1.add(newAcc);
       
       OrderApi__Business_Group__c busGroup=new  OrderApi__Business_Group__c();
       busGroup.Collegiate_Chapter_Number__c=Integer.valueOf(chapNum) + 1;
       busGroup.id=bg[0].id;
       update busGroup;

       }
       }
     }
     }
     } 
     
     if(accountList1.size()>0) {
     update accountList1;
     }   
     }
}

Test class:
@isTest
public class Test_SubscriptionService {
  public static Boolean isInstalled = false;
        
        private static void install() {
            if (!isInstalled) {
                Framework.Installer.install();
                PagesApi.PackageScripts.install();
                OrderApi.PackageScripts.install();
                EventApi.PackageScripts.install();
                
                // Create a routing rule for Subscription
                Framework.RoutingRule[] rules = new List<Framework.RoutingRule>();
                Framework.RoutingRule subscriptionRule = new Framework.RoutingRule();
                subscriptionRule.apexClass = NAME_Subscription.class.getName();
                subscriptionRule.namespace = '';
                subscriptionRule.isEnabled = true;
                subscriptionRule.sObjectName = 'OrderApi__Subscription__c';
                subscriptionRule.executionOrder = 80;
                rules.add(subscriptionRule);
                Framework.Config.push(rules);               
                
                isInstalled = true;
               
            }
        
}

static testmethod void subTest(){

    install();
    
    Account acc=new Account();
    acc.Name = 'Test My Account Test';
    acc.Chapter__c = '';
    insert acc;
    
    Contact con=new Contact();
    con.FirstName='Test';
    con.LastName='Sub Contact 1';
    con.email='testnafme@mailinator.com';
    con.accountid=acc.id;
    insert con;
    
    Contact con1=new Contact();
    con1.FirstName='Test';
    con1.LastName='Sub Contact 2';
    con1.email='testnafme1@mailinator.com';
    con1.accountid=acc.id;
    insert con1;
    
    User usr = new User();
    usr.ProfileId = [select Name, id From Profile where UserLicense.Name like '%Customer Community%' limit 1].Id;
    usr.Contactid = con.id;
    usr.Username = con.Email;
    usr.LastName=con.LastName;
    usr.Email=con.Email;
    usr.Alias='MNU'+String.ValueOf(con.id).right(5);
    usr.CommunityNickname = con.Email;
    usr.EmailEncodingKey='UTF-8';
    usr.languagelocalekey='en_US';
    usr.timezonesidkey='America/Los_Angeles';
    usr.localesidkey='en_US';
    usr.IsActive=true;
    Insert usr;
    
    OrderApi__Business_Group__c bg = new OrderApi__Business_Group__c();
    bg.Name = 'Test Business Group';
    bg.Collegiate_Chapter_Number__c = Integer.valueOf('1111');
    insert bg;
    
    OrderApi__Subscription_Plan__c subPlan=new OrderApi__Subscription_Plan__c();
    subPlan.Name = 'Test Subscription Plan';
    subPlan.OrderAPi__Type__c = 'Termed';
    subPlan.OrderAPi__Initial_Term_Months__c = 12;
    subPlan.OrderAPi__Renewal_Term_Months__c = 12;
    subPlan.OrderAPi__Bill_Cycle__c = 'AUTO';
    subPlan.OrderAPi__Auto_Renew_Option__c = 'Enabled';
    subPlan.OrderAPi__Is_Active__c = true;
    subPlan.OrderApi__Business_Group__c = bg.id;
    insert subPlan;
    
    OrderApi__Item_Class__c ic = new OrderApi__Item_Class__c();
    ic.OrderApi__Business_Group__c = bg.Id;
    ic.OrderApi__Is_Active__c = true;
    ic.OrderApi__Is_Subscription__c = true;
    ic.OrderApi__Enable_Assignments__c = false;
    ic.OrderApi__Assignment_Sobject__c = 'OrderApi__Assignment__c';
    insert ic;
    
    OrderApi__Item__c i=new OrderApi__Item__c();
    i.OrderApi__Item_Class__c = ic.Id;
    i.Name = 'Test Collegiate';
    insert i;
   
    Account a = [Select id from Account where Name = 'Test My Account Test'];
    Contact c = [Select id from Contact where Name = 'Test Sub Contact 1'];
    OrderApi__Subscription_Plan__c sp = [Select id from OrderApi__Subscription_Plan__c where Name = 'Test Subscription Plan'];
    OrderApi__Item__c it = [Select id from OrderApi__Item__c where Name = 'Test Collegiate'];
    
    Test.startTest();
    OrderApi__Subscription__c sub=new OrderApi__Subscription__c();
    sub.OrderApi__Account__c = acc.id;
    sub.OrderApi__Contact__c = con.id;
    sub.OrderApi__Is_Active__c = True;
    sub.OrderApi__Status__c = 'Active';
    sub.OrderApi__Subscription_Plan__c = subPlan.id;
    sub.OrderApi__Item__c = i.id;
    sub.OrderApi__Paid_Through_Date__c = date.parse('06/30/2020');
    sub.OrderApi__Activated_Date__c = date.parse('08/01/2019');
    insert sub;
    
    con.Chapter_Member_Status__c = 'Active';
    con.Member_Term_Start_Date__c = string.valueOf('sub.OrderApi__Term_Start_Date__c');
    con.Member_Term_End_Date__c = string.valueOf('sub.OrderApi__Term_End_Date__c');
    con.Member_Paid_Through_Date__c = string.valueOf('sub.OrderApi__Paid_Through_Date__c');
    con.Chapter_Member_Activated_Date__c = string.valueOf('sub.OrderApi__Activated_Date__c');
    update con;
    
    if(acc.Chapter__c == NULL){
    acc.Chapter__c = bg.Collegiate_Chapter_Number_Text__c;
    }
    acc.Chapter_Term_Start_Date__c = string.valueOf('sub.OrderApi__Term_Start_Date__c');
    acc.Chapter_Term_End_Date__c = string.valueOf('sub.OrderApi__Term_End_Date__c');
    acc.Chapter_Paid_Through_Date__c = string.valueOf('sub.OrderApi__Paid_Through_Date__c');
    acc.Chapter_Activated_Date__c = string.valueOf('sub.OrderApi__Activated_Date__c');
    acc.Status__c = 'Active';
    update acc;
    
    OrderApi__Subscription__c sub1=new OrderApi__Subscription__c();
    sub1.OrderApi__Account__c = acc.id;
    sub1.OrderApi__Contact__c = con1.id;
    sub1.OrderApi__Is_Active__c = True;
    sub1.OrderApi__Status__c = 'Active';
    sub1.OrderApi__Subscription_Plan__c = sp.id;
    sub1.OrderApi__Item__c = it.id;
    sub1.OrderApi__Paid_Through_Date__c = date.parse('06/30/2020');
    sub1.OrderApi__Activated_Date__c = date.parse('09/01/2019');
    insert sub1;
    
    con1.Chapter_Member_Status__c = 'Active';
    con1.Member_Term_Start_Date__c = string.valueOf('sub.OrderApi__Term_Start_Date__c');
    con1.Member_Term_End_Date__c = string.valueOf('sub.OrderApi__Term_End_Date__c');
    con1.Member_Paid_Through_Date__c = string.valueOf('sub.OrderApi__Paid_Through_Date__c');
    con1.Chapter_Member_Activated_Date__c = string.valueOf('sub.OrderApi__Activated_Date__c');
    update con;
    
    acc.Chapter_Term_Start_Date__c = string.valueOf('sub1.OrderApi__Term_Start_Date__c');
    acc.Chapter_Term_End_Date__c = string.valueOf('sub1.OrderApi__Term_End_Date__c');
    acc.Chapter_Paid_Through_Date__c = string.valueOf('sub1.OrderApi__Paid_Through_Date__c');
    acc.Chapter_Activated_Date__c = string.valueOf('sub1.OrderApi__Activated_Date__c');
    acc.Status__c = 'Active';
    update acc;
    
    sub.OrderApi__Status__c = 'Cancelled';
    sub.OrderApi__Paid_Through_Date__c = date.parse('06/30/2020');
    sub.OrderApi__Activated_Date__c = date.parse('08/01/2019');
    update sub;
    
    con.Chapter_Member_Status__c = 'Cancelled';
    con.Member_Term_Start_Date__c = string.valueOf('sub.OrderApi__Term_Start_Date__c');
    con.Member_Term_End_Date__c = string.valueOf('sub.OrderApi__Term_End_Date__c');
    con.Member_Paid_Through_Date__c = string.valueOf('sub.OrderApi__Paid_Through_Date__c');
    con.Chapter_Member_Activated_Date__c = string.valueOf('sub.OrderApi__Activated_Date__c');
    update con;
    
    acc.Chapter_Term_Start_Date__c = string.valueOf('sub.OrderApi__Term_Start_Date__c');
    acc.Chapter_Term_End_Date__c = string.valueOf('sub.OrderApi__Term_End_Date__c');
    acc.Chapter_Paid_Through_Date__c = string.valueOf('sub.OrderApi__Paid_Through_Date__c');
    acc.Chapter_Activated_Date__c = string.valueOf('sub.OrderApi__Activated_Date__c');
    acc.Status__c = 'Cancelled';
    update acc;
     
    sub1.OrderApi__Status__c = 'Cancelled';
    sub1.OrderApi__Paid_Through_Date__c = date.parse('08/30/2020');
    sub1.OrderApi__Activated_Date__c = date.parse('09/01/2019');
    update sub;
    
    con1.Chapter_Member_Status__c = 'Cancelled';
    con1.Member_Term_Start_Date__c = string.valueOf('sub1.OrderApi__Term_Start_Date__c');
    con1.Member_Term_End_Date__c = string.valueOf('sub1.OrderApi__Term_End_Date__c');
    con1.Member_Paid_Through_Date__c = string.valueOf('sub1.OrderApi__Paid_Through_Date__c');
    con1.Chapter_Member_Activated_Date__c = string.valueOf('sub1.OrderApi__Activated_Date__c');
    update con1;
    
    acc.Chapter_Term_Start_Date__c = string.valueOf('sub1.OrderApi__Term_Start_Date__c');
    acc.Chapter_Term_End_Date__c = string.valueOf('sub1.OrderApi__Term_End_Date__c');
    acc.Chapter_Paid_Through_Date__c = string.valueOf('sub1.OrderApi__Paid_Through_Date__c');
    acc.Chapter_Activated_Date__c = string.valueOf('sub1.OrderApi__Activated_Date__c');
    acc.Status__c = 'Cancelled';
    update acc;
    
    SubscriptionService.forTestMethod();
    
    Test.stopTest();
    
    }
    
}
Hello,

Can anyone help me with the requirement below? Thanks in adv

Requirement: I have 2 objects "Account" and custom object "Order__Api__Subscription__c". The custom object has a OrderApi__Term_start_date__c field. When the custom object record is inserted/updated, I want to update certain information on Account from custom object record having the latest/most recent OrderApi_Term_start_date__c field value.

I also have another requirement to populate the count of active records from the custom object on account which is working fine. The first part of the requirement is not working.

I wrote the below code. If I use Trigger.new in the for loop for(OrderApi__Subscription__c sub: Trigger.new) , it is updating the information from whichever record was updated recently. If I use the below code, it does not update anything.
  1. trigger updateAccount on OrderApi__Subscription__c (after insert, after update) {
  2.  
  3.     Map<ID, Account> parentAcc;
  4.     List<Id> listIds = new List<Id>();
  5.     List<Account> accountList = new List<Account>();
  6.     List<Account> accountList1 = new List<Account>();
  7.         
  8.     for (OrderApi__Subscription__c childObj : Trigger.new) {
  9.     listIds.add(childObj.OrderApi__Account__c);
  10.     }
  11.     
  12.     Integer soql = [Select count() FROM OrderApi__Subscription__c WHERE OrderApi__Account__r.Id =:listIds AND OrderApi__Is_Active__c != FALSE];
  13.     
  14.     parentAcc = new Map<Id, Account>([SELECT id, Collegiate_Current_Term_Start_Date__c, Member_Count__c, Collegiate_Paid_Through_Date__c, Collegiate_Current_Term_End_Date__c, Collegiate_Chapter_Status__c, Collegiate_Chapter_Activated_Date__c  FROM Account WHERE ID IN :listIds]);
  15.     
  16.     List<OrderApi__Subscription__c> subscription = [SELECT ID, OrderApi__Term_End_Date__c FROM OrderApi__Subscription__c WHERE id IN: listIds AND OrderApi__Term_End_Date__c != NULL Order By OrderApi__Term_End_Date__c desc LIMIT 1];
  17.     
  18.     Subscription__c mhc = Subscription__c.getInstance();
  19.     string ItemCollID = mhc.Collegiate_Item_Id__c;
  20.     
  21.     for(OrderApi__Subscription__c sub:subscription){
  22.     
  23.     if(sub.OrderApi__Item__c != NULL && sub.OrderApi__Item__c == ItemCollID) {
  24.  
  25.             Account newAcc = parentAcc.get(sub.OrderApi__Account__c);
  26.  
  27.             newAcc.Collegiate_Chapter_Status__c=sub.OrderApi__Status__c;
  28.  
  29.             newAcc.Collegiate_Chapter_Activated_Date__c=sub.OrderApi__Activated_Date__c;
  30.  
  31.             newAcc.Collegiate_Paid_Through_Date__c =sub.OrderApi__Paid_Through_Date__c;
  32.  
  33.             newAcc.Collegiate_Current_Term_End_Date__c=sub.OrderApi__Current_Term_End_Date__c;
  34.  
  35.             newAcc.Collegiate_Current_Term_Start_Date__c=sub.OrderApi__Current_Term_Start_Date__c;
  36.  
  37.             accountList.add(newAcc);
  38.  
  39.         }
  40.         
  41.     }
  42.     
  43.     for(OrderApi__Subscription__c subs:Trigger.new){
  44.     
  45.         Account newAcc1 = parentAcc.get(subs.OrderApi__Account__c);
  46.         
  47.         newAcc1.Member_Count__c = soql;
  48.         
  49.         accountList1.add(newAcc1);
  50.         
  51.     }
  52.     
  53.     if(accountList.size()>0) {
  54.  
  55.     update accountList;
  56.  
  57.     }
  58.        
  59.     if(accountList1.size()>0) {
  60.  
  61.     update accountList1;
  62.  
  63.     } 
  64.         
  65. }

 
Requirement: I have 2 objects "Contact" and a custom object "OrderApi__Subscription__c". When the custom object record is inserted/updated, I want to update the certain information present on custom object record on Contact object when a lookup field on custom object = 'ABC'

Below is the trigger I wrote which is not giving me any error, but it is not updating the fields on contact

trigger updateContact on OrderApi__Subscription__c (after insert, after update) {

    Set<String> contactIds = new Set<String>();
    List<Contact> conupdateList = new List<Contact>();
    Integer count = 0;
    
    for(OrderApi__Subscription__c sub: Trigger.new) {
    contactIds.add(sub.OrderApi__Contact__c);
    }
    
    List<Contact> contactList = [Select id, Chapter_Member_Status__c, Chapter_Member_Activated_Date__c, Member_Paid_Through_Date__c, Member_Term_End_Date__c, Member_Term_Start_Date__c from Contact where id IN : contactIds];
    
    List<OrderApi__Subscription__c> subList = [Select id, OrderApi__Status__c , OrderApi__Item__c , OrderApi__Item__r.Name, OrderApi__Activated_Date__c, OrderApi__Paid_Through_Date__c, OrderApi__Current_Term_Start_Date__c, OrderApi__Current_Term_End_Date__c from OrderApi__Subscription__c where id IN: contactIds];
    
    List<Contact> contactList2 = new List<Contact>();
    for(OrderApi__Subscription__c sub: subList) {
    if(sub.OrderApi__Item__r.Name == 'ABC') {
    count++;
    }
    }
    
    for(Contact con:contactList) {
    OrderApi__Subscription__c membership = new OrderApi__Subscription__c();
    con.Chapter_Member_Status__c = membership.OrderApi__Status__c;
    String activatedDate = string.valueof(membership.OrderApi__Activated_Date__c);
    con.Chapter_Member_Activated_Date__c = activatedDate;
    String paidThroughDate = string.valueof(membership.OrderApi__Paid_Through_Date__c);
    con.Member_Paid_Through_Date__c = paidThroughDate;
    String termEndDate = string.valueof(membership.OrderApi__Current_Term_End_Date__c);
    con.Member_Term_End_Date__c = termEndDate;
    String termStartDate = string.valueof(membership.OrderApi__Current_Term_Start_Date__c);
    con.Member_Term_Start_Date__c = termStartDate;
    contactList2.add(con);
    }
    
    if(count > 0) {
    update contactList2;
    }   
}

 

Hello,

Can anyone help me build a trigger for the below requirement please? I'm new to using delete triggers and triggers in general. Thank you!

Requirement: I have 2 objects "Order__c" and "Detail__c". Order__c is the parent and Detail__c (the children) has a lookup to order. There is a field on Detail__c called "Class__c" which is a lookup to another Item object. The Class__c field can hold two types of values only. One is "Regular" and other is "Linear".

Now when a Detail__c record is deleted, if any of the remaining detail records contain Class__c = Linear, then nothing should happen. If the Detail__c records related to Order__c contains only Class__c = Regular, then the Detail__c record (Class__c = Regular) and the correspionding Order__c record should be deleted

Below is the trigger I tried, but it is giving a null-pointer exception. I do not know if this trigger will perform the requirement stated above even if the error is corrected. Quick help will be highly appreciated. Many thanks!
 

trigger deleteOrder on Detail__c (after delete) {

    List<Order__c> orderDelete = new List<Order__c>();
    Set<Id> orderId = new Set<Id>();  
    Integer count = 0;
    
    for(Detail__c d: trigger.old) {
        orderId.add(d.Order__c);
    }
    
    Map<Id,Order__c> orderMap = new Map<Id,Order__c>([select Id from Order__c where Id In :orderId]);

    for(Detail__c d :trigger.old) {
        if(d.Class__r.Name.contains('Linear')){
        count++;
        Order__c o = orderMap.get(d.Order__c);
        orderDelete.add(o);
        }
    }
    
    if(count == 0) {
       delete orderDelete;
    }
}

 

Hi Guys,

I'm trying to add contacts to a public group when a flag on contact 'Add to group' is set to True but receiving Attempt to de-ref null object error on the line highlighted. Any idea what is wrong? Thanks in advance. Pasting the trigger & helper class below.

Trigger:
  1. trigger addToGroup on Contact (after insert, after update) {
  2.  
  3.     List<GroupMember> groupList = new List<GroupMember>();
  4.     
  5.     
  6.     Set<String> contactEmails = new Set<String>();
  7.     
  8.     for(Contact con : Trigger.New) {
  9.     
  10.         contactEmails.add(con.email);
  11.     }
  12.      
  13.      Map<String,User> emailUserMap = new Map<String,User>();
  14.  
  15.      for(User u:[select id, email from User where email in : contactEmails]) {
  16.         emailUserMap.put(u.email, u);
  17.      }
  18.      
  19.      List<Id> userIdList = new List<Id>();
  20.      for(Contact con : Trigger.New) {
  21.         if(con.Add_to_Group__c == True) { 
  22.             userIdList.add(emailUserMap.get(con.email).id);  
  23.         }
  24.      }  
  25.      
  26.      Group grp = [select id from Group where Name = 'Account Group'];
  27.      if(null != grp) {
  28.  
  29.         ContactTriggerHelper.addUsersToGroup(grp.id,userIdList);
  30.     }
  31.  
  32. }
Helper class:
  1. public class ContactTriggerHelper {
  2.  
  3.     @future
  4.  
  5.     public static void addUsersToGroup(String groupId, List<Id> userIds) {
  6.     List<GroupMember> gmList = new List<GroupMember>();
  7.         for(ID userId: userIds){
  8.  
  9.             GroupMember gm = new GroupMember();
  10.             gm.GroupId = groupId;
  11.             gm.UserOrGroupId = userId;
  12.             gmList.add(gm);
  13.         }
  14.        
  15.         if(gmList.size() > 0) {
  16.             insert gmList;
  17.         }
  18.     }
  19. }
Hi Guys,

The functionality i'm trying to perform here is to update the parent object status to complete when all related child object records have a status complete. But i'm receiving the error "Error: Compile Error: Variable does not exist: id at line 11"

I'm pasting the code below. Can you please help me out here. Thanks!!

trigger updateParentStatus on Child_Object__c (after insert, after update) {

    Set<Id> parentIds = new Set<Id>();
    Integer count = 0;

        for(Child_Object__c wd: Trigger.new) {
            parentIds.add(wd.Parent_Object__c);
        }
    
    List<Parent_Object__c> parentList = [Select id, Status__c from Parent_Object__c where id =: parentIds];
    List<Child_Object__c> childList = [Select id, Status__c from Parent_Object__c Where parentIds =: parentList.id];
    
    for(Child_Object__c wd: Trigger.new) {
        if(wd.Status__c != 'Completed') {
            count++;
        }
    }
    
    for(Parent_Object__c wo: parentList) {
        wo.Status__c = 'Completed';    
    }
    
    if(count==0) {
        update parentList;
    }
}
 
Hello,

Can anyone help me with the requirement below? Thanks in adv

Requirement: I have 2 objects "Account" and custom object "Order__Api__Subscription__c". The custom object has a OrderApi__Term_start_date__c field. When the custom object record is inserted/updated, I want to update certain information on Account from custom object record having the latest/most recent OrderApi_Term_start_date__c field value.

I also have another requirement to populate the count of active records from the custom object on account which is working fine. The first part of the requirement is not working.

I wrote the below code. If I use Trigger.new in the for loop for(OrderApi__Subscription__c sub: Trigger.new) , it is updating the information from whichever record was updated recently. If I use the below code, it does not update anything.
  1. trigger updateAccount on OrderApi__Subscription__c (after insert, after update) {
  2.  
  3.     Map<ID, Account> parentAcc;
  4.     List<Id> listIds = new List<Id>();
  5.     List<Account> accountList = new List<Account>();
  6.     List<Account> accountList1 = new List<Account>();
  7.         
  8.     for (OrderApi__Subscription__c childObj : Trigger.new) {
  9.     listIds.add(childObj.OrderApi__Account__c);
  10.     }
  11.     
  12.     Integer soql = [Select count() FROM OrderApi__Subscription__c WHERE OrderApi__Account__r.Id =:listIds AND OrderApi__Is_Active__c != FALSE];
  13.     
  14.     parentAcc = new Map<Id, Account>([SELECT id, Collegiate_Current_Term_Start_Date__c, Member_Count__c, Collegiate_Paid_Through_Date__c, Collegiate_Current_Term_End_Date__c, Collegiate_Chapter_Status__c, Collegiate_Chapter_Activated_Date__c  FROM Account WHERE ID IN :listIds]);
  15.     
  16.     List<OrderApi__Subscription__c> subscription = [SELECT ID, OrderApi__Term_End_Date__c FROM OrderApi__Subscription__c WHERE id IN: listIds AND OrderApi__Term_End_Date__c != NULL Order By OrderApi__Term_End_Date__c desc LIMIT 1];
  17.     
  18.     Subscription__c mhc = Subscription__c.getInstance();
  19.     string ItemCollID = mhc.Collegiate_Item_Id__c;
  20.     
  21.     for(OrderApi__Subscription__c sub:subscription){
  22.     
  23.     if(sub.OrderApi__Item__c != NULL && sub.OrderApi__Item__c == ItemCollID) {
  24.  
  25.             Account newAcc = parentAcc.get(sub.OrderApi__Account__c);
  26.  
  27.             newAcc.Collegiate_Chapter_Status__c=sub.OrderApi__Status__c;
  28.  
  29.             newAcc.Collegiate_Chapter_Activated_Date__c=sub.OrderApi__Activated_Date__c;
  30.  
  31.             newAcc.Collegiate_Paid_Through_Date__c =sub.OrderApi__Paid_Through_Date__c;
  32.  
  33.             newAcc.Collegiate_Current_Term_End_Date__c=sub.OrderApi__Current_Term_End_Date__c;
  34.  
  35.             newAcc.Collegiate_Current_Term_Start_Date__c=sub.OrderApi__Current_Term_Start_Date__c;
  36.  
  37.             accountList.add(newAcc);
  38.  
  39.         }
  40.         
  41.     }
  42.     
  43.     for(OrderApi__Subscription__c subs:Trigger.new){
  44.     
  45.         Account newAcc1 = parentAcc.get(subs.OrderApi__Account__c);
  46.         
  47.         newAcc1.Member_Count__c = soql;
  48.         
  49.         accountList1.add(newAcc1);
  50.         
  51.     }
  52.     
  53.     if(accountList.size()>0) {
  54.  
  55.     update accountList;
  56.  
  57.     }
  58.        
  59.     if(accountList1.size()>0) {
  60.  
  61.     update accountList1;
  62.  
  63.     } 
  64.         
  65. }

 
Requirement: I have 2 objects "Contact" and a custom object "OrderApi__Subscription__c". When the custom object record is inserted/updated, I want to update the certain information present on custom object record on Contact object when a lookup field on custom object = 'ABC'

Below is the trigger I wrote which is not giving me any error, but it is not updating the fields on contact

trigger updateContact on OrderApi__Subscription__c (after insert, after update) {

    Set<String> contactIds = new Set<String>();
    List<Contact> conupdateList = new List<Contact>();
    Integer count = 0;
    
    for(OrderApi__Subscription__c sub: Trigger.new) {
    contactIds.add(sub.OrderApi__Contact__c);
    }
    
    List<Contact> contactList = [Select id, Chapter_Member_Status__c, Chapter_Member_Activated_Date__c, Member_Paid_Through_Date__c, Member_Term_End_Date__c, Member_Term_Start_Date__c from Contact where id IN : contactIds];
    
    List<OrderApi__Subscription__c> subList = [Select id, OrderApi__Status__c , OrderApi__Item__c , OrderApi__Item__r.Name, OrderApi__Activated_Date__c, OrderApi__Paid_Through_Date__c, OrderApi__Current_Term_Start_Date__c, OrderApi__Current_Term_End_Date__c from OrderApi__Subscription__c where id IN: contactIds];
    
    List<Contact> contactList2 = new List<Contact>();
    for(OrderApi__Subscription__c sub: subList) {
    if(sub.OrderApi__Item__r.Name == 'ABC') {
    count++;
    }
    }
    
    for(Contact con:contactList) {
    OrderApi__Subscription__c membership = new OrderApi__Subscription__c();
    con.Chapter_Member_Status__c = membership.OrderApi__Status__c;
    String activatedDate = string.valueof(membership.OrderApi__Activated_Date__c);
    con.Chapter_Member_Activated_Date__c = activatedDate;
    String paidThroughDate = string.valueof(membership.OrderApi__Paid_Through_Date__c);
    con.Member_Paid_Through_Date__c = paidThroughDate;
    String termEndDate = string.valueof(membership.OrderApi__Current_Term_End_Date__c);
    con.Member_Term_End_Date__c = termEndDate;
    String termStartDate = string.valueof(membership.OrderApi__Current_Term_Start_Date__c);
    con.Member_Term_Start_Date__c = termStartDate;
    contactList2.add(con);
    }
    
    if(count > 0) {
    update contactList2;
    }   
}

 

Hello,

Can anyone help me build a trigger for the below requirement please? I'm new to using delete triggers and triggers in general. Thank you!

Requirement: I have 2 objects "Order__c" and "Detail__c". Order__c is the parent and Detail__c (the children) has a lookup to order. There is a field on Detail__c called "Class__c" which is a lookup to another Item object. The Class__c field can hold two types of values only. One is "Regular" and other is "Linear".

Now when a Detail__c record is deleted, if any of the remaining detail records contain Class__c = Linear, then nothing should happen. If the Detail__c records related to Order__c contains only Class__c = Regular, then the Detail__c record (Class__c = Regular) and the correspionding Order__c record should be deleted

Below is the trigger I tried, but it is giving a null-pointer exception. I do not know if this trigger will perform the requirement stated above even if the error is corrected. Quick help will be highly appreciated. Many thanks!
 

trigger deleteOrder on Detail__c (after delete) {

    List<Order__c> orderDelete = new List<Order__c>();
    Set<Id> orderId = new Set<Id>();  
    Integer count = 0;
    
    for(Detail__c d: trigger.old) {
        orderId.add(d.Order__c);
    }
    
    Map<Id,Order__c> orderMap = new Map<Id,Order__c>([select Id from Order__c where Id In :orderId]);

    for(Detail__c d :trigger.old) {
        if(d.Class__r.Name.contains('Linear')){
        count++;
        Order__c o = orderMap.get(d.Order__c);
        orderDelete.add(o);
        }
    }
    
    if(count == 0) {
       delete orderDelete;
    }
}

 

Hi Guys,

I'm trying to add contacts to a public group when a flag on contact 'Add to group' is set to True but receiving Attempt to de-ref null object error on the line highlighted. Any idea what is wrong? Thanks in advance. Pasting the trigger & helper class below.

Trigger:
  1. trigger addToGroup on Contact (after insert, after update) {
  2.  
  3.     List<GroupMember> groupList = new List<GroupMember>();
  4.     
  5.     
  6.     Set<String> contactEmails = new Set<String>();
  7.     
  8.     for(Contact con : Trigger.New) {
  9.     
  10.         contactEmails.add(con.email);
  11.     }
  12.      
  13.      Map<String,User> emailUserMap = new Map<String,User>();
  14.  
  15.      for(User u:[select id, email from User where email in : contactEmails]) {
  16.         emailUserMap.put(u.email, u);
  17.      }
  18.      
  19.      List<Id> userIdList = new List<Id>();
  20.      for(Contact con : Trigger.New) {
  21.         if(con.Add_to_Group__c == True) { 
  22.             userIdList.add(emailUserMap.get(con.email).id);  
  23.         }
  24.      }  
  25.      
  26.      Group grp = [select id from Group where Name = 'Account Group'];
  27.      if(null != grp) {
  28.  
  29.         ContactTriggerHelper.addUsersToGroup(grp.id,userIdList);
  30.     }
  31.  
  32. }
Helper class:
  1. public class ContactTriggerHelper {
  2.  
  3.     @future
  4.  
  5.     public static void addUsersToGroup(String groupId, List<Id> userIds) {
  6.     List<GroupMember> gmList = new List<GroupMember>();
  7.         for(ID userId: userIds){
  8.  
  9.             GroupMember gm = new GroupMember();
  10.             gm.GroupId = groupId;
  11.             gm.UserOrGroupId = userId;
  12.             gmList.add(gm);
  13.         }
  14.        
  15.         if(gmList.size() > 0) {
  16.             insert gmList;
  17.         }
  18.     }
  19. }
Hi Guys,

The functionality i'm trying to perform here is to update the parent object status to complete when all related child object records have a status complete. But i'm receiving the error "Error: Compile Error: Variable does not exist: id at line 11"

I'm pasting the code below. Can you please help me out here. Thanks!!

trigger updateParentStatus on Child_Object__c (after insert, after update) {

    Set<Id> parentIds = new Set<Id>();
    Integer count = 0;

        for(Child_Object__c wd: Trigger.new) {
            parentIds.add(wd.Parent_Object__c);
        }
    
    List<Parent_Object__c> parentList = [Select id, Status__c from Parent_Object__c where id =: parentIds];
    List<Child_Object__c> childList = [Select id, Status__c from Parent_Object__c Where parentIds =: parentList.id];
    
    for(Child_Object__c wd: Trigger.new) {
        if(wd.Status__c != 'Completed') {
            count++;
        }
    }
    
    for(Parent_Object__c wo: parentList) {
        wo.Status__c = 'Completed';    
    }
    
    if(count==0) {
        update parentList;
    }
}