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
Vinit Sharma 100Vinit Sharma 100 

Triggers:-Add Products & Product Bundle on Subscribers

How can I solve this scenario:-

Create an Object called Product, Subscriber, Product Bundle, Product To Product Bundle Junction
Fields on Product & Product Bundle
Lookup To Opportunity
Lookup To Subscriber

Fields on Product To Product Bundle Junction
Lookup to Product
Lookup to Product Bundle

Field on Subscriber
Lookup to Opportunity

Create a trigger on Subscriber to add Products based on Products or Products Bundles associated on Opportunity.

 

 

Vinit Sharma 100Vinit Sharma 100
Is this right way to solve this scenario:-

Trigeer On Subscriber Object:-

trigger AddSubscriberToOpportunity on Subscriber__c (after insert)
{
    Set<Id> parentIdSet = new Set<Id>();
    for(Subscriber__c record : trigger.new){
        parentIdSet.add(record.Opportunity__c);
    }
    Map<Id,Opportunity__c> allParentRecords  = new Map<Id,Opportunity__c>([Select Id from Subscriber__c where id in : parentIdSet]);
    for(Subscriber__c record : trigger.new)
    {
        if(allParentRecords.size() > 0 && allParentRecords.containsKey(record.Opportunity__c))
        {
            if(allParentRecords.get(record.Opportunity__c).Name == null || allParentRecords.get(record.Opportunity__c).Name == '')
            {
                allParentRecords.get(record.Opportunity__c).Name = record.Name;
            }
           else{
                    allParentRecords.get(record.Opportunity__c).Name = allParentRecords.get(record.Opportunity__c).Name+''record.Name);
                }
        }
        
    }

    update allParentRecords.values();
}
Vinit Sharma 100Vinit Sharma 100
Trigger on Product Object:-

trigger AddProductToSubscriber on ProductCustom__c (before insert)
{
//Collect List Of Contacts Being Inserted without an Account
List<ProductCustom__c> needSubs = new List<ProductCustom__c>();
    
    for( ProductCustom__c pr : Trigger.new)
    {
        if(String.isBlank(pr.Subscriber__r.Id))  {
           needSubs.add(pr);                                     
      }
    }
    if(needSubs.size()>0){
        List<Subscriber__c> newSubscriber = new List<Subscriber__c>();
        Map<String,ProductCustom__c> productbyNameKeys = new Map<String,ProductCustom__c>();
        //Create Subscriber for each Product
        for(ProductCustom__c pr : needSubs){
            String Name = pr.Name;
            productbyNameKeys.put(Name,pr);
           Subscriber__c sub = new Subscriber__c(Name=Name);
            new Subscriber__c.add(sub);
            
        }
        insert Subscriber__c;
        for(Subscriber__c sub : new Subscriber__c){
            if(productbyNameKeys.containsKey(sub.Name)){
               productbyNameKeys.get(sub.Name).Subscriber__r.Id;
            }
        }
    }
}