1

I have a ajax cutom button that calls a apex class and the class is changing status field of that object. Now I want to invoke a before update trigger on the same object, which is not working.

Custom Button :

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}

sforce.apex.execute("ABV_SMPORD","submitSMPORD",{localId:"{!Sample_Order__c.Id}"});
alert("Done");

Apex Class:

global class ABV_SMPORD{
    webservice static void submitSMPORD(Id localId) { 
                V_SAMPLE_CANCEL.isSampleCancel = true;
                Sample_Order__c sot = new Sample_Order__c(Id=localId);
                sot.Status__C='Submitted';
                sot.Delivery_Status__C='Delivered'; 
    }
}

Trigger:

trigger V_SAMPLE_ORDER_TRANSACTION_BEFORE on Sample_Order__c (before update,before delete) {
    String ProfileId = UserInfo.getProfileId();
    V_ERROR_MSG_BUNDLE bundle = new V_ERROR_MSG_BUNDLE();
    Profile pr = [Select Id, PermissionsModifyAllData From Profile where Id = :ProfileId];
    boolean modAllData = false;
    if (pr != null && pr.PermissionsModifyAllData)
       modAllData = true;
    if (Trigger.isUpdate) {
        boolean isUnlocked = false;            
        for (Sample_Order__c so : Trigger.new) {
            if (so.Unlock__c == true) {
                so.Status__c = 'Saved';
                so.Unlock__c = false;
                isUnlocked = true;
            } else {
                if (modAllData == false && so.Status__c == 'Submitted') {
                    if (V_SAMPLE_CANCEL.isSampleCancel == false) 
                       so.Id.addError(bundle.getErrorMsg('UPD_SAMPORDER'), false);
                }
            }
        }
    } else {
        //  
        for (Sample_Order_Transaction__c so : Trigger.old) {
            if ( so.Status__c == 'Submitted') {
                so.Id.addError(bundle.getErrorMsg('DEL_SAMPORDER'), false);
            }
        }
    }
}
6

In order to trigger the trigger you need to actually save the record to the database. So update your code to this:

global class ABV_SMPORD{
webservice static void submitSMPORD(Id localId) { 
            V_SAMPLE_CANCEL.isSampleCancel = true;
            Sample_Order__c sot = new Sample_Order__c(Id=localId);
            sot.Status__C='Submitted';
            sot.Delivery_Status__C='Delivered'; 
            // adding an update here!
            update sot;
}
| improve this answer | |

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.