Compile error when trying to make rollup apex trigger - triggers

Been amending a piece of code to suit my needs but it won't compile. My naive eyes cannot see the error of my ways:
trigger doRollup on Time_Record__c (after insert, after update, after delete, after undelete) {
// List of parent record ids to update
Set<Id> parentIds = new Set<Id>();
// In-memory copy of parent records
Map<Id,Time_Record__c> parentRecords = new Map<Id,Time_Record__c>();
// Gather the list of ID values to query on
for(Daily_Time_Record__c c:Trigger.isDelete?Trigger.old:Trigger.new)
parentIds.add(c.Time_Record_Link__c);
// Avoid null ID values
parentIds.remove(null);
// Create in-memory copy of parents
for(Id parentId:parentIds)
parentRecords.put(parentId,new Time_Record__c(Id=parentId,RollupTarget__c=0));
// Query all children for all parents, update Rollup Field value
for(Daily_Time_Record__c c:[select id,FieldToRoll__c,Time_Record_Link__c from Daily_Time_Record__c where id in :parentIds])
parentRecords.get(c.Time_Record_Link__c).RollupTarget__c += c.FieldToRoll__c;
// Commit changes to the database
Database.update(parentRecords.values());
}
Where:
Time_Record__c is my parent
RollUpTarget__c is the place I am trying to roll to
Daily_Time_Record__c is the child
Time_Record_Link__c is the link to parent that exists on the child
FieldToRoll__c is a test field to roll up on the child
I think that is what I had to replace from this generic template:
trigger doRollup on Child__c (after insert, after update, after delete, after undelete) {
// List of parent record ids to update
Set<Id> parentIds = new Set<Id>();
// In-memory copy of parent records
Map<Id,Parent__c> parentRecords = new Map<Id,Parent__c>();
// Gather the list of ID values to query on
for(Child__c c:Trigger.isDelete?Trigger.old:Trigger.new)
parentIds.add(c.ParentField__c);
// Avoid null ID values
parentIds.remove(null);
// Create in-memory copy of parents
for(Id parentId:parentIds)
parentRecords.put(parentId,new Parent__c(Id=parentId,RollupField__c=0));
// Query all children for all parents, update Rollup Field value
for(Child__c c:[select id,Amount__c,ParentField__c from Child__c where id in :parentIds])
parentRecords.get(c.ParentField__c).RollupField__c += c.Amount__c;
// Commit changes to the database
Database.update(parentRecords.values());
}
Ideally I want to roll up a sum of 7 fields from the child (hence needing this solution), but starting small.

Related

Trigger to prevent User From Adding more than one product to an opportunity

So i have written a trigger to prevent user from entering more than one opportunity product to the same opportunity, but the problem is when he adds more than one opportunity product at the same time, my trigger does not fire, salesforce takes it as one product.
What can i add to my trigger to fix this ?
My trigger :
trigger OpportunityLineItemBeforeInsert on OpportunityLineItem (before insert) {
Set<Id>opportunityIds = new Set<Id>();
// get all parent IDs
for(OpportunityLineItem i : trigger.new)
{
opportunityIds.add(i.OpportunityId);
}
// query for related Olis (Opportunity Line Items)
Map<Id, Opportunity> opps = new Map<Id, Opportunity>([SELECT ID,
(SELECT ID
FROM OpportunityLineItems)
FROM Opportunity
WHERE ID IN :opportunityIds]);
for(OpportunityLineItem i : trigger.new)
{
if(opps.get(i.OpportunityId).OpportunityLineItems.size()>0)
{
i.addError('Your Message');
}
}
}
Thank you in advance.
I would probably ignore anything related to the Oppty.
You want only one product created, so on creation, either the number of LI is 0 and you can create exactly one, or it' snot 0 and you can't create any.
I would just create a rollup field on the Oppty, count the products. If the count != 0, then fail the validation. If count = 0, then count the Olis in trigger.new and if !=1, fail.
Instead of writing code to do this you should instead create a field on products that stores the id of the parent opportunity, make that field unique, and populate the value via workflow or process builder with the id of the parent opportunity. That way if a second product gets added the unique constraint would fire and prevent the record from being inserted.
Another option would be to create a rollup on opportunity to count the number of opportunity products, then add a validation rule that show an error if the number of products > 1. The advantage of doing it this way is that you get to set the error message as opposed to the generic duplicate error message with the first option.

Apex code to update new Opportunity with values from related object

What would be the proper method to update a list of new Opportunities with the values from a related record.
for (Opportunity opps:Trigger.new){
[SELECT Id, CorpOwner__r, Contact__r,(SELECT Id, AccountLocation from Account)]
o.CorpOwner__r =Account.Id; o.AccountLocation = opps.Account.AccountLocation;
insert opps
Do you call the lookup fields by the __r suffix? Could you do a before insert operation and still look up the Opportunity.CorpOwner__r relationship to values in the CorpOwner__r Account record, or does that relationship not exist since the record has not been created? What would be a proper batchified way to go about it?
Here's a possibility that demonstrates a number of concepts:
trigger UpdateOpptyWithAccountInfo on Opportunity (before insert) {
// Keep this SOQL query out of the FOR loop for better efficiency/batching
Map<Id, Account> relatedAccounts = new Map<Id, Account>(
[SELECT Id, AccountLocation__c
FROM Account
WHERE Id IN
(SELECT AccountId
FROM Opportunity
WHERE Id = :Trigger.new)
]
);
for (Opportunity o : Trigger.new) {
/* Find each opportunity's Account in the map we queried for earlier
* Note: there's probably a more efficient way to use a Map of Opportunity IDs to Account objects...
* This works fine and could be more readable.
*/
for (Account a : relatedAccounts.values()) {
if (a.Id == o.AccountId) {
// Once you've found the account related to this opportunity, update the values
o.CorpOwner__c = a.Id;
o.AccountLocation__c = a.AccountLocation__c;
}
}
}
// We're still inside an `insert` trigger, so no need to call `insert` again.
// The new fields will be inserted along with everything else.
}
If you're establishing the relationship between objects, use the __c suffix:
o.CorpOwner__c = a.Id; // Associate Account `a` as Opportunity `o`'s CorpOwner
If you're looking up a field on a related object, then you would use __r:
System.debug(o.CorpOwner__r.Name); // Print Opportunity `o`'s CorpOwner's name

Entity framework getting IDs for multiple insert in foreach

When we insert multiple rows at the same time with Entity framework it's good to add these objects one by one and then commit in the end, instead of adding and comitting each time.
So in that case, how do I get IDs for those insertions?
Example:
foreach (var item in list)
{
Subscription subscription = new Subscription();
subscription.Amount = item.ItemTotal;
this.ClientRepositories.LiveData.AddToSubscriptions(subscription);
// LiveData is db context via webservice proxy
// how to get IDS of these insertions if you do not commit each time?
int id = subscription.Id;
someOtherOperation(id); //i need to insert ID for each row that was inserted
}
this.ClientRepositories.LiveData.SaveChanges();
If I use commit inside for each time it adds new object I can easily get the latest inserted ID, but its not good to commit each time for multiple rows I heard.
I'm assuming that you are using auto incremented id's. I think your going to have to keep a list of your subscription objects outside of your loop. During each loop iteration, add the current subscription to the list. After your execute 'SaveChanges();' The subscription id's will be automatically added.
Would look like this:
List<Subscription> subList = new List<Subscription>();
foreach (var item in list)
{
Subscription subscription = new Subscription();
subscription.Amount = item.ItemTotal;
this.ClientRepositories.LiveData.AddToSubscriptions(subscription);
subList.add(subscription);
}
this.ClientRepositories.LiveData.SaveChanges();
/* At this point, each Subscription in your 'subList' should have an id */

Creating a Trigger that will create a new Opportunity Owner every time a there is a new Opportunity in Salesforce

I'm new to Salesforce and I'm trying to create a trigger that will basically update fields and create a new Opportunity owner every time a new Opportunity gets added.
For clarity, I've attached my code below:
trigger trig_Opportunity_CreateOppOwner on Opportunity (before insert, before update) {
//Opportunity OppOwner = null;
List<id>OppsID = new List<id>(); //Get the id of all new Opportunities owners
for (Opportunity Opp : Trigger.new) { //If a new Opportunity is added, then create new OppOwner, if not, then don't add.
OppsId.add(Opp.ID); //adds all new Opportunities Id's
}
List<Opportunity>OppToUpdate = [SELECT Id,
Name,
Owner__c,
OppOwner,
FROM Opportunity
WHERE Id IN: Opp.ID // Select Id, OpportunityName,
];
if Trigger.oldMap.get(opp.id).Owner__c != Trigger.oldMap.get(OppToUpdate.id).Owner__c // verify that if previous Opportunity has a matching owner.
OppsId.add(Opp.ID); //populates new oppowner with ID's of all owners.
This is basically what I'm trying to do:
Trigger(Before Update, Before Insert){
Get all Triggered Opportunities.
Verify if old opportunities already has a matching Owner.
If it's not a matching owner, update Opportunity fields and update the opportunity.
I'm not sure how to get from step 2 to step 3. Any help would be appreciated.
By the code that you provided isn't clear where it's finished, from my side seems that after couples of lines that you've provided another couples of lines exists. Could you please post all your code? If it isn't so, and code that you posted is all your code, from my point of view, this code do nothing, absolutely nothing.
Why?
no validation's errors
no DML operation
trigger trig_Opportunity_CreateOppOwner on Opportunity (before insert, before update) {
//Opportunity OppOwner = null;
/*
List<id>OppsID = new List<id>(); //Get the id of all new Opportunities owners
for (Opportunity Opp : Trigger.new) { //If a new Opportunity is added, then create new OppOwner, if not, then don't add.
OppsId.add(Opp.ID); //adds all new Opportunities Id's
}
*/
// 3 started lines might be replaced by the following one
List<id>OppsID = Trigger.newMap.getKeys();
//but the following code perform select on Opportunity object and return the same list as Trigger.new
// OppToUpdate == Trigger.new
// What for? May be you should work with this part on "after insert/update"
List<Opportunity>OppToUpdate = [SELECT Id,
Name,
Owner__c,
OppOwner,
FROM Opportunity
//WHERE Id IN: Opp.ID // Opp - isn't exist and it isn't list/set of id
WHERE Id IN OppsId // I guess you meant this
];
// 1.variable "opp" isn't exist here
// 2. "OppToUpdate.id" - you can't use list in this manner
if Trigger.oldMap.get(opp.id).Owner__c != Trigger.oldMap.get(OppToUpdate.id).Owner__c // verify that if previous Opportunity has a matching owner.
OppsId.add(Opp.ID); //populates new oppowner with ID's of all owners.

Prevent duplicate values on bulk insert (Salesforce)

I need a trigger that receives data from users (Bulk load of about 1000 records) and store them in a Salesforce database. The problem is that users can show up more than once in Trigger.new or even on a different batch. The custom object name is CBK_User and has an EXTERNAL_ID (unique) called USER_ID. In my code I check that the users does not yet exist in the database:
Map<String, CBK_User__c> users = new Map<String,CBK_User__c>
([select Id, USER_ID__c from CBK_User__c where USER_ID__c in : userIds]);
(userIds has the external ids of the Trigger.new objects)
When a I try to insert, it gives me the error:
DUPLICATE_VALUE, duplicate value found: USER_ID__c duplicates value on
record with id: a1QJ0000000HRd8"
How do I prevent duplicate values on bulk insert?
I've adapted your problem to this basic example (Exercise 2: Lead duplicate prevention)
You should clean first the "new" list from duplicated entries, and then clean from existing in db.
trigger CBK_UserDuplicatePreventer on CBK_User__c (before insert, before update) {
//Enter a map declaration to hold records which we will add,
// this will become a unique map, no duplicate values within it.
Map<String, CBK_User__c> cbkUserMap = new Map<String, CBK_User__c>();
//The next few lines loop across the array of records that are passed into
//the trigger in bulk fashion from any API or User Interface database operation.
//The goal of this loop is to ensure that there are no duplicates within
//the batch that we have received and to gather a list of externalIds that we will use later
for (CBK_User__c cbkUser : System.Trigger.new) {
/* Make sure we don't treat an externalId that
isn't changing during an update as a duplicate. */
if ((cbkUser.USER_ID__c != null) && (System.Trigger.isInsert ||
(cbkUser.USER_ID__c != System.Trigger.oldMap.get(cbkUser.Id).USER_ID__c))) {
// Make sure another new CBK_User__c isn't also a duplicate
if (cbkUserMap.containsKey(cbkUser.USER_ID__c)) {
cbkUser.USER_ID__c.addError('Another new CBK_User__c has the same USER_ID.');
} else {
cbkUserMap.put(cbkUser.USER_ID__c, cbkUser);
}
}
}
// Using a single database query, find all the CBK_User__c in
// the database that have the same USER_ID as ANY
// of the CBK_User__c being inserted or updated. */
for (CBK_User__c cbkUser : [SELECT USER_ID__c FROM CBK_User__c WHERE USER_ID__c IN :cbkUserMap.KeySet()]) {
CBK_User__c newCbkUser = cbkUserMap.get(cbkUser.USER_ID__c);
newCbkUser.USER_ID__c.addError('A CBK_User__c with this USER_ID already exists.');
}
}