+ Start a Discussion
StreepieStreepie 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

Hi,

 

 

I have to update a Custom field in Account before update of account 

 

So I have a trigger on Accounts before update

 

That trigger is calling a apex class which executes a webservice request to a external webservice

 

I am getting a value back in the respone...

 

BUT when i try to update the account in the Apex class i am getting a error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

 

 

 

THIS IS MY TRIGGER 

 

trigger updateRelatienrInAccountIfKlant on Account (after insert, after update) {

    System.debug('Trigger on account');
     
  
    
    string uid=UserInfo.getUserId();
    User usr = [SELECT Alias FROM User WHERE Id = :uid];
    
    
    // Find accounts to update  
    for(Account a: Trigger.new){
      
       
          
            integer mskey = integer.valueof(a.MarktSelect_Sleutel__c);
            soapmessage_naar_marktselect.salesforce(a.Id, usr.Alias, uid, mskey);
    }
}

 

 

 

THIS IS MY APEX CLASS

 

 

public class soapmessage_naar_marktselect {

  //Future annotation to mark the method as async.
  // methods may call this class =callout=true
  @Future(callout=true)

   public static void salesforce(String id, String uid, String upw, Integer mskey ) {

   System.debug('Calling ccc.marktselect.nl/SFservice');


   //construct an HTTP request
   HttpRequest req = new HttpRequest();
   req.setEndpoint('http://www.abc.nl/cgi-bin/jsmdirect?SFservice');
   req.setMethod('POST');
   req.setHeader('Content-Type', 'text/xml; charset=utf-8');

   // Create the soap message envelope 
   String soapMsg = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://soapserver.jsm.marktselect.com">'+
                       '<soapenv:Header/>'+
                           '<soapenv:Body>'+
                             '<soap:NewAccount>'+
                             '<soap:UserId>'+uid+'</soap:UserId>'+
                             '<soap:UserPw>'+upw+'</soap:UserPw>'+
                             '<soap:Mskey>'+mskey+'</soap:Mskey>'+
                          '</soap:NewAccount>'+
                       '</soapenv:Body>'+
                    '</soapenv:Envelope>';
                
   // set the soapbody to the message envelope                 
   req.setBody(soapMsg);




   //send the request
   Http http = new Http();
   HttpResponse res = http.send(req);
     
   integer rlcode = 0;
       
   //check the response
   if (res.getStatusCode() == 200) {
         XMLDom responseXML = new XMLDom(res.getBody());
         rlcode = integer.valueof(responseXML.getElementByTagName('relatiecode').nodeValue);
         
         if (rlcode != 0){
             //update account
             Account acc = new Account(Id=id);
             acc.Relatienr__c = rlcode;
             update acc;
             
         } 

    } else {
          System.debug('Callout failed: ' + res);
    }
     
  }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
StreepieStreepie

Case solved...

 

1 = MUST use before insert and before update instead of after insert or update  (if not you get readonly)

2 = MUST test in trigger if value has been updated otherwise it will call the apexcode again and again....

 

(...after the update in the apexcode the trigger is called again...)