• DDS
  • NEWBIE
  • 0 Points
  • Member since 2009

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

Hi everyone!

 

I have three objects that are linked together (Account, Project__c, Space__c). Both the Project and the Space have a lookup to an Account and the Project looks up to a Space. What I want to do is when I populate the Account on the Project record it will update the related Space with that same Account.

 

Here's my code:

 

 

trigger PopulateCurrentTenant on Project__c (after insert, after update) {
    Set<id> tenantIds = new Set<id>();
    Set<id> spaceIds = new Set<id>();
    for (Project__c p : Trigger.new){
        tenantIds.add(p.Account__c);
        spaceIds.add(p.Space__c);   
    }
    // query for all the tenant records for the unique tenantIds in the records
    // create a map for a lookup / hash table for the user info
    List<Account> tenants = [Select Id, Name from Account Where Id in :tenantIds];
    Map<id, Account> tenantsMap = new Map<id, Account>();
    for(Account a : tenants){
    tenantsMap.put(a.Id,a);
  }  
 	
 	List<Space__c> spaces = [Select Id, Name, Account__c from Space__c Where Id in :spaceIds];
 	Map<id, Space__c> spacesMap = new Map<id, Space__c> ();
 	for(Space__c s : spaces){
 		spacesMap.put(s.Id,s);
 	}
    // iterate over the list of records being processed in the trigger and
    // set the Existing Tenant on the Space after being inserted or updated
    for (Project__c p : Trigger.new){
    	Space__c thisSpace = spacesMap.get(p.Space__c);
        Account thisTenant = tenantsMap.get (p.Account__c);
thisSpace.Account__c = thisTenant;
} }

 

 

The code deploys fine but there's only one issue: it doesn't do a thing; nothing happens when I edit a project. it just doesnt update the related space with the account.

 

Are the maps empty? Am I querying the data incorrectly?

 

Thank you in advance for your help!

 

 

  • September 15, 2010
  • Like
  • 0

I am receiving the error:

 

Developer script exception from Company : CreateIndividualAccount : CreateIndividualAccount: execution of BeforeInsert caused by: System.Exception: Too many DML statements: 21

 

and I understand why but I'm just having a hard time figuring out how to fix it.

 
The way the trigger works, it checks if the contact has an account, if it

doesnt it creates an account for it and associates the contact to that new
account. The problem is that it's doing this for each account everytime instead
of grouping all these accounts in a list and then updating all the contacts with
the new accounts in bulk. I'm not a versed enough programmer to 

 

 

trigger CreateIndividualAccount on Contact (before insert, before update) {
RecordType arec = [SELECT ID from RecordType WHERE SobjectType='Account' AND Name='Individual' limit 1];
for (Contact c : trigger.new)
{
if(c.AccountID == null)
{
Account[] acc = new Account[0];
string cName = (c.FirstName + ' ' + c.LastName);
acc.add(new Account (Name = cName, RecordTypeId = arec.Id));

insert acc;
c.AccountID = acc[0].Id;
}
}
}

 

  

Is there a way to go arround this?

 

 

Thanks in advance for you help.

  • January 12, 2010
  • Like
  • 0

Hi guys,

 

I'm a beginner apex coder and I just wrote a small trigger to automatically create an account when you don't assign one to a new contact. It basically creates an individual account with the same name as the contact and then associates that account to the contact. Here's the code:

 

 

trigger CreateIndividualAccount on Contact (before insert, before update) {
Account[] acc = new Account[0];
RecordType arec = [SELECT ID from RecordType WHERE SobjectType='Account' AND Name='Individual' limit 1];
for (Contact c : trigger.new)
{
if(c.AccountID == null)
{
string cName = (c.FirstName + ' ' + c.LastName);
acc.add(new Account (Name = cName, RecordTypeId = arec.Id));

insert acc;
c.AccountID = acc[0].Id;
}
}
}

 

I tried importing contacts in my dev org and it works fine, but when I do it in production under a managed package it returns the error " INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call "

 

Does anyone know what I'm doing wrong? Again, these are some of the first lines of code I've ever written so it might be pretty obvious.

 

Thanks in advance.

 

 

Note: The account record type "Individual" does exist and is part of the managed package. Would i have to add the namespace prefix to the record type name?

 

  • December 22, 2009
  • Like
  • 0

Hi everyone!

 

I have three objects that are linked together (Account, Project__c, Space__c). Both the Project and the Space have a lookup to an Account and the Project looks up to a Space. What I want to do is when I populate the Account on the Project record it will update the related Space with that same Account.

 

Here's my code:

 

 

trigger PopulateCurrentTenant on Project__c (after insert, after update) {
    Set<id> tenantIds = new Set<id>();
    Set<id> spaceIds = new Set<id>();
    for (Project__c p : Trigger.new){
        tenantIds.add(p.Account__c);
        spaceIds.add(p.Space__c);   
    }
    // query for all the tenant records for the unique tenantIds in the records
    // create a map for a lookup / hash table for the user info
    List<Account> tenants = [Select Id, Name from Account Where Id in :tenantIds];
    Map<id, Account> tenantsMap = new Map<id, Account>();
    for(Account a : tenants){
    tenantsMap.put(a.Id,a);
  }  
 	
 	List<Space__c> spaces = [Select Id, Name, Account__c from Space__c Where Id in :spaceIds];
 	Map<id, Space__c> spacesMap = new Map<id, Space__c> ();
 	for(Space__c s : spaces){
 		spacesMap.put(s.Id,s);
 	}
    // iterate over the list of records being processed in the trigger and
    // set the Existing Tenant on the Space after being inserted or updated
    for (Project__c p : Trigger.new){
    	Space__c thisSpace = spacesMap.get(p.Space__c);
        Account thisTenant = tenantsMap.get (p.Account__c);
thisSpace.Account__c = thisTenant;
} }

 

 

The code deploys fine but there's only one issue: it doesn't do a thing; nothing happens when I edit a project. it just doesnt update the related space with the account.

 

Are the maps empty? Am I querying the data incorrectly?

 

Thank you in advance for your help!

 

 

  • September 15, 2010
  • Like
  • 0

Trying to solve one last problem on a trigger I am working on. Currently, the trigger works fine on 'insert' and 'delete', but generating the following error message when 'updating':

 

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger rollupCaseHoursTo360 caused an unexpected exception, contact your administrator: rollupCaseHoursTo360: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.rollupCaseHoursTo360: line 91, column 44

 

The problem occurs when one of our particular fields is NULL; which is the case most of the time.  So the update trigger needs to test to make sure the value is not NULL before executing the rest of the code.  Below is a copy of the update section of trigger and comments of how I tried to test for the NULL value and section generating error:

 

 

.
.
.
else if(Trigger.isUpdate)
{
	//Tried adding test to verify value was not null before executing rest of code
	if (Case.X360_Contract_Cycle__c != NULL) {
	
	//sum total both old and new
	Case [] oldTime = Trigger.old;
	Case [] newTime = Trigger.new;
	Double newSum = 0.0;
	Double oldSum = 0.0;

	for(Case newTe: newTime)
		{
			for(Case oldTe : oldTime)
				{

// SECTION GENERATING ERROR IF 'X360_Contract_Cycle__c' IS NULL
X360_Contract_Cycle__c oldTimesheet = [Select Id, Name, Delivered_Hours__c from X360_Contract_Cycle__c where Id = :oldTe.X360_Contract_Cycle__c];

X360_Contract_Cycle__c newTimesheet = [Select Id, Name, Delivered_Hours__c from X360_Contract_Cycle__c where Id = :newTe.X360_Contract_Cycle__c];
					
					
					Case [] newSumHours = [Select Id, Case_Hours__c  from Case where X360_Contract_Cycle__c = :newTimesheet.Id];
					Case [] oldSumHours = [Select Id, Case_Hours__c from Case where X360_Contract_Cycle__c = :oldTimesheet.Id];

							//sum premiums from child objects
							for(Case oldSumHour : oldSumHours)
							{
								//converting 'Case_Hours__c' from string data type to number (double)
    							double oldCaseHours = double.valueOf(oldSumHour.Case_Hours__c);
								oldSum += oldCaseHours;
							}

							for(Case newSumHour : newSumHours)
							{
								//converting 'Case_Hours__c' from string data type to number (double)
    							double newCaseHours = double.valueOf(newSumHour.Case_Hours__c);
								newSum += newCaseHours;
							}

					newTimesheet.Delivered_Hours__c = newSum;
					oldTimesheet.Delivered_Hours__c = oldSum;

					//sheetsToUpdate.add(newTimesheet);
					//sheetsToUpdate.add(oldTimesheet);

					sheetsToUpdate.add(newTimesheet);
					if(newTimesheet.Id != oldTimesheet.Id){
						sheetsToUpdate.add(oldTimesheet);
					}

				}
			}


update sheetsToUpdate;

}

}

 

I am far from a full fledged Developer so trying to hack my way through this a bit.  Logically I thought the IF test would work, but the rest of the code appears to execute regardless if the value is NULL or not.  If the value is not NULL the update works fine.  Any suggestions?  Thanks!!

 

Hi All,

 

For the first time using the change sets to deploy from sandbox to production org.Im deploying custom fields,objects and tabs.Though im the platform administrator in both sandbox & prod,after deployment all fields\tabs are hidden in the prod org.I had to manually grant access to each of the field.

Please let me know if there is any additional configuration required before creating the change sets.

 

 

Thanks

SC

 

I am receiving the error:

 

Developer script exception from Company : CreateIndividualAccount : CreateIndividualAccount: execution of BeforeInsert caused by: System.Exception: Too many DML statements: 21

 

and I understand why but I'm just having a hard time figuring out how to fix it.

 
The way the trigger works, it checks if the contact has an account, if it

doesnt it creates an account for it and associates the contact to that new
account. The problem is that it's doing this for each account everytime instead
of grouping all these accounts in a list and then updating all the contacts with
the new accounts in bulk. I'm not a versed enough programmer to 

 

 

trigger CreateIndividualAccount on Contact (before insert, before update) {
RecordType arec = [SELECT ID from RecordType WHERE SobjectType='Account' AND Name='Individual' limit 1];
for (Contact c : trigger.new)
{
if(c.AccountID == null)
{
Account[] acc = new Account[0];
string cName = (c.FirstName + ' ' + c.LastName);
acc.add(new Account (Name = cName, RecordTypeId = arec.Id));

insert acc;
c.AccountID = acc[0].Id;
}
}
}

 

  

Is there a way to go arround this?

 

 

Thanks in advance for you help.

  • January 12, 2010
  • Like
  • 0

Hi guys,

 

I'm a beginner apex coder and I just wrote a small trigger to automatically create an account when you don't assign one to a new contact. It basically creates an individual account with the same name as the contact and then associates that account to the contact. Here's the code:

 

 

trigger CreateIndividualAccount on Contact (before insert, before update) {
Account[] acc = new Account[0];
RecordType arec = [SELECT ID from RecordType WHERE SobjectType='Account' AND Name='Individual' limit 1];
for (Contact c : trigger.new)
{
if(c.AccountID == null)
{
string cName = (c.FirstName + ' ' + c.LastName);
acc.add(new Account (Name = cName, RecordTypeId = arec.Id));

insert acc;
c.AccountID = acc[0].Id;
}
}
}

 

I tried importing contacts in my dev org and it works fine, but when I do it in production under a managed package it returns the error " INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call "

 

Does anyone know what I'm doing wrong? Again, these are some of the first lines of code I've ever written so it might be pretty obvious.

 

Thanks in advance.

 

 

Note: The account record type "Individual" does exist and is part of the managed package. Would i have to add the namespace prefix to the record type name?

 

  • December 22, 2009
  • Like
  • 0

Goal: use a trigger to update a custom field on the Task (OppNumber) with a field on the Opportunity that is selected in the WhatID field (if an Opp is selected)

 

I thought this would be easy to do using a lookup and just passing the value, but you apparently cannot create a lookup on the Activity/Task object. 

 

I know I can pull in the record ID for the opp related via the WhatID field, but how can I refer to a specific field on that record? (Such as the Opportunity Number).

 

Open to suggestions.

 

 

Hi, I recently switched over from VerticalResponse's web-to-lead procedure and I'm trying to emulate some of its convenience and simplicity. (I am using the Nonprofit Starter Kit.) I have been through the support/help files and am empty-handed, so here I am.

 

1) Is a way to either automatically convert every lead into a contact, or batch convert leads, without creating a task or logging a donation? We have a designation for people who contact us, which is simply "Interested." I'd like to make it so that everyone who signed up through the site was automatically a contact marked Interested with something in their Description field like, "Web-to-lead signup." I have seen some help files about mapping Lead fields into Contact fields, but nothing about automatically converting them.

 

2) I would like to send an email to the administrator when a new web-to-lead is created. This, I think, is fairly easy and is accomplished from sending web-to-leads in to a queue and notifying the owner of the queue.

 

Clearly, most of my attention is focused on #1 above, to simplify the process of gathering leads and contact information from the website. Is there a trigger or something I can set up to accomplish this? I appreciate any help.Ryan