You need to sign in to do that
Don't have an account?
developernewbie
Copy value from one object to another object based on criteria
Hello,
I am new to developing code in Apex, so mind my errors. To give you a background of what the current scenario looks like
There is a Master Object say Object Master.
Object A and Object B each have a Master -Detail relationship with with Object Master
Object A:
Fields-
-Date
-Amount A
Object B:
Fields-
-Date
-Amount B
What I am trying to achieve in my code is that ;
if date of object A matches date of object B then i would like the value of Amount A equal to Amount B. I am trying to write this in a Class and having trouble getting to this.
The reason there are two objects - Object B is actualzied with a Data Load at end of every month with actual numbers. This then I would like to flow in to Object A so as to show reveue in that object.
Thanks in advance for helping!
I am new to developing code in Apex, so mind my errors. To give you a background of what the current scenario looks like
There is a Master Object say Object Master.
Object A and Object B each have a Master -Detail relationship with with Object Master
Object A:
Fields-
-Date
-Amount A
Object B:
Fields-
-Date
-Amount B
What I am trying to achieve in my code is that ;
if date of object A matches date of object B then i would like the value of Amount A equal to Amount B. I am trying to write this in a Class and having trouble getting to this.
The reason there are two objects - Object B is actualzied with a Data Load at end of every month with actual numbers. This then I would like to flow in to Object A so as to show reveue in that object.
Thanks in advance for helping!
There is a similar situation and code available in the link below,
http://salesforce.stackexchange.com/questions/23055/trigger-to-update-custom-field-from-another-custom-field-on-a-separate-object
Regards,
Ashish
One thing i forgot to mention is that in the below code you will see Custom_Solution__c field. this is on both objects A and B and had master detail relationship with the master object.
As you can see I am a new and still trying to understand Apex coding. Could you help me with this?
trigger UpdateActualDelivery on ObjectB__c (after insert, after update) {
Set<String> customSolutions = new Set<String>();
for(ObjectB__c objB:trigger.new) {
customSolutions.add(ObjB.Custom_Solution__c);
}
List <ObjectA__c> objA = [SELECT Date__c,Actual_Delivery__c
FROM ObjectA__c
WHERE Custom_Solution__c =:customSolutions];
Map <String, ObjB__c> listMap = new Map<String, ObjB__c>();
for(ObjB OB : trigger.new) {
listMap.put(OB.Custom_Solution__c, OB);
}
for (ObjectA__c OA : OARev) {
if(listMap.containsKey(OA.Custom_Solution__c)){
if(OA.Date__c = listMap.get(OA.Custom_Solution__c).Date__c) {
OA.Actual_Delivery__c = listMap.get(OA.Custom_Solution__c).Actual_Delivery__c;
}
}
}
update OARev;
}
Hi,
Can you share the error and the description that you are getting on the line so that we can have a better idea.
Regards,
Ashish
Sure, so i fixed the error, I was not comparing the right way in my if statement. But now I have written the below code and an am still unable to get it to work. There are no errors, just doesnt show the output I desire.
Here is my actual trigger. Thanks Again everyone!! :)
trigger CustomSolutionRevenue on Custom_Solution__c (after insert, after update) {
CustomRevenue cr = new CustomRevenue();
for(Custom_Solution__c CustomSoln: System.trigger.new){
delete[select id from Custom_Solution_Revenue__c where Custom_Solution__c =:CustomSoln.id];
List <Custom_Solution_Revenue__c> customRevenueToInsert = new List<Custom_Solution_Revenue__c>();
customRevenueToInsert.addAll(cr.CustomRevenueCalculation(CustomSoln));
//Updating Actual Delivery
List <Custom_Solution_Delivery__c> customDel = [Select Custom_Solution__c,id,date__c,actual_delivery__c from Custom_Solution_Delivery__c
where Custom_Solution__c =:CustomSoln.id];
for (Custom_Solution_Delivery__c cusoDel : customDel){
for(Custom_Solution_Revenue__c csr : [Select Date__c,Actual_Delivery__c from Custom_Solution_Revenue__c where Custom_Solution__c =:CustomSoln.id]){
if(csr.Custom_Solution__c == cusoDel.Custom_Solution__c){
system.debug('DO YOU SEE THIS');
if(csr.Date__c == cusoDel.Date__c){
system.debug('DID THIS WORK');
csr.Actual_Delivery__c = cusoDel.Actual_Delivery__c;
}
else{csr.Actual_Delivery__c = 0000;}
customRevenueToInsert.add(csr);
}
}//for loop 3
}//for loop 2
//Actions
insert customRevenueToInsert;
} // for loop
} //main trigger
Enable debug logs for the user record and look into the logs - it will help understand the error,
Monitoring | Debug Logs or Logs | Debug Logs, and then click View next to the debug log you want to examine. Click Download
http://help.salesforce.com/HTViewHelpDoc?id=code_viewing_log_details.htm&language=en_US
Regards,
Ashish
Note:
- Based on the situation you've given regarding if Object A's date matches to Object B's date, that you would like for Amount A to equal Amount B, I am assuming that this will occur before the record is saved since records within Trigger.new is read-only. Thus, these changes must occurr before the record is saved. I also changed the object that the trigger is based on from Object B to Object A since we are comparing new or updated Object A records to Object B.
- No DML statements were necessary since these changes occurr before the records is created or updated in the database.
- I didn't test this code, so I apologize for any typos that I may have made.
- I added comments so that is easier to follow the process:
Code Logic:
trigger UpdateActualDelivery on ObjectA__c (before insert, before update) {
//Setup
//Gather all Object B records and store within a list via SOQL
List <ObjectB__c> objB = [SELECT Date__c,Actual_Delivery__c
FROM ObjectB__c];
//Create new Map to store Date values from Object B and its records
Map <Date, ObjectB__c> objBMap = new Map <Date,ObjectB__c>();
//Store values within Map using for loop from objB list
for (ObjectB__c singObjB : objB) {
objBMap.put(singObjB.Date__c, singObjB);
}
//Execute
//Iterate through the Trigger.new records
for (ObjectA__c singObjA : Trigger.new) {
//Logic: If the objBMap contains a key that is equivalent to the Date value within the object A record, then it is confirmed that there is
// at least one object B record that has a date that matches the Object A record. Thus, this will return true and execute the rest of the
// code.
if (objBMap.containsKey(singObjA.Date__c)) {
singObjA.Actual_Delivery__c = objBMap.get(singObjA.Date__c).Actual_Delivery__c;
}
}
}