• jayshree kb
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies
HI I have a custom object where there are two lookfields Observation__c and User__c..both are lookup fields ..Based upon these two field values, duplication should not be allowed .. any idea how to incorporate it
 
I have an Obj called Observation. Im trying to write a trigger so when each time a record in inserted in Observation a new record has to be created in another obj called Share_Staging_Table__c. Im geeting an error when im trying to assign value to the lookupfield .Pls find the code and pls do help.


public class ObsTriggerHandler {
List<Share_Staging_Table__c> SSTList= new list<Share_Staging_Table__c>();
Set<Id> userSet = new Set<Id>();   
List <User> ObsUser = new List <User>();
public void Afterinsert(List<Observation__c> newRec)
  {
for(Observation__c recObservation : newRec){ 
    userSet.add(recObservation.LastModifiedBy.id);
    ObsUser = [select Name ,id from User where id IN :userSet ];
    Share_Staging_Table__c SST = new Share_Staging_Table__c() ; 
    SST.Observation__c = recObservation.Name;
    SST.Shared_with__c = ObsUser[0].id;
    SSTList.add(SST);  
    }
  }
    if(SSTList != null && SSTList.size() != 0) 
    {
        Database.insert(SSTList) ;
    }
   
   }
I I have two custom obj one is called Parent and the other Child..lookup relationship exists between the two obj..i want to count the child records for each parent and update it in a field named "ContactsCount"..I facing some exception error when tryin to insert Child records..below is the code
trigger ConCount on Child__c (after insert) {
Set <id> CHildIds = new set<id>();
List<Child__c> ChildList = new List<Child__c>();
List<Child__c> ListCon = new List<Child__c>();
List<Parent__c> ParentList = new List <Parent__c>();
List<Parent__c> ListParent = new List <Parent__c>();
Map<id,integer> ConMap = new map <id,integer>();
if(Trigger.isInsert){
    for(Child__c Chil : Trigger.New){
    CHildIds.add(Chil.Parentlookup__c);}   
    ParentList = [select id, name from Parent__c where id IN :CHildIds];
    ChildList = [select id,name , Parentlookup__c from Child__c where Parentlookup__c IN :CHildIds];
        for(Parent__c P :ParentList){
        Listcon.clear();
        for(Child__c C : ChildList){
        if(C.Parentlookup__c == P.id){
        ListCon.add(C);
         ConMap.put(P.id,ListCon.size());
         
         } }}
        if(ParentList.size()>0){
        for(Parent__c Pa : ParentList){
        if(ConMap.size()>0){
        Pa.ContactsCount__c = ConMap.get(Pa.id);
        ListParent.add(Pa);
        }}}
        if(ListParent.size()>0)
        update ListParent;
        }}

 
I have an Obj called Observation. Im trying to write a trigger so when each time a record in inserted in Observation a new record has to be created in another obj called Share_Staging_Table__c. Im geeting an error when im trying to assign value to the lookupfield .Pls find the code and pls do help.


public class ObsTriggerHandler {
List<Share_Staging_Table__c> SSTList= new list<Share_Staging_Table__c>();
Set<Id> userSet = new Set<Id>();   
List <User> ObsUser = new List <User>();
public void Afterinsert(List<Observation__c> newRec)
  {
for(Observation__c recObservation : newRec){ 
    userSet.add(recObservation.LastModifiedBy.id);
    ObsUser = [select Name ,id from User where id IN :userSet ];
    Share_Staging_Table__c SST = new Share_Staging_Table__c() ; 
    SST.Observation__c = recObservation.Name;
    SST.Shared_with__c = ObsUser[0].id;
    SSTList.add(SST);  
    }
  }
    if(SSTList != null && SSTList.size() != 0) 
    {
        Database.insert(SSTList) ;
    }
   
   }