function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Matt TindallMatt Tindall 

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.
 
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
 
Bhanu MaheshBhanu Mahesh
Hi 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
shiv@SFDCshiv@SFDC
Hi Matt,

I have made a small changes in code , can you please check this and let me know the result.
 
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> wasteSuppList = Database.query('SELECT ' + WeightField + ' FROM Waste_Supplier__c WHERE Waste_Supplier__c.ID = :SupplierID limit 1');        
        
		if(! wasteSuppList.isEmpty() != 0){
		
			string TheValue = String.valueOf(wasteSuppList[0].get(WeightField));
			binWeight.Message__c = TheValue;
		}                
        
        
    }
}



If this answer resolve your issue please mark answer as solution.
Thanks,
Shiv
Matt TindallMatt Tindall
Hi Bhanu,

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
Bhanu MaheshBhanu Mahesh
Hi 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
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> wasteSuppList = Database.query('SELECT ' + WeightField + ' FROM Waste_Supplier__c WHERE Waste_Supplier__c.ID = :SupplierID limit 1');        
        
		if(!wasteSuppList.isEmpty()){
			binWeight.Message__c = String.valueOf(wasteSuppList[0].get(WeightField));
		}                
        
        
    }
}


Regards,
Bhanu Mahesh
shiv@SFDCshiv@SFDC
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> wasteSuppList = Database.query('SELECT ' + WeightField + ' FROM Waste_Supplier__c WHERE Waste_Supplier__c.ID = :SupplierID limit 1');        
        
		if(! wasteSuppList.isEmpty() ){
		
			string TheValue = String.valueOf(wasteSuppList[0].get(WeightField));
			binWeight.Message__c = TheValue;
		}                
        
        
    }
}
Matt TindallMatt Tindall

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

Bhanu MaheshBhanu Mahesh
In that case, you can directly assign the value
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