You need to sign in to do that
Don't have an account?

Trigger help. Pull Query result into field
Hello,
I am tring to use a dyamic query to pull a value from a related object depending on the PRODUCT and SUPPLIER used.
From what i can see the trigger is working to an extent. The "Message__c" field is being populated but getting alot more info than required. See the code used below.
The correct result is in there and it is "54545.00"
Can anyone advise how i can change this?
Thanks,
Matt
I am tring to use a dyamic query to pull a value from a related object depending on the PRODUCT and SUPPLIER used.
From what i can see the trigger is working to an extent. The "Message__c" field is being populated but getting alot more info than required. See the code used below.
trigger GetBinWeight on OpportunityLineItem (before update) { for (OpportunityLineItem binWeight : Trigger.new) { string SupplierID = binWeight.Waste_Supplier_ID__c; string WeightField = binWeight.Weight_Field_To_Use__c; List<Waste_Supplier__c> myList = Database.query('SELECT ' + WeightField + ' FROM Waste_Supplier__c WHERE Waste_Supplier__c.ID = :SupplierID limit 1'); string TheValue = string.valueOf(myList); binWeight.Message__c = TheValue; } }The result in "Message__c" is: (Waste_Supplier__c:{X1100_generalwaste_weight_nonefood__c=54545.00, Id=a09L00000056MbdIAE})
The correct result is in there and it is "54545.00"
Can anyone advise how i can change this?
Thanks,
Matt
If you use any SOQL query without any field, the Query will return you the Id field by default. So the result will include Id value also.
As per my unserstanding you need X1100_generalwaste_weight_nonefood__c in Message__c field
For that you can simply assign that filed to the Message__c field
if(!myList.isEmpty()){
binWeight.Message__c = String.valueOf(myList[0].X1100_generalwaste_weight_nonefood__c);
}
Regards,
Bhanu Mahesh
I have made a small changes in code , can you please check this and let me know the result.
If this answer resolve your issue please mark answer as solution.
Thanks,
Shiv
Thanks for the help. When i try the code you suggested im getting "Comparison arguments must be compatible types: Boolean, Integer" on line 9.
Any ideas how to get around that?
Thanks,
Matt
This may be because of this line wasteSuppList.isEmpty() != 0 of which one is boolean and the other is Integer while comparing
Please try this code
Regards,
Bhanu Mahesh
Bhanu,
The works great! If i want wanting to populate a numeric field instead of a text what would i need to edit?
I've tried using: binWeight.WeightResult__c = decimal.valueof(wasteSuppList[0].get(WeightField));
But it says decimal does not exist
Thanks,
Matt
if WeightResult__c is a number field with precesion
binWeight.WeightResult__c = wasteSuppList[0].get(WeightField);
if it has no precession then
.binWeight.WeightResult__c = wasteSuppList[0].get(WeightField).intValue();
Note: Check for null whereever possible to avoid exception
Regards,
Bhanu Mahesh