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

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
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?

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?