You need to sign in to do that
Don't have an account?
Help With Integer in Apex
Hello:
I have the following code which is designed to insert multiple child recods (Assigned_Address__c) when a parent is created (New_Door_Assigned__c).
What I need to do is after the child records are inserted, update a field on the child record with a value from the parent record, dependent upon what the index/integer value of the row is.
For example, if there are 100 child records, I would want the first 50 to have a Rep__c value = New_Door_Assignment__c.Sales_Rep_One__c, the second 50 to New_Door_Assignment__c.Sales_Rep_Two__c, etc until all child records have a Rep_Name__c value.
The code to create the child records works and I have written test class for it. I am now attempting to add in the logic around this starting with
List<Assigned_Address__c> assigned = new List<Assigned_Address__c>(newAssignment);
assigned.sort(Address__c);
Attached is my full code. Any guidance is appreciated.
Thanks,
Hampton
trigger AddressAssignment on New_Door_Assignment__c (before update) { Set<String> releaseName = new Set<String>(); for (New_Door_Assignment__c newDoor: Trigger.new) { if(newDoor.Assigned__c = TRUE){ {releaseName.add(newDoor.Name);} } Map<String, Address__c> addressMap = new Map<String, Address__c>(); for(Address__c address1 : [Select ID, Name, Unparsed_Address__c, Release__c from Address__c where Release__c in : releaseName]){ addressMap.put(address1.Release__c, address1); List<Assigned_Address__c> newAssignment = new List<Assigned_Address__c>(); for(New_Door_Assignment__c newRelease : trigger.new) { if(addressMap.containsKey(newRelease.Name)) newAssignment.add(new Assigned_Address__c( Address__c = addressMap.get(newRelease.Name).Unparsed_Address__c, New_Door_Assignment__c = newRelease.ID)); Date_Assigned__c = newRelease.Date_Released__c, } List<Assigned_Address__c> assigned = new List<Assigned_Address__c>(newAssignment); assigned.sort(Address__c); List<Integer> int = new List<Integer>(assigned); if(integer i < newRelease.Doors_Per_Rep__c){ assigned.Rep__c = newRelease.Sales_Rep_One__c else { if(integer i < (2*newRelease.Doors_Per_Rep__c)){ assigned.Rep__c = newRelease.Sales_Rep_Two__c else { assigned.Rep__c = newRelease.Sales_Rep_Three__c; } } } update newAssignment; insert newAssignment; } } }
Sorry - one more extra brace causing an issue I believe. Please copy and paste the following and try it again.
trigger RepAssignment on New_Door_Assignment__c (after update) {
Set<String> newDoors = new Set<String>();
for(New_Door_Assignment__c newDoorRep : trigger.new) {
if(newDoorRep.Assigned__c = TRUE) {
(newDoors.add(newDoorRep.ID));
}
}
List<New_Door_Assignment__c> newDoorsList = [Select Id, Doors_Per_Rep__c, Rep_One__c,
Rep_Two__c, Rep_Three__c, Rep_Four__c, Rep_Five__c
from New_Door_Assignment__c
where Id in :newDoors];
Map<Id, New_Door_Assignment__c> newDoorsMap = new Map<Id, New_Door_Assignment__c>();
for(New_Door_Assignment__c newDoor : newDoorsList) {
(newDoorsMap.put(newDoor.ID, newDoor));
}
List<Assigned_Address__c> assignedToUpdate = new List<Assigned_Address__c>();
assignedToUpdate = [Select ID, Name, Address__c, Rep__c, New_Door_Assignment__c from Assigned_Address__c
where New_Door_Assignment__c in : newDoors
Order by Address__c];
Integer assignedIndex = 0;
for (Assigned_Address__c assigned2 : assignedToUpdate){
New_Door_Assignment__c newDoor = newDoorsMap.get(assigned2.New_Door_Assignment__c);
if (assignedIndex < newDoorsList.Doors_Per_Rep__c)
assigned2.Rep__c = newDoorsList.Rep_One__c;
else {
if (assignedIndex < (2*newDoorsList.Doors_Per_Rep__c))
assigned2.Rep__c = newDoorsList.Rep_Two__c;
else {
assigned2.Rep__c = newDoorsList.Rep_Three__c;
assignedIndex++;
}
update assignedToUpdate;
}
All Answers
Ram:
Here is what I decided to do. I decided to write a spearate trigger because the assigning of sales reps to address will not occur at the same time that a New_Door_Assignment__c record gets created. It will occur once our sales manager goes into the record, updates Sales_Rep_One__c, etc on the New_Door_Assignment__c record and updates Assigned__c to TRUE.
Here is what I've got:
I am getting Compile Error: Initial term of field expression must be a concrete SObject: SET<String> at line 17 column 34
I feel like I am close. Where have I gone wrong here?
Thanks!!
Hampton
It's complaining because newDoors is a Set that you defined at the beginning. Here's what you might need. It will get you there for one record htting the trigger at a time. There is one last part to multiple records hitting the trigger. But for now, try this:
trigger RepAssignment on New_Door_Assignment__c (after update) {
Set<String> newDoors = new Set<String>();
for(New_Door_Assignment__c newDoorRep : trigger.new) {
if(newDoorRep.Assigned__c = TRUE) {
(newDoors.add(newDoorRep.ID));
}
}
List<New_Door_Assignment__c> newDoorsList = [Select Id, Doors_Per_Rep__c, Sales_Rep_One__c,
Sales_Rep_Two__c, Sales_Rep_Three__c
from New_Door_Assignment__c
where Id in :newDoors];
Map<Id, New_Door_Assignment__c> newDoorsMap = new Map<Id, New_Door_Assignment__c>();
for(New_Door_Assignment__c newDoor : newDoorsList) {
(newDoorsMap.put(newDoor.ID, newDoor));
}
List<Assigned_Address__c> assignedToUpdate = [Select ID, Name, Address__c, Rep__c, New_Door_Assignment__c
from Assigned_Address__c where
New_Door_Assignment__c in : newDoors
Order by Address__c];{
Integer assignedIndex = 0;
for (Assigned_Address__c assigned2 : assignedToUpdate) {
New_Door_Assignment__c newDoor = newDoorsMap.get(assignedToUpdate.New_Door_Assignment__c);
if (assignedIndex < newDoor.Doors_Per_Rep__c)
assigned2.Rep__c = newDoor.Sales_Rep_One__c;
else
if (assignedIndex < (2*newDoor.Doors_Per_Rep__c))
assigned2.Rep__c = newDoor.Sales_Rep_Two__c;
else
assigned2.Rep__c = newDoor.Sales_Rep_Three__c;
assignedIndex++;
}
update assignedToUpdate;
}
I am getting an error on:
New_Door_Assignment__c newDoor = newDoorsMap.get(assignedToUpdate.New_Door_Assignment__c);
I am getting error Compile Error: Initial term of field expression must be a concrete SObject: LIST<Assigned_Address__c> at line 27 column 59
Any thoughts?
Hampton
I'm getting the error now on the assigned2.Rep__c = newDoorsList.Rep_One__c line. It's telling me the same concrete sObject error
Try this:
trigger RepAssignment on New_Door_Assignment__c (after update) {
Set<String> newDoors = new Set<String>();
for(New_Door_Assignment__c newDoorRep : trigger.new) {
if(newDoorRep.Assigned__c = TRUE) {
(newDoors.add(newDoorRep.ID));
}
}
List<New_Door_Assignment__c> newDoorsList = [Select Id, Doors_Per_Rep__c, Rep_One__c,
Rep_Two__c, Rep_Three__c, Rep_Four__c, Rep_Five__c
from New_Door_Assignment__c
where Id in :newDoors];
Map<Id, New_Door_Assignment__c> newDoorsMap = new Map<Id, New_Door_Assignment__c>();
for(New_Door_Assignment__c newDoor : newDoorsList) {
(newDoorsMap.put(newDoor.ID, newDoor));
}
List<Assigned_Address__c> assignedToUpdate = new List<Assigned_Address__c>();
assignedToUpdate = [Select ID, Name, Address__c, Rep__c, New_Door_Assignment__c from Assigned_Address__c
where New_Door_Assignment__c in : newDoors
Order by Address__c];
Integer assignedIndex = 0;
for (Assigned_Address__c assigned2 : assignedToUpdate){
New_Door_Assignment__c newDoor = newDoorsMap.get(assigned2.New_Door_Assignment__c);{
if (assignedIndex < newDoorsList.Doors_Per_Rep__c)
assigned2.Rep__c = newDoorsList.Rep_One__c;
else {
if (assignedIndex < (2*newDoorsList.Doors_Per_Rep__c))
assigned2.Rep__c = newDoorsList.Rep_Two__c;
else {
assigned2.Rep__c = newDoorsList.Rep_Three__c;
assignedIndex++;
}
update assignedToUpdate;
}
Hi
try to create for loop above the condition and do like this.
for (interger i=0;i<=Trigger.new.size();i++)
{
if (assignedIndex < newDoorsList[i].Doors_Per_Rep__c)
assigned2.Rep__c = newDoorsList[i].Rep_One__c;
}
Thanks & Regards,
Rajesh S.
Same problem...assigned2 is a list. Does it need to be a Map or a Set? That's what is giving the error.
I really appreciate all your help.
Thanks,
Hampton
Sorry - one more extra brace causing an issue I believe. Please copy and paste the following and try it again.
trigger RepAssignment on New_Door_Assignment__c (after update) {
Set<String> newDoors = new Set<String>();
for(New_Door_Assignment__c newDoorRep : trigger.new) {
if(newDoorRep.Assigned__c = TRUE) {
(newDoors.add(newDoorRep.ID));
}
}
List<New_Door_Assignment__c> newDoorsList = [Select Id, Doors_Per_Rep__c, Rep_One__c,
Rep_Two__c, Rep_Three__c, Rep_Four__c, Rep_Five__c
from New_Door_Assignment__c
where Id in :newDoors];
Map<Id, New_Door_Assignment__c> newDoorsMap = new Map<Id, New_Door_Assignment__c>();
for(New_Door_Assignment__c newDoor : newDoorsList) {
(newDoorsMap.put(newDoor.ID, newDoor));
}
List<Assigned_Address__c> assignedToUpdate = new List<Assigned_Address__c>();
assignedToUpdate = [Select ID, Name, Address__c, Rep__c, New_Door_Assignment__c from Assigned_Address__c
where New_Door_Assignment__c in : newDoors
Order by Address__c];
Integer assignedIndex = 0;
for (Assigned_Address__c assigned2 : assignedToUpdate){
New_Door_Assignment__c newDoor = newDoorsMap.get(assigned2.New_Door_Assignment__c);
if (assignedIndex < newDoorsList.Doors_Per_Rep__c)
assigned2.Rep__c = newDoorsList.Rep_One__c;
else {
if (assignedIndex < (2*newDoorsList.Doors_Per_Rep__c))
assigned2.Rep__c = newDoorsList.Rep_Two__c;
else {
assigned2.Rep__c = newDoorsList.Rep_Three__c;
assignedIndex++;
}
update assignedToUpdate;
}
Thanks again for your help. Here is where I am:
1. The code is saved
2. The code is mapping, somewhat. It is putting Rep_One__c in the Rep__c field on all Assigned_Address__c records. Here is the code I was able to get to do that. Now I just need to get the Rep_Two__c and Rep_Three__c mapping to work based on the index.
Thank you so much again for your help!
Hampton
Hampton - You need to be careful with the use of braces and to include the exact code that needs to be there within each pairing of braces. Your assignedIndex++; needs to be outside all braces except for the one for the "for" loop. If it's easier to read, just initialize assignedIndex to -1 outside the loop (rather than to 0) and then make assignedIndex++; the first statement in your loop.
best,
Ram
Ram:
I forgot to let you know yesterday that this did work and is doing what i needed it to do. Thank you again for all your help.
Hampton