function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KR_ForceKR_Force 

Can some one please help me with this code??

Need to auto populate productionsubscription__c(multi piclist on contact). all i have to do is based on Contact.AccountID i need to pull the Linked accounts and
1.Filter those linked accounts by agent_c == true && cases__c == true && expiration__c > today (or expiration_c empty) 
2. Get the agents of the of the linked accounts filtered in the previous step (i.e. a list of all linkedAccount.agent_Contact__c).
 
3. Get the linked accounts of the agents gotten in the previous step. and
select LINKED_ACCOUNT_FIELDS
from Linked_account__c
where isDeleted = false
and Agent__c = false
and Agent_Contact__c in (STEP_2_AGENTS)
Step 4: for all these linked account.AccountS and Contact.AccountID need to query the asset with product familiy field and get the uniq value and populate contact productSubscription__c field.

I'mcalling this method from before insert trigger.

*****ANY HELP WILL BE MUCH APPRICITED*******
  1. public static void ContactProductSubscription(List<Contact>newcontacts,set<ID>acIds){
  2.     //set<id>acIds=new set<Id>();
  3.     set<id>acIds1=new set<Id>();
  4.     list<Asset>ast=new List<Asset>();
  5.     List<Linked_Account__c>lActs=new List<Linked_Account__c>();
  6.     List<Linked_Account__c>linkedAccounts=new List<Linked_Account__c>();
  7.     Map<Id,Linked_Account__c>actLActMap=new Map<Id,Linked_Account__c>();
  8.     Map<Id,ID>alActs=new Map<Id,ID>();
  9.     Map<Id,Id>conActMap=new Map<Id,Id>();
  10.     Map<ID,String>ActPfamilyMap=new Map<ID,String>();
  11.     set<id>aContacts=new set<id>();//Agent Contacts
  12.    
  13.     for(contact c :newcontacts){conActMap.put(c.id,c.accountID);}
  14.      if(acids.size()>0){
  15.       lActs=[select Active__c, Agent__c, Name, Agent_Contact__c,Agent_Contact__r.account.ID,Agent_Contact_AccountId__c, Assets__c,
  16.       Cases__c, Contacts__c, Expiration__c, Account__c, Id, User_Account__c, User_Contact__c FROM Linked_Account__c
  17.       where Agent__c=true and  Contacts__c=true and (Expiration__c<>null OR Expiration__c >: date.today()) and Linked_Account__c in:acids ];
  18.      }
  19.      if(lActs.size()>0){
  20.      for(Linked_Account__c l:lActs){
  21.       aContacts.add(l.Agent_Contact__c);
  22.       actLActMap.put(l.Linked_Account__c,l);
  23.      
  24.       }
  25.      }
  26.      if(aContacts.size()>0){
  27.       linkedAccounts=[select Active__c, Agent__c, Name, Agent_Contact__c,Agent_Contact__r.account.ID,Agent_Contact_AccountId__c, Assets__c,
  28.       Cases__c, Contacts__c, Expiration__c, Account__c, Id, User_Account__c, User_Contact__c FROM Linked_Account__c
  29.       where Agent__c=false and  isDeleted=false and Agent_Contact__c in:aContacts];
  30.      }
  31.      if(linkedAccounts.size()>0){
  32.      For(Linked_Account__c l1:linkedAccounts){
  33.       acIds1.add(l1.Account__c);
  34.       //aContact.put(l1.Agent_Contact__c,l1.Account__c);
  35.      }
  36.      }
  37.      acIds1.addAll(acIds);
  38.      String psubscription;
  39.      if(acIds1.size()>0){ast=[select AccountId, Id, Product_Family__c FROM Asset where AccountId in:acIds1];}
  40.      for(Asset a:ast){ActPfamilyMap.put(a.AccountId,a.Product_Family__c);}
  41.      for(contact c :newcontacts){
  42.       list<Linked_Account__c>l1=actLActMap.get(c.AccountId);
  43.       set<id>acts=add(c.accountid);
  44.      for(Linked_Account__c l:l1){acts.add(alActs.get(l.Agent_Contact__c));
  45.       List<Asset>ast=ActPfamilyMap.get(acts);
  46.       }
  47.      for(Asset a1:ast){
  48.      psubscription+=a1.Product_Family__c+'; ';}
  49.      c.productSubscriptions__c=psubscription;
  50.      }
  51.    }
Best Answer chosen by KR_Force
KR_ForceKR_Force
Im able to resolve the issue.

All Answers

pradeep naredlapradeep naredla
Hi kr__force,
     Are u getting any errors if yes post the error below.

thanks,
pradeep

KR_ForceKR_Force
HI Pradeep...unfortunately im not getting any error....-based on my limited knowledge i build thos code and its not working.

I'm a total newbie to Apex and have diligently searched and searched through out the forums for a solution to my problem..
pradeep naredlapradeep naredla
hi,
 Check ur trigger is in active mode or not checked ur code its ok i didn't find error.
 
 thanks,
pradeep.
KR_ForceKR_Force
Pardeep@ Trigger is active and its invoking the method fine.
KR_ForceKR_Force
im getting below error now.

Error: Invalid Data.
Review all error messages below to correct your data.
An Error occured at trigger XXXXXXXXX on contact:System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com about custom indexing. Even if a field is indexed a filter might still not be selective when: 1. The filter value includes null (for instance binding with a list that contains null) 2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times)
KR_ForceKR_Force
Im able to resolve the issue.
This was selected as the best answer