• visu
  • NEWBIE
  • 10 Points
  • Member since 2014
  • Infosys Ltd

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 1
    Likes Given
  • 4
    Questions
  • 4
    Replies
The below trigger is throwing "System.NullPointer Exception.Attempt to de-reference a null object",  when there are no products associated with opportunity! When we add Opp to a custom object(detail side) , this trigger will fetch all the Products on this Opp to a field on this custom object,if there are no products sholud leave this field empty but on saving it is throwing the above error.Kindly help out with a solution for this. Thanks....

trigger productnames on cobj__c(before insert,before update) {
List<Id> oppIds = new List<Id>();
for(cobj__c co : Trigger.new)          //cobj__c is custom object
{
oppIds.add(co.Opportunity__c);    //Opportunity__C is the Opportunity master field on this object.
}
Map<Id,List<String>> oppIdToProdNameMap = new Map<Id,List<String>>();
for(OpportunityLineItem p2: [Select Id, Product2.Name,OpportunityId from OpportunityLineItem where OpportunityId in :oppIds])
{
   
     if(oppIdToProdNameMap.containsKey(p2.OpportunityId ))
      {
        List<String> l1 = oppIdToProdNameMap.get(p2.OpportunityId);
        l1.add(p2.Product2.Name);
        oppIdToProdNameMap.put(p2.OpportunityId,l1);
      }
     else
      {
        List<String> l1 = new List<String>();
        l1.add(p2.Product2.Name);
        oppIdToProdNameMap.put(p2.OpportunityId,l1);
      }
}
for(cobj__c co : Trigger.new)
{
 
    String s = '';
    boolean first = true;
    for(String p : oppIdToProdNameMap.get(co.Opportunity__c))
    {
    
        if(!first)
            s+=',';
        first = false;
        s += p;
      
    }
    co.Product_Names__c = s;  //field to populate
}
}
  • June 12, 2014
  • Like
  • 0
The below trigger is throwing "System.NullPointer Exception.Attempt to de-reference a null object",  when there are no products associated with opportunity! When we add Opp to a custom object(detail side) , this trigger will fetch all the Products on this Opp to a field on this custom object,if there are no products sholud leave this field empty but on saving it is throwing the above error.Kindly help out with a solution for this. Thanks....

trigger productnames on cobj__c(before insert,before update) {
List<Id> oppIds = new List<Id>();
for(cobj__c co : Trigger.new)          //cobj__c is custom object
{
oppIds.add(co.Opportunity__c);    //Opportunity__C is the Opportunity master field on this object.
}
Map<Id,List<String>> oppIdToProdNameMap = new Map<Id,List<String>>();
for(OpportunityLineItem p2: [Select Id, Product2.Name,OpportunityId from OpportunityLineItem where OpportunityId in :oppIds])
{
   
     if(oppIdToProdNameMap.containsKey(p2.OpportunityId ))
      {
        List<String> l1 = oppIdToProdNameMap.get(p2.OpportunityId);
        l1.add(p2.Product2.Name);
        oppIdToProdNameMap.put(p2.OpportunityId,l1);
      }
     else
      {
        List<String> l1 = new List<String>();
        l1.add(p2.Product2.Name);
        oppIdToProdNameMap.put(p2.OpportunityId,l1);
      }
}
for(cobj__c co : Trigger.new)
{
 
    String s = '';
    boolean first = true;
    for(String p : oppIdToProdNameMap.get(co.Opportunity__c))
    {
    
        if(!first)
            s+=',';
        first = false;
        s += p;
      
    }
    co.Product_Names__c = s;  //field to populate
}
}
  • June 12, 2014
  • Like
  • 0

Hi, I created a Visualforce page for a List button to the related list object to add the child records into parent related list.

The controller I've written for that is shown below. Its adding duplicate records and as well as creating the same records again and again when ever i add the records. Will you please tell me the logic how to add the records and does not allow duplicates or create new records for  the child object....

 

public with sharing class ProgramController {
public Program__c Program ;
public List<Wrapper> wraplist{get;set;}
public ProgramController(ApexPages.StandardController controller) {
program = (Program__c) Controller.getRecord();
wraplist = new List<Wrapper>();
List<Title__c> t = [SELECT id,name,Program__c FROM Title__c LIMIT 20];

for(title__c t1:t){
wrapList.add( new wrapper(t1));
}
}
public class Wrapper{
public Boolean Selected{get;set;}
public title__c title{get;set;}
public wrapper(Title__c ti){
title = ti;
}
}
public PageReference addTitle(){
List<Title__c> newTitle = new List<Title__c>();
for(Wrapper wrap :WrapList){

if(Wrap.Selected== true)
{

title__c newtitle1 = new Title__c(Name=wrap.title.name,Program__c=program.id);

newTitle.add(newtitle1);
}
upsert newtitle;
}
return (new ApexPages.StandardController(Program)).view();
}
}

Hi, I created a Visualforce page for a List button to the related list object to add the child records into parent related list.

The controller I've written for that is shown below. Its adding duplicate records and as well as creating the same records again and again when ever i add the records. Will you please tell me the logic how to add the records and does not allow duplicates or create new records for  the child object....

 

public with sharing class ProgramController {
public Program__c Program ;
public List<Wrapper> wraplist{get;set;}
public ProgramController(ApexPages.StandardController controller) {
program = (Program__c) Controller.getRecord();
wraplist = new List<Wrapper>();
List<Title__c> t = [SELECT id,name,Program__c FROM Title__c LIMIT 20];

for(title__c t1:t){
wrapList.add( new wrapper(t1));
}
}
public class Wrapper{
public Boolean Selected{get;set;}
public title__c title{get;set;}
public wrapper(Title__c ti){
title = ti;
}
}
public PageReference addTitle(){
List<Title__c> newTitle = new List<Title__c>();
for(Wrapper wrap :WrapList){

if(Wrap.Selected== true)
{

title__c newtitle1 = new Title__c(Name=wrap.title.name,Program__c=program.id);

newTitle.add(newtitle1);
}
upsert newtitle;
}
return (new ApexPages.StandardController(Program)).view();
}
}