-1

when I create or update record, am getting too many soql query error. following are my code:

trigger UpdateAssetImageurl on SymphonyIPM__Patent__c (after insert,after update) {

    List<SymphonyIPM__Patent__c> objPatents = [SELECT Id,  SymphonyIPM__Asset_Image__c, Asset_Image_Url__c FROM SymphonyIPM__Patent__c where id IN: Trigger.new];
    for(SymphonyIPM__Patent__c objPatent : objPatents)
    {
        if (objPatent.SymphonyIPM__Asset_Image__c!=null)
        {
            objPatent.Asset_Image_Url__c = objPatent.SymphonyIPM__Asset_Image__c;
            system.debug(objPatent.Asset_Image_Url__c);
            objPatent.Asset_Image_Url__c = objPatent.Asset_Image_Url__c.replace('<img src="','');
            objPatent.Asset_Image_Url__c = objPatent.Asset_Image_Url__c.replace('" width="500" height="281"></img>','');
            objPatent.Asset_Image_Url__c = objPatent.Asset_Image_Url__c.replace('&amp;','&');
            objPatent.Asset_Image_Url__c = objPatent.Asset_Image_Url__c.replace('seq=1&IsImage=1','');
            system.debug(objPatent.Asset_Image_Url__c);    
        }

    }
    update objPatents;
}
  • please format your code. – glls Apr 6 '18 at 13:29
  • i am getting this error :CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateAssetImageurl: maximum trigger depth exceeded Patent trigger event AfterInsert Patent trigger event AfterUpdate – Elango Apr 6 '18 at 13:33
  • if you have anything to add, please edit your post rather than followin up on comments, thanks – glls Apr 6 '18 at 13:34
  • You have a trigger that runs when SymphonyIPM__Patent__c instances are updated and then within that trigger you are updating SymphonyIPM__Patent__c instances. So you enter an infinite loop. See Tushar's answer that avoids that. – Keith C Apr 6 '18 at 13:41
  • The error that your question states that you're receiving is different from the error that you provided us in the comments. Please clarify which one you are actually seeing (again, you should do this as an edit to your question, not as a comment on your question). Also, paraphrasing error messages does not help people understand your problem. Error messages are generally one of the things that you should provide verbatim (i.e. word-for-word) along with the stack trace. – Derek F Apr 6 '18 at 13:50
5

You don't need a Query here. You can easily do this in Before insert and Before update event.

trigger UpdateAssetImageurl on SymphonyIPM__Patent__c (before insert,before update) {

 for(SymphonyIPM__Patent__c objPatent : Trigger.New)
{
    if (objPatent.SymphonyIPM__Asset_Image__c!=null)
    {
        objPatent.Asset_Image_Url__c = objPatent.SymphonyIPM__Asset_Image__c;
        system.debug(objPatent.Asset_Image_Url__c);
        objPatent.Asset_Image_Url__c = objPatent.Asset_Image_Url__c.replace('<img src="','');
        objPatent.Asset_Image_Url__c = objPatent.Asset_Image_Url__c.replace('" width="500" height="281"></img>','');
        objPatent.Asset_Image_Url__c = objPatent.Asset_Image_Url__c.replace('&amp;','&');
        objPatent.Asset_Image_Url__c = objPatent.Asset_Image_Url__c.replace('seq=1&IsImage=1','');
        system.debug(objPatent.Asset_Image_Url__c);    
    }

}

}

In before trigger we also don't need DML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.