• Gateway Bikes Group4
  • NEWBIE
  • 20 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 11
    Questions
  • 8
    Replies
Trail Head: Build a Conference Management App >Create Apex Triggers

Working on Trigger That Sends Confirmation Emails
If i want to opt Process builder instead of trigger for this particular use case.
Please help me
Working on Creating a lightning component with Server-Side Application Controller
Please help me in resolving the issue
User-added image


Schema 
User-added image

Data in Session Object

User-added image

Data in Speaker Object
User-added image
/***ERROR Message
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger ven.IdenticalOpp9 caused an unexpected exception, contact your administrator: ven.IdenticalOpp9: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ven.LeadingCompetitor: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.ven.LeadingCompetitor: line 34, column 1: []: Trigger.ven.IdenticalOpp9: line 32, column 1

*/
/*Write a trigger that creates 10 identical Opportunities whenever an Account with more than 99 employees is created. 
Make sure all Opportunities are associated with the Account. 
Use any values for the fields on the Opportunities*/

  trigger IdenticalOpp9 on Account(after insert){
 
      List<Opportunity> opplist= new List<Opportunity>();

     
        for(Account ac:Trigger.new){
        
               if(ac.NumberOfEmployees>99){
       
                     for(integer count=0;count<10;count++){
       
                        Opportunity opp=new Opportunity();
                          opp.AccountId=ac.id;
                               opp.Name='Created from account';
                          opp.StageName='Prospecting';
                          opp.CloseDate=Date.toDay();
                          opplist.add(opp);
                            }
                      }
             else{
             }
         }
       if(opplist!=null){
       insert opplist;

 }
 
}
User-added image

User-added image
<!-------Lightning Component:Opencase.cmp----->

<aura:component controller="OpenCasesApexController" implements="force:appHostable">
<aura:attribute name="cases" type="Case[]"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:iteration items="{!v.cases}" var="case">
    <c:CaseList case="{!case}"/>
    </aura:iteration>
  </aura:component>
  
  <!--Lightning App:OpenCasesApp.app-->
<aura:application >
    <c:OpenCases/>
</aura:application>

<!---OpenCasesController.js->
({
doInit:function(cmp,event,helper){
       var action=cmp.get("c.getCaseDB");
       console.log('calling doInit');
       
       action.setCallback(this,function(response){
        var state=response.getState();
        
        if(cmp.isValid()  &&  state === "SUCCESS"){
          console.log('getting cases');
        
          cmp.set("v.cases",response.getReturnValue());
          }
});
     $A.enqueueAction(action);
}
  
   })
   
   //Apex Controller
    public with sharing class OpenCasesApexController {
 @AuraEnabled
    public static List <Case> getCaseDB(){
     String [] caseFields =new String[]{'Id','OwnerId','Type','Status','Priority',
                                     'IsClosed','Reason','Origin'};
        
         //Create a Map for all the Case object
          Map<String,Schema.SobjectField> caseMap = 
           Schema.SobjectType.Case.fields.getMap();
         
           for(String field:caseFields){
                if(!caseMap.get(field).getDescribe().isAccessible()){
                 System.debug('This field was not accessible:' +caseMap.get(field));
                 return null;
                     }
                  }   
    
         return [SELECT  Id
                      FROM Case 
                      where IsClosed=false
                      AND OwnerId =:UserInfo.getUserId() limit 5];
  }
}
<!--Nesting the Component:CaseList.cmp -->
<aura:component >
    <aura:attribute name="case" type="Case"/>
       <div onclick="{!c.goToRecord}">
        <force:recordview recordId="{!v.case.Id}" type="MINI"/>
        </div>
</aura:component>

<!--CaseListController.js-->
({
      goToRecord:function(cmp,event,helper){
    //Declaration of variable
    var sObjectEvent=$A.get("e.force:navigateToSobject");
        sObjectEvent.setParams({    "recordId":cmp.get("v.case.Id"),
                                "slideDevName":'detail'
        }) 
            sObjectEvent.fire();
        
        }
})

   
   
   
//Use case
Trigger has  to fire on Opportunity Object and Highest price has to store in Lead_CompetitorPrice__c field and Highest Price Postion to store in Leading_Competitor_del_del__c field.
trigger LeadingCompetitor on Opportunity (before insert,before update) {
for(Opportunity opp:Trigger.new){
    //Add all our prices in a list in order of competitor
     List<Decimal> competitorPrices =new List<Decimal>();
       competitorPrices.add(opp.Competitor_1_Price__c);
        competitorPrices.add(opp.Competitor_2_Price__c);
        competitorPrices.add(opp.Competitor_3_Price__c);
     //Add all our comptitors in a list in order
    List<String> competitors =new List<String>();
         competitors.add(opp.ven__Competitor_1__c);
         competitors.add(opp.ven__Competitor_2__c);
         competitors.add(opp.ven__Competitor_3__c);
   //Loop through alll competitors to find the position of the lowest price
   Decimal Highprice;
   Integer HighPricePosition;
    for(Integer i=0;i<competitorPrices.size();i++){
        Decimal currentPrice =competitorPrices.get(i);
        if(Highprice==null || Highprice>currentPrice){
           HighPrice=currentPrice;
           HighPricePosition=i;
           }
           }
         //Populate the leading competitor field with the competitor matching the lowest price position
         opp.Leading_Competitor_del_del__c=competitors.get(HighPricePosition);
        opp.Lead_CompetitorPrice__c=competitorPrices.get(Highprice)
                                   } 
I’m getting error message when I saving the record on opportunity object?

User-added image
Use Case
Trigger on the Lead object that populates a custom field (API Name: Key_Fields_Populated__c) number field whenever the Lead is created.
“Key Fields Populated” should count the total number of the following fields that are not null:
FirstName
LastName
Email
Phone
Website
Title
#1 example, if FirstName, LastName, and Email are populated, “Key Fields Populated” should equal to 3.
If at least 3 key fields are populated, create a new Task for each key field populated:
The name of each Task should be: Verify the <<key field name>> field

 #2  if any of the key fields contain the word “test” (in any combination of upper or lowercase letters), always:
Create a new Task with:
Subject: “WARNING”
Description: “This Lead contains the TEST keyword in the following key fields: <<comma separated list of only the key fields containing the TEST keyword>>”
 #3  If a custom Bypass_Triggers__c field is checked, skip all the logic 
 
Question no1: *****No Test Coverage for this below listed Test Class****
Write a trigger that creates two identical Opportunities when ever an Account is created.Make sure both opportunities are associates with the Account.Use any values for the fields on the opportunities.Just make sure to use variables when populating the fields of each opportunity to make sure they are identical.

trigger IdenticalOpp on Account(after insert){
list<Opportunity> opplist = new list<Opportunity>();
  for(Account a:Trigger.new)
    {
        Opportunity opp=new Opportunity();
        opp.AccountId=a.id;
        opp.Name='Created from account';
        opp.StageName='Prospecting';
        opp.CloseDate=Date.toDay();
        opplist.add(opp);
                Opportunity opp1=new Opportunity();
        opp1.AccountId=a.id;
        opp1.Name='Created from account';
        opp1.StageName='Prospecting';
        opp1.CloseDate=Date.toDay();
        opplist.add(opp1);
        }     
    if(opplist != null){
        insert opplist ;
    }}

@isTest
private class IdenticalOppTest {
    
@isTest
static void Opportunitycreate() {
         Account acct = new Account(Name='Test Account');
        insert acct;
        Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity',
                                       StageName='Prospecting',
                                       CloseDate=System.today().addMonths(1),
                                       AccountId=acct.Id);
        insert opp;

Question 2: ***Working on Trigger LeadCompetitor on Opportunity ***
                        What is the datatype field on Leading_Competitor__c ?
                        The Leading Competitor field unable to collect the data of                                    Competitor lowest Price

trigger LeadingCompetitor on Opportunity (before insert,before update) {

for(Opportunity opp:Trigger.new){
    //Add all our prices in a list in order of competitor
    List<Decimal> competitorPrices =new List<Decimal>();
    
        competitorPrices.add(opp.Competitor_1_Price__c);
        competitorPrices.add(opp.Competitor_2_Price__c);
        competitorPrices.add(opp.Competitor_3_Price__c);
       
    //Add all our comptitors in a list in order
    List<String> competitors =new List<String>();
         competitors.add(opp.ven__Competitor_1__c);
         competitors.add(opp.ven__Competitor_1__c);
         competitors.add(opp.ven__Competitor_1__c);
 
  //Loop through alll competitors to find the position of the lowest price
   Decimal lowestPrice;
   Integer lowestPricePosition;
   
    for(Integer i=0;i<competitorPrices.size();i++){
        Decimal currentPrice =competitorPrices.get(i);
        
        if(lowestPrice==null || currentPrice<lowestPrice){
           lowestPrice=currentPrice;
           lowestPricePosition=i;
           }
           }
           //Populate the leading competitor field with the competitor matching the lowest price position
           // opp.Leading_Competitor__c=competitors.get(lowestPricePosition);
      } 
Write a trigger that creates two identical Opportunities when ever an Account is created.Make sure both opportunities are associates with the Account.
Use any values for the fields on the opportunities
Just make sure to use variables when populating the fields of each opportunity to make sure they are identical.
 
/* Write a trigger that automatically creates an Account whenever a Lead is created. The Account be named after the last name of the Lead*/


trigger Accountcreate on Lead(after insert){
 
   for(Lead ld:Trigger.new){

         Account acc= new Account();
 
         acc.Name=ld.Lastname;
     
           insert acc;

}

}


@isTest
private class AccountcreateTest {
    

    @isTest static void Accountcreate() {
        // Implement test code
    
           Lead ld = new Lead();
             
      ld.LastName ='Creed';
                
      ld.FirstName='Apollo';
            
      ld.Company  ='Rocky';
           
         Insert ld;

}
}
/*Write a trigger that automatically changes a Contact’s email address to “hello@world.com” whenever a Contact is created. Verify it works!
*/
trigger ContactEmail on Contact(before insert){
   
 for(Contact con:Trigger.new){
        
 con.Email='hello@world.com';
 
  
}
}

@isTest
private class ContactEmailTest {

    @isTest static void createContact() {
        
// Implement test code
      
    
           Contact con = new Contact(
  
                    FirstName='fname',
 
                    LastName ='lname',
 
                    Email = 'email@gmail.com',
  
                    Phone = '9743800309');
               
                         insert con;

}
}

 
Create a Kanban view for opportunity so that Sales folk can easily access there opportunity, make sure other folks can not see opportunity of others?
Create a list view which will show property data (custom object) in the list view user should able to create charts?
User-added image

User-added image
<!-------Lightning Component:Opencase.cmp----->

<aura:component controller="OpenCasesApexController" implements="force:appHostable">
<aura:attribute name="cases" type="Case[]"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:iteration items="{!v.cases}" var="case">
    <c:CaseList case="{!case}"/>
    </aura:iteration>
  </aura:component>
  
  <!--Lightning App:OpenCasesApp.app-->
<aura:application >
    <c:OpenCases/>
</aura:application>

<!---OpenCasesController.js->
({
doInit:function(cmp,event,helper){
       var action=cmp.get("c.getCaseDB");
       console.log('calling doInit');
       
       action.setCallback(this,function(response){
        var state=response.getState();
        
        if(cmp.isValid()  &&  state === "SUCCESS"){
          console.log('getting cases');
        
          cmp.set("v.cases",response.getReturnValue());
          }
});
     $A.enqueueAction(action);
}
  
   })
   
   //Apex Controller
    public with sharing class OpenCasesApexController {
 @AuraEnabled
    public static List <Case> getCaseDB(){
     String [] caseFields =new String[]{'Id','OwnerId','Type','Status','Priority',
                                     'IsClosed','Reason','Origin'};
        
         //Create a Map for all the Case object
          Map<String,Schema.SobjectField> caseMap = 
           Schema.SobjectType.Case.fields.getMap();
         
           for(String field:caseFields){
                if(!caseMap.get(field).getDescribe().isAccessible()){
                 System.debug('This field was not accessible:' +caseMap.get(field));
                 return null;
                     }
                  }   
    
         return [SELECT  Id
                      FROM Case 
                      where IsClosed=false
                      AND OwnerId =:UserInfo.getUserId() limit 5];
  }
}
<!--Nesting the Component:CaseList.cmp -->
<aura:component >
    <aura:attribute name="case" type="Case"/>
       <div onclick="{!c.goToRecord}">
        <force:recordview recordId="{!v.case.Id}" type="MINI"/>
        </div>
</aura:component>

<!--CaseListController.js-->
({
      goToRecord:function(cmp,event,helper){
    //Declaration of variable
    var sObjectEvent=$A.get("e.force:navigateToSobject");
        sObjectEvent.setParams({    "recordId":cmp.get("v.case.Id"),
                                "slideDevName":'detail'
        }) 
            sObjectEvent.fire();
        
        }
})

   
   
   
Question no1: *****No Test Coverage for this below listed Test Class****
Write a trigger that creates two identical Opportunities when ever an Account is created.Make sure both opportunities are associates with the Account.Use any values for the fields on the opportunities.Just make sure to use variables when populating the fields of each opportunity to make sure they are identical.

trigger IdenticalOpp on Account(after insert){
list<Opportunity> opplist = new list<Opportunity>();
  for(Account a:Trigger.new)
    {
        Opportunity opp=new Opportunity();
        opp.AccountId=a.id;
        opp.Name='Created from account';
        opp.StageName='Prospecting';
        opp.CloseDate=Date.toDay();
        opplist.add(opp);
                Opportunity opp1=new Opportunity();
        opp1.AccountId=a.id;
        opp1.Name='Created from account';
        opp1.StageName='Prospecting';
        opp1.CloseDate=Date.toDay();
        opplist.add(opp1);
        }     
    if(opplist != null){
        insert opplist ;
    }}

@isTest
private class IdenticalOppTest {
    
@isTest
static void Opportunitycreate() {
         Account acct = new Account(Name='Test Account');
        insert acct;
        Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity',
                                       StageName='Prospecting',
                                       CloseDate=System.today().addMonths(1),
                                       AccountId=acct.Id);
        insert opp;

Question 2: ***Working on Trigger LeadCompetitor on Opportunity ***
                        What is the datatype field on Leading_Competitor__c ?
                        The Leading Competitor field unable to collect the data of                                    Competitor lowest Price

trigger LeadingCompetitor on Opportunity (before insert,before update) {

for(Opportunity opp:Trigger.new){
    //Add all our prices in a list in order of competitor
    List<Decimal> competitorPrices =new List<Decimal>();
    
        competitorPrices.add(opp.Competitor_1_Price__c);
        competitorPrices.add(opp.Competitor_2_Price__c);
        competitorPrices.add(opp.Competitor_3_Price__c);
       
    //Add all our comptitors in a list in order
    List<String> competitors =new List<String>();
         competitors.add(opp.ven__Competitor_1__c);
         competitors.add(opp.ven__Competitor_1__c);
         competitors.add(opp.ven__Competitor_1__c);
 
  //Loop through alll competitors to find the position of the lowest price
   Decimal lowestPrice;
   Integer lowestPricePosition;
   
    for(Integer i=0;i<competitorPrices.size();i++){
        Decimal currentPrice =competitorPrices.get(i);
        
        if(lowestPrice==null || currentPrice<lowestPrice){
           lowestPrice=currentPrice;
           lowestPricePosition=i;
           }
           }
           //Populate the leading competitor field with the competitor matching the lowest price position
           // opp.Leading_Competitor__c=competitors.get(lowestPricePosition);
      } 
Write a trigger that creates two identical Opportunities when ever an Account is created.Make sure both opportunities are associates with the Account.
Use any values for the fields on the opportunities
Just make sure to use variables when populating the fields of each opportunity to make sure they are identical.
 
/*Write a trigger that automatically changes a Contact’s email address to “hello@world.com” whenever a Contact is created. Verify it works!
*/
trigger ContactEmail on Contact(before insert){
   
 for(Contact con:Trigger.new){
        
 con.Email='hello@world.com';
 
  
}
}

@isTest
private class ContactEmailTest {

    @isTest static void createContact() {
        
// Implement test code
      
    
           Contact con = new Contact(
  
                    FirstName='fname',
 
                    LastName ='lname',
 
                    Email = 'email@gmail.com',
  
                    Phone = '9743800309');
               
                         insert con;

}
}