• smohyee
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies

Hi all, here's a simple trigger I made to update Start Date and End Date fields based on whether a new user was created or deactivated:

 

// For manual additions and deactivations of users. Adds current day as Start Date for new employees, and as End Date for deactivated employees

trigger User_SetEmploymentDates on User (before insert, before update) {

    for (User u: Trigger.New){
        
    	if (Trigger.isInsert && u.Start_Date__c == NULL){
        	u.Start_Date__c = Date.Today();
            
        }
        
        else if (Trigger.isUpdate && u.End_Date__c == NULL && u.IsActive == False){
        	u.End_Date__c = Date.Today();            
        }
    
    }
    
 
}

My test methods show that the Start Date and End Date fields remain with NULL values, which I realize is due to me not updating the database with the new field values (I'm only assigning to u, not sticking u in the dB).  

 

 

 

I used to have an "update u;" or "insert u;" after each field value assignment,  but I couldn't keep it due to the test methods returning an error:

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, User_SetEmploymentDates: execution of BeforeInsert

caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old

 

I also had the trigger working on after update and insert, which apparently just can't be done, because it says the record is read only.

 

 

 

I don't have a class associated with this trigger, because the field update is so simple. Is this where I'm going wrong?

 

If I understand Dynamic SOQL, it basically allows you to use variables in your query. However, it also requires a call to the database class (ie database.query("query here")).

 

I'm trying to use SOQL results in a custom Javascript button, and the only SOQL query method I know of is

 

sforce.connection.query("query here")

 

From my testing it doesn't look like I can use variables within the query. Therefore, when I query ContactID from the OpportunityContactRole object:

 

var myquery = "SELECT ContactId FROM OpportunityContactRole WHERE OpportunityId = '{!Opportunity.Id}' AND IsPrimary = True limit 1";

 

var result = sforce.connection.query(myquery);
var records = result.getArray("records");

 


I know I can access the contactid using records[0].ContactId. The issue I have is when I want to use this variable in another query to pull information from the contact object:

 

var contact_email = sforce.connection.query("SELECT Email FROM Contacts WHERE Id = records[0].ContactId");

 

 

Whether I use records[0].ContactId, or assign its value to a string var, or anything similar, I get a MALFORMED QUERY error, saying it doesn't recognize the token (the var I'm using for the ID filter). 

 

Anyone know how I can solve this in the context of Javascript? I'm not familiar enough with Dynamic SOQL to know how to call it within a javascript button. Thanks for your help!

Hi all. I'm trying to piece together a javascript button without actually knowing JS, and I feel like I'm close, short of some syntax errors. Greatly appreciate any help.

The goal here is to have the button give an error message if a field is missing from the buttons page, and otherwise to go to a standard url redirect. 

My current code:
 
{!REQUIRESCRIPT("/soap/ajax/23.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/23.0/apex.js")}
 
IF (ISBLANK({!Lead.Email}){
alert ("Lead must have an email to schedule demo");
}
else {
 
 
}

 

 

If I understand Dynamic SOQL, it basically allows you to use variables in your query. However, it also requires a call to the database class (ie database.query("query here")).

 

I'm trying to use SOQL results in a custom Javascript button, and the only SOQL query method I know of is

 

sforce.connection.query("query here")

 

From my testing it doesn't look like I can use variables within the query. Therefore, when I query ContactID from the OpportunityContactRole object:

 

var myquery = "SELECT ContactId FROM OpportunityContactRole WHERE OpportunityId = '{!Opportunity.Id}' AND IsPrimary = True limit 1";

 

var result = sforce.connection.query(myquery);
var records = result.getArray("records");

 


I know I can access the contactid using records[0].ContactId. The issue I have is when I want to use this variable in another query to pull information from the contact object:

 

var contact_email = sforce.connection.query("SELECT Email FROM Contacts WHERE Id = records[0].ContactId");

 

 

Whether I use records[0].ContactId, or assign its value to a string var, or anything similar, I get a MALFORMED QUERY error, saying it doesn't recognize the token (the var I'm using for the ID filter). 

 

Anyone know how I can solve this in the context of Javascript? I'm not familiar enough with Dynamic SOQL to know how to call it within a javascript button. Thanks for your help!

I have a standard button which is already designed by making use of javascript.

 

Right now when i click on the button its getting opened in the Same page.

 

My req is that when i click on the   button it should redirect to the new tab.

 

Please help me how can i get the id of the button so that i can proceed further.

 

Thanks in Advance.

  • May 29, 2013
  • Like
  • 0