• nitin sharma 366
  • NEWBIE
  • 35 Points
  • Member since 2019

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 4
    Replies
Hey everyone-trying to write a simple trigger for populating a couple of custom lookup fields on Contacts. We have 2 custom fields called "First Campaign" and "Most Recent Campaign". I'm trying to get "First Campaign" populated with the first campaign the Contact was added to and "Most Recent Campaign" with the latest campaign they've been added to. Here's what I have so far, would love any help with this. Thanks in advance!
trigger FirstAndMostRecentCampaignOnContact on Contact (after insert, after update) {

    Set<Id> CampaignIds = new Set<Id>();
    for(Contact CON: Trigger.new) CampaignIds.add (CON.MostRecentCampaign__c);
    List<Campaign> c= new List<Campaign>([
        select 
        Id
        from Campaign 
        where Id = :CampaignIds ORDER BY CreatedDate DESC NULLS LAST LIMIT 1
    ]);
    Map<Id, List<Campaign>> cmMap = new Map<Id, List<Campaign>>();
    for (Campaign cm: c) {
        if (cmMap.containsKey(cm.Id)) {
            List<Campaign> x;
            x = cmMap.get(cm.Id);
            x.add(cm);
            cmMap.put(cm.Id, x);
        } else {
            List<Campaign> tmp = new List<Campaign>();
            tmp.add(cm);
            cmMap.put(cm.Id, tmp);
        }
    }
    List<Contact> CON1 = new List<Contact>(); 
    for(Contact newCon: Trigger.new){
        if (cmMap.containsKey(newCon.MostRecentCampaign__c)) {
            for (Campaign cm: cmMap.get(newCon.MostRecentCampaign__c)) {
                CON1.add(
                    New Contact(
                        id=newCon.id)                    
                );
            }
        }
    }
}

 
Hi All,

Objects which are a part of my trigger.

Opportunity,Opportuity line Item, Price book entry and Product object 

I have a custom field cost_price__c on the product object and quantity on the opportunity line item....I am simply multiplying quantity field with with cost_price__c which is on the product table and updating a field on the Opportunity obect known as Total_price_of_all_the_products__c.

However,I am getting an error

caused by: System.NullPointerException: Attempt to de-reference a null object

Below given line is causing an error.Sombody please help

Opportunity abc=m.get(opportunitylineitemRecords.opportunityid);


trigger UpdateOppWithPriceAndQty on OpportunityLineItem (After Insert)
{

Opportunity opp;
set<id>Ids=new set<id>();
set<id>Prodid=new set<id>();
Map<id,opportunitylineitem>oppMap=new Map<id,opportunitylineitem>();
for(opportunitylineitem f:trigger.new)
{
    
oppmap.put(f.opportunityid,null);
prodid.add(f.product2id);
    
}
    
Map<id,opportunity>m=new map<id,opportunity>([Select id,Total_price_of_all_the_products__c from opportunity where id in:oppmap.keyset()]);
list<pricebookentry>ProdOpplineItemRec=[Select id,product2id,Product2.Cost_price__c,(select opportunityid,quantity from opportunitylineitems) from pricebookentry where product2id in:prodid];  
list<opportunity>updateOpp=new List<opportunity>();
for(pricebookentry pri:ProdOpplineItemRec)
{
    for(opportunitylineitem opportunitylineitemRecords :pri.opportunitylineitems)
        {

            if(pri.product2.cost_price__c!=null)
                {
                                    
                    
Opportunity abc=m.get(opportunitylineitemRecords.opportunityid); 
abc.Total_price_of_all_the_products__c+=(pri.product2.cost_price__c*opportunitylineitemRecords.Quantity);
                    
                    
                   //system.debug('record has the following values'+abc.Total_price_of_all_the_products__c);
                   //abc.Total_price_of_all_the_products__c=(pri.product2.cost_price__c*opportunitylineitemRecords.Quantity);
                   //updateOpp.add(abc);
                      
                        
                 }
                         
                       
                    
                                       
                       
                   
                }
            //updateOpp.add(abc);
      
            
}
if(updateOpp.size()>0)
    {
        try
        {
            
            //update updateOpp;
            
        }
        
        Catch(DmlException de)
        {
            
            System.debug('Exception has been raised as error message has been created');
            
        }
    }
    
}
   

 
Hi All,
I am only trying to update a check box on an Opp object when stagename is changed to closed won but i get the following error

Compile Error: Variable does not exist: stagename at line 10 column 32

I want to write most of the code in the class not in the trigger .So please tell me how can i write this code in the class.



public class UpdateOppCheckBox
{
public static void UpdateOpp(List<opportunity>listOpps)
{
//list<opportunity>updte=new list<opportunity>();

for(opportunity opp1:ListOpps)
{

if(trigger.oldmap.get(opp1.id).stagename!=trigger.newmap.get(opp1.id).stagename && opp1.stagename=='closed won')
{
opp1.Update_Checkbox__c=true;
}
}

}



 
Using process builder I wants to create a task for the owner after an opportunity has been in a particular stage for 10 days.How do I get the rule trigger date in process builder?.

Condition would be :-Stage Ischanged  True 
But how do I get the Rule trigger date in process builder

Thanks,
Object Reference for Salesforce and Lightning Platform

                            Product and Schedule Objects

In the Diagram when I look at the OpportunityLineItem.I see the following fields 

1)ID
2)OpportunityID
3)PriceBookEntryID

It does not talk about the Product ID,Why is it so?

However ,In the Salesforece org when I look at the fields of OpportunityLineitem I do see ProductID as lookup field.Can somebody explian why ProductID is missing from the  diagram.I am sure I am missing something here.



 

Object Reference for Salesforce and Lightning Platform

                            Product and Schedule Objects

In the Diagram when I look at the OpportunityLineItem.I see the following fields 

1)ID
2)OpportunityID
3)PriceBookEntryID

It does not talk about the Product ID,Why is it so?

However ,In the Salesforece org when I look at the fields of OpportunityLineitem I do see ProductID as lookup field.Can somebody explian why ProductID is missing from the  diagram.I am sure I am missing something here.



 
Hi Everbody,

I have AfterInsert Trigger on Opportunity object so when an opportunity is inserted ,task should be automatically created and associated with that opportunity.Trigger is working fine and now I have written test class for the trigger which is given below.I need to know if I can write better test class or below given class is fine.

Secondly,How do I test for exceptions in the code.How do I fail records in the tests class to ensure that test class is covering all the scenarios.

Two things needs to be tested.
1)Successful bulk insertion of records
2)Failed records .


Please advice.

@isTest
public class TaskCreation {
     @isTest static void TestTaskcreation()
     {
         list<opportunity>OppRecords=new list<opportunity>();
         
         for(integer i=0;i<200;i++)
         {
             
         Opportunity opp=new opportunity();
         opp.Name='New opportunity';
         opp.stagename='Prospecting';
         opp.Closedate=system.Today().addMonths(1);
         OppRecords.add(opp);
                     
         }
         
         //insert oppRecords;
                  
         
         List<opportunity>InsertedOpps=new list<opportunity>([select id from opportunity where id in:oppRecords]);
         
         List<task>TaskTobeInserted=new list<task>();
             
        for(opportunity Fetched:InsertedOpps) 
        {
            
            Task t=new task();
            t.subject='New task has been created for new opportunity';
            t.whatid=Fetched.id;
            TaskTobeInserted.add(t);
                        
        }
         
         insert TaskTobeInserted; 
         
     
         Test.startTest();
      
         Database.SaveResult[] result= Database.Insert(oppRecords,false);
               
         set<id>SuccessfullOppIds=new set<id>();   
        
         for(Database.SaveResult sr : Result)
         {
         If(sr.isSuccess())
         {
                          
             SuccessfullOppIds.add(sr.getId());
             
         } 
             
         }
            for(list<task>r:[Select id,subject from task where whatid in:SuccessfullOppIds])
             { 
             for(Task CheckSubject:r)
             {
             
                                  
                 System.assertEquals('New task inserted on Opportunity creation',CheckSubject.subject);
             }

             }    
         
         
             
         
         Test.stopTest();
     }
    
}




 
Hi All,

Objects which are a part of my trigger.

Opportunity,Opportuity line Item, Price book entry and Product object 

I have a custom field cost_price__c on the product object and quantity on the opportunity line item....I am simply multiplying quantity field with with cost_price__c which is on the product table and updating a field on the Opportunity obect known as Total_price_of_all_the_products__c.

However,I am getting an error

caused by: System.NullPointerException: Attempt to de-reference a null object

Below given line is causing an error.Sombody please help

Opportunity abc=m.get(opportunitylineitemRecords.opportunityid);


trigger UpdateOppWithPriceAndQty on OpportunityLineItem (After Insert)
{

Opportunity opp;
set<id>Ids=new set<id>();
set<id>Prodid=new set<id>();
Map<id,opportunitylineitem>oppMap=new Map<id,opportunitylineitem>();
for(opportunitylineitem f:trigger.new)
{
    
oppmap.put(f.opportunityid,null);
prodid.add(f.product2id);
    
}
    
Map<id,opportunity>m=new map<id,opportunity>([Select id,Total_price_of_all_the_products__c from opportunity where id in:oppmap.keyset()]);
list<pricebookentry>ProdOpplineItemRec=[Select id,product2id,Product2.Cost_price__c,(select opportunityid,quantity from opportunitylineitems) from pricebookentry where product2id in:prodid];  
list<opportunity>updateOpp=new List<opportunity>();
for(pricebookentry pri:ProdOpplineItemRec)
{
    for(opportunitylineitem opportunitylineitemRecords :pri.opportunitylineitems)
        {

            if(pri.product2.cost_price__c!=null)
                {
                                    
                    
Opportunity abc=m.get(opportunitylineitemRecords.opportunityid); 
abc.Total_price_of_all_the_products__c+=(pri.product2.cost_price__c*opportunitylineitemRecords.Quantity);
                    
                    
                   //system.debug('record has the following values'+abc.Total_price_of_all_the_products__c);
                   //abc.Total_price_of_all_the_products__c=(pri.product2.cost_price__c*opportunitylineitemRecords.Quantity);
                   //updateOpp.add(abc);
                      
                        
                 }
                         
                       
                    
                                       
                       
                   
                }
            //updateOpp.add(abc);
      
            
}
if(updateOpp.size()>0)
    {
        try
        {
            
            //update updateOpp;
            
        }
        
        Catch(DmlException de)
        {
            
            System.debug('Exception has been raised as error message has been created');
            
        }
    }
    
}
   

 
Hi All,

I am trying to write a SOQL statement on the account object that will select all opportunities that it is associtated through a custom look up field named "Referring Facility" on the opportunity to the account. I would like to be able to sum the amount of all of those opportunities. Right now I have this working through a junction object but I am trying to get rid of those. 

I was able to figure out how to write a trigger to do the same thing on the contact using a trigger that looks down at the contact roles takes the contact id of the referring therapist. But I am struggling to write the SOQL statement upwards from the opportunity to the account through the custom field. The closet I got it to work was to get one opportunity to write its into to the account but it wouldn't sum. 

Also, I am open to advice on the best way to do this. Ideally it would be nice if this happened when the opportunity closed and would update the account without having to press edit on the account to see the new value.

Thanks in advance for your help,
Edward
SELECT Id,(SELECT Id FROM Opportunities) FROM Account. This SOQL works but not SELECT Id,(SELECT Id FROM Opportunity) FROM Account. Can anyone explain? Also, SELECT Id FROM Opportunity worked.
Hey everyone-trying to write a simple trigger for populating a couple of custom lookup fields on Contacts. We have 2 custom fields called "First Campaign" and "Most Recent Campaign". I'm trying to get "First Campaign" populated with the first campaign the Contact was added to and "Most Recent Campaign" with the latest campaign they've been added to. Here's what I have so far, would love any help with this. Thanks in advance!
trigger FirstAndMostRecentCampaignOnContact on Contact (after insert, after update) {

    Set<Id> CampaignIds = new Set<Id>();
    for(Contact CON: Trigger.new) CampaignIds.add (CON.MostRecentCampaign__c);
    List<Campaign> c= new List<Campaign>([
        select 
        Id
        from Campaign 
        where Id = :CampaignIds ORDER BY CreatedDate DESC NULLS LAST LIMIT 1
    ]);
    Map<Id, List<Campaign>> cmMap = new Map<Id, List<Campaign>>();
    for (Campaign cm: c) {
        if (cmMap.containsKey(cm.Id)) {
            List<Campaign> x;
            x = cmMap.get(cm.Id);
            x.add(cm);
            cmMap.put(cm.Id, x);
        } else {
            List<Campaign> tmp = new List<Campaign>();
            tmp.add(cm);
            cmMap.put(cm.Id, tmp);
        }
    }
    List<Contact> CON1 = new List<Contact>(); 
    for(Contact newCon: Trigger.new){
        if (cmMap.containsKey(newCon.MostRecentCampaign__c)) {
            for (Campaign cm: cmMap.get(newCon.MostRecentCampaign__c)) {
                CON1.add(
                    New Contact(
                        id=newCon.id)                    
                );
            }
        }
    }
}