+ Start a Discussion
JJamesJJames 

Assign new value to custom settings through trigger update?

I am trying to assign a value to a custom setting with an automated daily data pull from a site, which all works fine but I am having trouble assigning the actual custom setting variable to the new value.  Is this not possible?  The code is below, and the part that matters is the last few lines of code which try to assign a new value to a custom settings variable.
 
global class ALVIScheduledCurrencyDataPull Implements Schedulable
{
    global void execute(SchedulableContext sc)
    {
        pullCADRatio();
    }
    
    @future(callout=true)
    public static void pullCADRatio()
    {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('http://apilayer.net/api/live?access_key=getoffmykey');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        system.debug(response.getStatusCode());
        system.debug(response.getBody());
        
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) 
        {
            // Deserialize the JSON string into collections of primitive data types.
            
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            system.debug(results);
            // Cast the values in the 'quotes' key as a list
            Map<String,Object> ob = (Map<String, Object>)results.get('quotes');
            system.debug('quotes object: ' + ob);
            
            Object tst = ob.get('USDCAD');
            system.debug(tst);
            Decimal cadCon = (Decimal)tst;
            system.debug(cadCon);
			
            List<Currency_Conversion_Ratios__c> cadList = new List<Currency_Conversion_Ratios__c>();
            //pull the conversion ratio object, assign value from site, update into SFDC  
            Currency_Conversion_Ratios__c CADconvert = 
                Currency_Conversion_Ratios__c.getOrgDefaults();

            CADconvert.CAD__c = cadCon;
            cadList.add(CADconvert);
            update cadList; 

        }
    }
}



 
Richard Jimenez 9Richard Jimenez 9
Hi,

Have you enabled debug logs to see what errors you are getting?

On a side note, I would recommend refactoing the code into separate utility methods and writing some unit tests to test the parts in isolation, which should make it easier to find bugs and easier to maintain going foward. For example -

In pullCADRatio method -

Call CurrencyUtils.UpdateCADRatio

   --> Call CurrencyUtils.GetCurrencyRatio(String currencyString) returns Decimal (ratio)
          -- You could make this more dynamic by pulling in a list of currency strings to pull back, allowing you to update more currency ratios in your custom setting. If you re-factor this now, it would make it easier to extend in the future if you need to.
   
   --> Call CurrencyUtils.UpdateCADRatioCustomSetting(Decimal ratio) (updates custom setting)
          -- Only update if the value has changed. You could pass in a map of values (Map<String, Decimal>) to allow you to update multiple currencies in the future (if you ever see this becoming a requirement) 

Put the endpoint in a label or custom setting so it can be configured.

Thanks,
Richard.