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
Bujji vBujji v 

@future Method

When I am trying to update a field by using @future method. It was showing an error message like " Future method cannot be called from a future or batch method " .

Here is my code...

 

My trigger is

 

trigger test on Contact (before insert,before update) {

for (Contact contact : Trigger.new) {

 

        string endPoint;      
        endPoint = ''https://test.com/body.json';
        Webcooks.GetRequest(endPoint,contact.LastName,contact.email);

        string endPoint2;      
        endPoint2 = ''https://test.com/response.json?email=';
        Webcooksstatus.GetRequest(endPoint,contact.LastName,contact.email);
        contact.Campaign_Monitor_Status__c = '';
        Webcooksstatus.test();

}

 

}

 

 

 

My class is...

 

Public class Webcooks
{
@future(callout=true)
public static void GetRequest(String url,string LastName,string email)
{


HttpRequest con = new HttpRequest();

con.setBody('{"EmailAddress": "'+email+'","Name": "'+LastName+'"}');
String username = 'dfsfsdffsfsa';
String password = '';

 

Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
con.setHeader('Authorization', authorizationHeader);
con.setEndpoint(url);
con.setMethod('POST');

Http http = new Http();
HTTPResponse res = http.send(con);

}
}

 

My second class is....

 

Public class Webcooksstatus
{

public static string statused{get;set;}
public static string status{get;set;}
public static string state{get;set;}

@future(callout=true)
public static void GetRequest(string url,string LastName,String email)
{
String body='{"EmailAddress": "'+email+'","Name": "'+LastName+'"}';
state = email;
HttpRequest con = new HttpRequest();
con.setMethod('GET');
String username = 'dfsfsdffsfsa';
String password = '';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
con.setHeader('Authorization', authorizationHeader);

con.setEndpoint(url+Email);

Http http = new Http();
HTTPResponse res = http.send(con);

JSONParser parser = JSON.createParser(res.getBody());
       
        while (parser.nextToken() != null) {
            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
                (parser.getText() == 'State')) {
                
                parser.nextToken();
               
                status = parser.getText();
               
            }
        }
         statused =status;
}

test();
}

public static void test()
{
list<Contact> ContactsToUpdate =  new List<Contact>();
for (Contact c : [Select Id, Name,Campaign_Monitor_Status__c  From Contact where email=:state]) {

c.Campaign_Monitor_Status__c = statused ;

ContactsToUpdate.add(c);
}
update ContactsToUpdate;

}

}

 

 

I want to update "Campaign_Monitor_Status__c"  field with the "statused".... Please Help me

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
magicforce9magicforce9

Hi,

 

Your Webcooksstatus.test() method is trying to do an update on contact object which is causing the trigger to fire again....(recursive trigger)

 

You need to check if the trigger is being fired from the future method, add the below line as your first line of trigger.

if(system.isFuture()) return;

 

All Answers

magicforce9magicforce9

Hi,

 

Your Webcooksstatus.test() method is trying to do an update on contact object which is causing the trigger to fire again....(recursive trigger)

 

You need to check if the trigger is being fired from the future method, add the below line as your first line of trigger.

if(system.isFuture()) return;

 

This was selected as the best answer
Bujji vBujji v

Thanks...It is working fine..