• Bryan REVELANT 2
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
Having a bit of an issue with an if else Trigger.

If Rejected --- marked rejected on Parent
If Submitted not all Approved -- Mark Submit
If all Approved -- Approved.


trigger ADRUserTriggerV2 on ADRUser__c (after update) {

    list<ADR__c> ListOfADRRecords;
    set<Id> PrimaryKeyId = new set<Id>();
    for(ADRUser__c ADRUserRecords :trigger.new){
 
  if(trigger.oldmap.get(ADRUserRecords.id).ApproveReject__c != 'Submit'){
     if(ADRUserRecords.ApproveReject__c =='Submit' || ADRUserRecords.ApproveReject__c =='Approved' || ADRUserRecords.ApproveReject__c =='Rejected'){
   
            PrimaryKeyId.add(ADRUserRecords.ADR__c);// you child object must having parent object field, put that field Api Name here
  }
   
    }
    }

   if(!PrimaryKeyId.isEmpty()) {
                list<ADR__c> listOfParentRecordsToUpdate = [select id, Apex_Status__c from ADR__c where Id =: PrimaryKeyId];
                ListOfADRRecords = new list<ADR__c>();
     
             
    
                 
      for(ADRUser__c ADRUserRecords :trigger.new){
    for(ADR__c ADRRecordTOUpdate : listOfParentRecordsToUpdate) {

if (ADRUserRecords.ApproveReject__c =='Rejected' ) {
    ADRRecordTOUpdate.Apex_Status__c = 'Rejected';
} else if (ADRUserRecords.ApproveReject__c =='Submit' || ADRUserRecords.ApproveReject__c =='Approved') {
  ADRRecordTOUpdate.Apex_Status__c = 'Submit';
}  else {
    ADRRecordTOUpdate.Apex_Status__c = 'Approved';
}
ListOfADRRecords.add(ADRRecordTOUpdate);


            Update ListOfADRRecords;
}
          }
   }
    }
    // }
Hello

I have a trigger that I have a on a child record to update the parent if the conditions are met.. The trigger however isnt writting anythign to the parent.

If child has rejected write rejected.
If the record is submit and  not all approved then submit
else approved.

trigger ADRUserApproval on ADRUser__c (after update) {

list<id> ADRSubmittedList = new list<id>();
    list<ADR__c> ADRUserList = new list<ADR__c>();
   
    for(ADRUser__c lstADRTrigger : trigger.new){
        if(lstADRTrigger.Status__c == 'Submit' || lstADRTrigger.Status__c == 'Approved' || lstADRTrigger.Status__c == 'Rejected')
        {
        
       ADRSubmittedList.add(lstADRTrigger.id);
        }//*** End of if(lstADRTrigger.Next_Step__c...
    }//*** End of for(ADR__c lstADRTrigger : trigger.new){
 
    for(ADR__c lstADRTrigger : [ Select id, Apex_Status__c from ADR__c
    where id in :ADRSubmittedList] ){

       for(ADRUser__c lstADRUser : trigger.new){
       
         
       
       
        if(lstADRUser.Status__c == 'Submit' || lstADRUser.Status__c == 'Approved' || lstADRUser.Status__c == 'Rejected')
        {
        
         // if(lstADRUser.Status__c == 'Submit' && lstADRUser.Status__c == 'Approved' && lstADRUser.Status__c == 'Rejected'){
       
       lstADRTrigger.Apex_Status__c = 'Rejected';
      
    if(lstADRUser.Status__c == 'Submit' || lstADRUser.Status__c == 'Approved'){
            lstADRTrigger.Apex_Status__c = 'Submit';
         }
           else lstADRTrigger.Apex_Status__c = 'Approved';
           //  Site_Audit_Results__c FindAnswersVar = new Site_Audit_Results__c();
        ADR__c ADCUpdates = new ADR__c();
      
          ADCUpdates.Apex_Status__c = lstADRTrigger.Apex_Status__c;

        System.debug('lstADRTrigger ' + lstADRTrigger);
    
       
      ADRUserList.add(ADCUpdates);
     
          System.debug('ADCUpdates ' + ADCUpdates);
      update ADRUserList;
     


   
//}
}
}
}
}
Hello,

3 objects.

Custom object
Corss object
User object


I want to insert a list of userid from my cross object to flow within an approval. I have built code to do this with one user or hard code user id. However I want to take the cross object user id's and insert them into setNextApproverIds. however this appears to bomb everytime I try. Any thoughts on the below




trigger ADRTrigger on ADR__c(After update)
{

//    list<User> UserList = new list<User>();
      list<id> ADRUserIDVar = new list <id>();

public void ADRTrigger(list<ADRUser__c> NewADRU, map <id, ADRUser__c> NewMapSAS){
   map <id, list <id>> ADRUserMap = new map  <id, list <id>>();             
        
        for (ADRUser__c listSAS : NewADRU){
            system.debug('Log: ' + NewADRU.SIZE());
            list<id> ADRIds1st = new list<id>();
            if(ADRUserMap.containsKey(listSAS.id)){
                ADRIds1st = ADRUserMap.get(listSAS.id);
            }          
            ADRIds1st.add(listSAS.id);
            ADRUserMap.put(listSAS.User__c, ADRIds1st);         
            ADRUserIDVar.add(listSAS.User__c);               
        }
        system.debug('Variable ADRUserIDVar: ' + ADRUserIDVar);   
        }
      
      
    for (Integer i = 0; i < Trigger.new.size(); i++)
    {
     try
     {

        if(Trigger.new[i].Next_Step__c == 'Submit' && Trigger.old[i].Next_Step__c != 'Submit')
        {
           submitForApproval(Trigger.new[i]);
        }
     }catch(Exception e)
     {
         Trigger.new[i].addError(e.getMessage());
     }
    }

    //Methods to Submit the record
    public void submitForApproval(ADR__c opp)
    {
        // Create an approval request for the ADR__c
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval automatically using Trigger');
        req1.setObjectId(opp.id);
        req1.setNextApproverIds(ADRUserIDVar);

        Approval.ProcessResult result = Approval.process(req1);
    }
    //Get ProcessInstanceWorkItemId using SOQL
    public Id getWorkItemId(Id targetObjectId)
    {
        Id retVal = null;

        for(ProcessInstanceWorkitem workItem  : [Select p.Id from ProcessInstanceWorkitem p
            where p.ProcessInstance.TargetObjectId =: targetObjectId])
        {
            retVal  =  workItem.Id;
        }

        return retVal;
    }
}
I want a validation on the Javascript buttin, where if the current user is not the owner of the record, then display an error message. Is this possible. It doesnt seem to like userid.

I have tried var currentUser = sforce.connection.getUserInfo().userId
$userid
etc.

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

  var currentUser = sforce.connection.getUserInfo().userId

// identify the record
var o = new sforce.SObject("ADR_Non_Approval_Object__c");

o.id = "{!ADR_Non_Approval_Object__c.Id}";

if(currentUser.id != o.OwnerId){
alert("You must be the Assigned Approver to Approve this request.");
}
else{



// make the field change
o.Approval__c = "Approve";
o.Approval_Status_Date_Time__c = new Date();

// save the change
sforce.connection.update([o]);
}
//refresh the page
window.location.reload();
Hello,

3 objects.

Custom object
Corss object
User object


I want to insert a list of userid from my cross object to flow within an approval. I have built code to do this with one user or hard code user id. However I want to take the cross object user id's and insert them into setNextApproverIds. however this appears to bomb everytime I try. Any thoughts on the below




trigger ADRTrigger on ADR__c(After update)
{

//    list<User> UserList = new list<User>();
      list<id> ADRUserIDVar = new list <id>();

public void ADRTrigger(list<ADRUser__c> NewADRU, map <id, ADRUser__c> NewMapSAS){
   map <id, list <id>> ADRUserMap = new map  <id, list <id>>();             
        
        for (ADRUser__c listSAS : NewADRU){
            system.debug('Log: ' + NewADRU.SIZE());
            list<id> ADRIds1st = new list<id>();
            if(ADRUserMap.containsKey(listSAS.id)){
                ADRIds1st = ADRUserMap.get(listSAS.id);
            }          
            ADRIds1st.add(listSAS.id);
            ADRUserMap.put(listSAS.User__c, ADRIds1st);         
            ADRUserIDVar.add(listSAS.User__c);               
        }
        system.debug('Variable ADRUserIDVar: ' + ADRUserIDVar);   
        }
      
      
    for (Integer i = 0; i < Trigger.new.size(); i++)
    {
     try
     {

        if(Trigger.new[i].Next_Step__c == 'Submit' && Trigger.old[i].Next_Step__c != 'Submit')
        {
           submitForApproval(Trigger.new[i]);
        }
     }catch(Exception e)
     {
         Trigger.new[i].addError(e.getMessage());
     }
    }

    //Methods to Submit the record
    public void submitForApproval(ADR__c opp)
    {
        // Create an approval request for the ADR__c
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval automatically using Trigger');
        req1.setObjectId(opp.id);
        req1.setNextApproverIds(ADRUserIDVar);

        Approval.ProcessResult result = Approval.process(req1);
    }
    //Get ProcessInstanceWorkItemId using SOQL
    public Id getWorkItemId(Id targetObjectId)
    {
        Id retVal = null;

        for(ProcessInstanceWorkitem workItem  : [Select p.Id from ProcessInstanceWorkitem p
            where p.ProcessInstance.TargetObjectId =: targetObjectId])
        {
            retVal  =  workItem.Id;
        }

        return retVal;
    }
}