• skyla traci
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi Everyone,

I have a requirement where I need to call the external webservice and populate the fields on insert or update of record.

Already there is a button on record built using lightning component which will pass few values to external system and in return it populates few fields based on the input fields.

the lightning button code looks like below:
Apex:

@AuraEnabled
    public static object__c updatemethod(Id objId) {
        List<Id> dlist = new List<Id>();
        List< object __c> ret;
        dlist.add(objId);
        if(dlist.size()>0){
            ret = class1.updatemethod2(getDetailsByIds(dlist));}
            return ret.get(0);
    }
Global with sharing class class1{
private static List< object __c> calculateDate(List< object __c> dList) {
                              for (object __c d : dList) {
           
            // If date is not filled in, fetch the values
            if (d.Month__c == null && d.Day__c == null &&
                d.Day_g__c != null && d.Month_g__c != null & d.Year_g__c != null) {
                    String[] hDate = getDateFromG(d.Day_g__c, d.Month_g__c,
                                                                              d.Year_g__c, d.IsChecked__c);
                    d.Year__c = hDate[0];
                    d.Month__c = hDate[1];
                    d.Day__c = hDate[2];
            }
Update dlist;
     }  

public static String[] getDateFromG (String dayg, String monthg,
                                                      String yearg, Boolean IsChecked) {
               String url = geturl(dayg, monthg, yearg, IsChecked);
                              System.debug(url);
        String json = getContent(url);

                              String[] ret = new String[3];
                              JSONParser parser = System.Json.createParser(json);
                                                          
                              while (parser.nextToken() != null) {
            system.debug('inside while');
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
               if (parser.getText() == 'hy') {
                               // Get the value.
                           parser.nextToken();
                       ret[0] = parser.getText();
                       System.debug('Year='+ret[0]);
               }
               …..
            }
        }
                              return ret;       
    }   
private static String getContent(String url) {
                              // Instantiate a new http object
            Http h = new Http();
           
            // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
            HttpRequest req = new HttpRequest();
            req.setEndpoint(url);
            req.setMethod('GET');
           
            // Send the request, and return a response
            HttpResponse res = h.send(req);
            return res.getBody();
    }
I added the below code when a new record is inserted or updated , it should fire the trigger and callout the url to retrieve and populate the fields in the record.
trigger TriggerInsertorupdate on object__c (after insert, after update) {

if(Trigger.isAfter){
         if(updation.isfutureupdate!=true)
            {
               id lst;
                for(Deceased__c e :Trigger.new)
                {
                    lst=e.Id;
                    system.debug('$$$$$$$$$$$'+lst);
                }
                if(lst!=null)
                UpdateController.updatemethod(lst);
            }
    }       
}
I am getting the following error. Please can anyone give some idea to achieve this .
TriggerInsertorupdate : execution of AfterInsert caused by: System.CalloutException: Callout from triggers are currently not supported. Class.class1.getContent: line 112, column 1 
Thanks in Advance,
Sirisha