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
sreenath reddy 21sreenath reddy 21 

trigger creation Help


Hi all,This is the question asked in interview, how to solve it, please help me

User-added image
Best Answer chosen by sreenath reddy 21
Maharajan CMaharajan C
Hi Sreenath,

Please try like below :
 
trigger companyTrigger on company__c (after insert) {
	
	set<string> prodSet = new set<string>();
	Map<String,Id> prodMap = new Map<String,Id>();
	List<company_Product_Relationship__c> cprList = new company_Product_Relationship__c();
	for(company__c comp : Trigger.New){
		if(comp.product__c != null){
			 prodSet.addAll(comp.product__c.split(';'));
		}
	}
	
	for(product__c prod : [Select Id,Name from Product__c where Name IN: prodSet ]){
		prodSet.put(prod.Name, prod.Id);
	}
	
	for(company__c comp : Trigger.New){
		if(comp.product__c != null){
			 for(String str : comp.product__c.split(';')){
				company_Product_Relationship__c cpr = new company_Product_Relationship__c(company__c = comp.Id, product__c = prodSet.get(str));
				cprList.add(cpr);
			 }
		}
	}
	
	if(!cprList.IsEmpty())
		insert cprList;
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Sreenath,

Please try like below :
 
trigger companyTrigger on company__c (after insert) {
	
	set<string> prodSet = new set<string>();
	Map<String,Id> prodMap = new Map<String,Id>();
	List<company_Product_Relationship__c> cprList = new company_Product_Relationship__c();
	for(company__c comp : Trigger.New){
		if(comp.product__c != null){
			 prodSet.addAll(comp.product__c.split(';'));
		}
	}
	
	for(product__c prod : [Select Id,Name from Product__c where Name IN: prodSet ]){
		prodSet.put(prod.Name, prod.Id);
	}
	
	for(company__c comp : Trigger.New){
		if(comp.product__c != null){
			 for(String str : comp.product__c.split(';')){
				company_Product_Relationship__c cpr = new company_Product_Relationship__c(company__c = comp.Id, product__c = prodSet.get(str));
				cprList.add(cpr);
			 }
		}
	}
	
	if(!cprList.IsEmpty())
		insert cprList;
}

Thanks,
Maharajan.C
This was selected as the best answer
sreenath reddy 21sreenath reddy 21
Hi maharajan,
Small changes,

trigger CreateRecord on Company__c (after insert) {
      set<string> prodSet = new set<string>();
    Map<String,Id> prodMap = new Map<String,Id>();
    List<Company_Product_Relationship__c> cprList = new List<company_Product_Relationship__c>();
    for(company__c comp : Trigger.New){
        if(comp.product__c != null){
             prodSet.addAll(comp.product__c.split(';'));
        }
    }
    
    for(product__c prod : [Select Id,Name from product__c where Name IN: prodSet ]){
        prodMap.put(prod.Name, prod.Id);
    }
    
    for(company__c comp : Trigger.New){
        if(comp.product__c != null){
             for(String str : comp.product__c.split(';')){
                company_Product_Relationship__c cpr = new company_Product_Relationship__c(company__c = comp.Id, product__c = prodMap.get(str));
                cprList.add(cpr);
             }
        }
    }
    
    if(!cprList.IsEmpty())
        insert cprList;
}