• Adam Nila
  • NEWBIE
  • 40 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 2
    Replies
Ok, so I have a few questions about this specific challenge... First one of the requirements states:
"the Report Date field reflects the day the request was created, and the Due Date is calculated from the Report Date.".  Which request?  The original request or the new request?

Second, when checking the challenge I get the following notice:

"The challenge is expecting to find the closed Maintenance Request plus an 'New' Maintenance Request of type 'Routine Maintenance' with the same Vehicle as the closed one."

I have checked this trigger via the UI and it meets this requirement.  The new maintenance request does in fact show the same vehicle as the closed request, so why would I be getting this error?

 
for(Case C : triggerNew){
        	Case newCase = triggerNewMap.get(C.Id);
            Case oldCase = triggerOldMap.get(C.Id);
            if(newcase.status.contains('Closed') && !oldCase.status.contains('Closed') &&(newCase.recordTypeId=='012360000011FOeAAM' || newCase.recordTypeId =='012360000011FOjAAM')){
                for(Product2 prod : prodCodes){
                    
                    if(newCase.Product__c == prod.productCode){
                        prodExpiration = prod.lifespan_Months__c;
                    }
                }
                Decimal daysTilDue = prodExpiration * 30;
                Case maintenance = new Case(status='New',
                                            origin='Routine Maintenance',
                							//Added a routine maintenance type to the drop down field because I wasn't sure if it meant this 'type' or record type
                                            type='Routine Maintenance',
                                            //Id for 'Routine Maintenance' record type
                                            recordTypeId = '012360000011FOjAAM',
                                            reason= newCase.reason,
                                            subject='Routine Maintenance',
                                            product__c = newCase.Product__c,
                                           vehicle__c = newCase.Vehicle__c);
                maintenance.Date_Reported__c= newCase.CreatedDate.date();
                maintenance.Date_Due__c = maintenance.Date_Reported__c + daysTilDue.intValue();
                
                if(newCase.ContactId != null){
                    maintenance.ContactId = newCase.ContactId;
                }
                followUpCases.add(maintenance);
            }
        }//end of triggerNew for-loop
			insert followUpCases;

 
Requirement: Another aspect about parts is they all have different lifespans. Therefore, you need to calculate and set the next service due date to the end of the maintenance cycle defined on the related equipment record.

Relevant Code:
Public Static List<Product2> prodCodes = [SELECT Id, productCode, lifespan_months__c FROM Product2];

Public Static Decimal prodExpiration; 

for(Product2 prod : prodCodes){
                    
                    if(newCase.Product__c == prod.productCode){
                        system.debug(newCase.Product__c);
                    	system.debug(prod.productCode);
                        prodExpiration = prod.lifespan_Months__c;
                        system.debug(prodExpiration);
                        system.debug(prod.Lifespan_Months__c);
                    }
                }

                //This line is throwing a null pointer error according to salesforce
                Decimal daysTilDue = prodExpiration * 30;

                Date_Due__c = date.today()+ daysTilDue.intValue(),

The system debug confirms that it finds matching Product codes, but shows both prodExpiration and prod.lifespan_months__c as being null.  Should the direct field reference show up at a minimum?  And is there a better way to go about this?
 
I'm working on a trigger that will run before update or before insert.  As the title suggests, I want the trigger to add the account name to the opportunity name but only if the opportunity name is not already present.  The before insert part is easy since lazy sales reps are already used to this being done for them.  But if/when they update an opportunity name, I don't want the account name added a second time.  Here is what I've got so far:
 
trigger OppNameTrigger on Opportunity (before insert, before update) {
    Set<Id> AccountIds = new Set<Id>(); //Initialize a list of Ids
	String sName; //Initialize a string variable to manipulate our Opp names
    String accName;//Initialize a string variable to hold account name value when opp is updated instead of inserted
    
    //Add an account ID to the list for every opportunity caught by trigger
    for (Opportunity op : Trigger.new) {
  
 	  accountIds.add(op.AccountId);
	}
    
//Create a map of Ids and account, querying the name from account records
//whose Id is in the AccountIds list generated earlier
Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Name FROM Account WHERE Id IN :accountIds]);
    System.debug(accountMap);

//Loop through all opps setting the new name in a variable and then assigning that
//variables contents to the name field for each opportunity
for (Opportunity op : Trigger.new) {
    //Assigns account name of current op to a variable
    
    accName = accountMap.get(op.AccountId).Name;
    if(trigger.isInsert){
  		// add the account name and amount to current opp name
  		sName = accountMap.get(op.AccountId).Name + ' - ' + op.Name;

  		// set the name :-)
  		op.Name = sName;
    }else {
        if(trigger.oldmap.get(op.Name) != trigger.newmap.get(op.name)){
            if(!op.name.contains(accountMap.get(op.AccountId).Name))
                
                sName = accName + ' - ' + op.Name;
            	op.Name = sName;
        }
    }
    }
}
When I try to edit the opportunity, I get the following error, "Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger OppNameTrigger caused an unexpected exception, contact your administrator: OppNameTrigger: execution of BeforeUpdate caused by: System.StringException: Invalid id: Testing Testing Opps: External entry point".  Anybody have any thoughts on what i'm doing wrong?
 

What I want to do is pull a list of events scheduled for today but only list the events relevant to the active user.... Here's what I have so far...

 

public class QuickLogController {
    public String username{get;set;}
    
    public string finduser(){
		
        username = Userinfo.getUserName();
        return username;
    }  
    
    public list<event> getEventsToday(){
       
       List<event> TodaysEvents = Database.query (
           'SELECT Who.Name, Owner.id, Subject, StartDateTime, Location FROM event WHERE StartDateTime = TODAY AND Owner.Name = :username ');
        return TodaysEvents;
        	        
    }
    
}

When I run this without "AND Owner.Name = :username '" it works fine.  When I test it in the query editor and replace ":username" with my name, it works fine.  Not sure what I'm doing wrong here?
Ok, so I have a few questions about this specific challenge... First one of the requirements states:
"the Report Date field reflects the day the request was created, and the Due Date is calculated from the Report Date.".  Which request?  The original request or the new request?

Second, when checking the challenge I get the following notice:

"The challenge is expecting to find the closed Maintenance Request plus an 'New' Maintenance Request of type 'Routine Maintenance' with the same Vehicle as the closed one."

I have checked this trigger via the UI and it meets this requirement.  The new maintenance request does in fact show the same vehicle as the closed request, so why would I be getting this error?

 
for(Case C : triggerNew){
        	Case newCase = triggerNewMap.get(C.Id);
            Case oldCase = triggerOldMap.get(C.Id);
            if(newcase.status.contains('Closed') && !oldCase.status.contains('Closed') &&(newCase.recordTypeId=='012360000011FOeAAM' || newCase.recordTypeId =='012360000011FOjAAM')){
                for(Product2 prod : prodCodes){
                    
                    if(newCase.Product__c == prod.productCode){
                        prodExpiration = prod.lifespan_Months__c;
                    }
                }
                Decimal daysTilDue = prodExpiration * 30;
                Case maintenance = new Case(status='New',
                                            origin='Routine Maintenance',
                							//Added a routine maintenance type to the drop down field because I wasn't sure if it meant this 'type' or record type
                                            type='Routine Maintenance',
                                            //Id for 'Routine Maintenance' record type
                                            recordTypeId = '012360000011FOjAAM',
                                            reason= newCase.reason,
                                            subject='Routine Maintenance',
                                            product__c = newCase.Product__c,
                                           vehicle__c = newCase.Vehicle__c);
                maintenance.Date_Reported__c= newCase.CreatedDate.date();
                maintenance.Date_Due__c = maintenance.Date_Reported__c + daysTilDue.intValue();
                
                if(newCase.ContactId != null){
                    maintenance.ContactId = newCase.ContactId;
                }
                followUpCases.add(maintenance);
            }
        }//end of triggerNew for-loop
			insert followUpCases;

 

What I want to do is pull a list of events scheduled for today but only list the events relevant to the active user.... Here's what I have so far...

 

public class QuickLogController {
    public String username{get;set;}
    
    public string finduser(){
		
        username = Userinfo.getUserName();
        return username;
    }  
    
    public list<event> getEventsToday(){
       
       List<event> TodaysEvents = Database.query (
           'SELECT Who.Name, Owner.id, Subject, StartDateTime, Location FROM event WHERE StartDateTime = TODAY AND Owner.Name = :username ');
        return TodaysEvents;
        	        
    }
    
}

When I run this without "AND Owner.Name = :username '" it works fine.  When I test it in the query editor and replace ":username" with my name, it works fine.  Not sure what I'm doing wrong here?