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
Swayam PatiSwayam Pati 

Trigger needs modification: Help

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

for (Account act: Trigger.new) {

Update act.RemitfromUsedStatus == True from act

where act.Enterprise_ID__C in (select Remit_From__c from Account)

}

Please help me modify the above code in trigger which i wrote in simple SQL. I want to update RemitfromUsedStatus == True  on account object when account Enterprise_ID is a RemitFrom ID( RemotFrom_ID is a look up to account object on enterprise ID) for some records.
Best Answer chosen by Swayam Pati
ManojjenaManojjena
Hi Swayam,

Try with belwo code it will help !!
Trigger RemitStatusUpdate on Account(before insert,before update){
    Set<Id> accIdSet=new Set<Id>();
    for(Account acc :Trigger.new){
       if(acc.Remit_From__c != null){
         acc.RemitfromUsedStatus__c=false;
         accIdSet.add(acc.Remit_From__c);
       }
    }
    if(!accIdSet.isEmpty()){
       List<Account> remitFromAccListToUpdate=new List<Account>();
       for(Id accId :accIdSet){
         Account ac=new Account(Id=accId,RemitfromUsedStatus__c=True);
         remitFromAccListToUpdate.add(ac);
       }
       if(!remitFromAccListToUpdate.isEmpty()){
         try{
            update remitFromAccListToUpdate;
         }catch(DmlException de ){
           System.debug(de);
         }
       }
    }
}

Let me know if it helps !!
Thanks
Manoj

All Answers

ManojjenaManojjena
Hi Swayam,
Try with belwo code it will help !!
trigger UpdateRemitUsedStatus on Account (before insert, before update) {
for (Account act: Trigger.new) {
  if(act.Enterprise_ID__C != nulll){
    act.RemitfromUsedStatus == True ;
 }
}
If it will not help then explain your requirment so that I can help you .
For some concepts of trigger check belwo link it will help !!
http://manojjena20.blogspot.in/2013/03/manojs-trigger-blog.html
Let me know if it helps !!!
Thanks
Manoj
SS KarthickSS Karthick

Hi Swayam Pati,

I hope the below code snippet will help you.
 

trigger UpdateRemitUsedStatus on Account (before insert, before update) {
	if(trigger.isBefore && trigger.isUpdate){
		for (Account act: Trigger.new) {
		  if(act.Enterprise_ID__C==act.RemotFrom_ID){
			act.RemitfromUsedStatus = true ;
		}
	}	
}

Thanks,
Karthick.S
Swayam PatiSwayam Pati
Manoj/Kartik,

Appreciate your reply. But unfortunately i think both of your code is not fitting into my requirement. Let me explain with Example.
EnterpriseID     RemitFromID(look up to EnterpriseID)      RemitfromUSed Status
------------- --------------------------------------------------------- ------------------------------------
2000

 
Swayam PatiSwayam Pati
Appreciate your reply. But unfortunately i think both of your code is not fitting into my requirement. Let me explain with Example.
EnterpriseID     RemitFromID(look up to EnterpriseID)      RemitfromUSed Status
------------- --------------------------------------------------------- ------------------------------------
2000                    3000                                                        False
3000                     -                                                              True
4000                     -                                                              False
5000                    3000                                                        False
Basically Remitfrom is an account which Pays of other account. So i want to know who are the Remitfrom or Payer for other account, then make that Remitfromusedstatus True.

Manoj, in your code If enterpriseID is null then Make remitfromused status = True, i can make that in workflow. I don't need trigger.
Kartik, in your code if remitfromID = Enterprise iD then make status true. that also can be done in workflow.

I am specifically looking for trigger because this can't be done in formula or in workflow. If i am 2nd record which i want to make true, i want  put the logic that find this enterpriseID (3000) which is a RemitfromID from some other records, then make that record's RemitfromStatus = True.

Please help me in this trigger as i am very new to writing codes.
I really appreciate your help.

Thanks
Swayam PatiSwayam Pati
I tried to modify it looking at your code. is is correct?

trigger UpdateRemitUsedStatus on Account (before insert, before update) {

if(trigger.isBefore && trigger.isUpdate){

for (Account act: Trigger.new) {

if(act.Enterprise_ID__C in (select act.RemotFrom_ID from acc )){

act.RemitfromUsedStatus = true ;

}
}
}
}
Swayam PatiSwayam Pati
I also tried something like below but getting error. Please help.
trigger UpdateRemitUsedStatus on Account (before insert, before update) {
if(trigger.isBefore && trigger.isUpdate){
for (Account act: Trigger.new) {

        if(act.Enterprise_ID__c = Null){
              interger s = 0;
             s = [select count() from account t where t.Remit_From__c = act.Enterprise_ID__c];
                            if (s > 0)
                                {
                                    act.RemitfromUsedStatus = true ;
                                }
                                      }
                                }
ManojjenaManojjena

Hi Swayam,

Still your explanation is not clear to give you solution .What I understand is In account you have 3 Fields

1.Remit_From__c
2.RemitfromUsedStatus __c
3.Enterprise_ID__c

Remit_From__c is the look up to account means to other account .Each account have two  field called Enterprise_ID__c && RemitfromUsedStatus __c    ,Here you want to write trigger in account to check if parent account 's Enterprise_ID__c is equal with child then you want RemitfromUsedStatus __c to true .else false .Correct me if I am wrong .
Else please explain your requirment clearly .
 

SS KarthickSS Karthick
Hi Swayam Pati,
      As per my understanding the below code will help you.
 
trigger UpdateRemitUsedStatus on Account (before insert, before update) {
	if(trigger.isBefore && trigger.isUpdate){
		List<Account> accList=[select id,Remit_From__c from account where isParent=true];
		for (Account act: Trigger.new) 
			if(accList.size()>0)
			for(Account parentAcc : accList){
				if(act.Enterprise_ID__C==parentAcc.RemotFrom_ID)
					act.RemitfromUsedStatus = true ;
			}
	}	
}

Thanks,
Karthick.S
Swayam PatiSwayam Pati
Hi,
Thanks for your reply. let me explain the requirement clearly. I have account object which has 3 fields
1.Enterprise_ID__c (Unique)
2.RemitfromUsedStatus __c
3.Remit_From__c (look up to Enterprise_ID__c)
Now there is no parent child relationship, it is more of account -payer relationship. We have otherfields to establish parent-child relationships. So one account may be a payer for another account but not parent.
Here, Remit_From__c accounts are payer of Enterprise_ID_c accounts, If Remit_From__c on any record is blank that means two cases
1- Either it pays for itself ( like the account 4000 above)
2- Or the account is a Remit_From__c for some other accounts (like the account 3000 above)
So i want the accounts which are Remit_from_c for some otheraccounts, in those records Remitfromusedstatus__c to be populated as 'True'.
So lets say there is a account with 'Enterprise_Id_c' has a value 7000 and it's Remifrom_C is blank, and 7000 has not been a payer for any other account ,that means it pays for itself (So remitifromusedstatus = false). Now a user goes and create a new account record with 'Enterprise_id_c' as 8000 and select "Remitfrom_c on that record as '7000', then on record where enterprise_id_c is 7000, on that record the Remitfromusedstatus_c should be changed as 'True' thru trigger. Record with with 'Enterprise_id_c' as 8000 Remitfromusedstatus still should be 'False'.

So bottomline is any Enterprise_ID that is being used as a payer (Remit_From_c) for any other Enterprise_Id, then those Payer should be flagged with a Status =' 'True'/

Hope this expains it.
SS KarthickSS Karthick
Hi Swayam,

            You want to update status of the records which are used for another records of same object.
The following code will suit your requirement.
 
trigger UpdateRemitUsedStatus on Account (before insert, before update) {
	if(trigger.isBefore && trigger.isUpdate){
		List<Account> accList=[select id,Enterprise_Id_c,RemitfromUsedStatus__c from account where id!=null];
		List<Account> payerAccList = new List<Account>();
		for (Account act: Trigger.new){
			if(accList.size()>0)
				for(Account oldAcc : accList){
					if(act.Remit_From__c == oldAcc.Enterprise_Id_c){
						oldAcc.RemitfromUsedStatus__c=true;
						payerAccList.add(oldAcc);
					}
				}
		}
		
		//Update remittance status in payer account list records
		update payerAccList;
	}	
}


Please mark this post as solved . 

Thanks, 
Karthick.S
 
Swayam PatiSwayam Pati
Hi Kartik,
Thank for helping me out but i still have issues. I am getting error with your code. Since i am new to trigger, not sure how to debug. THe error i get is -

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UpdateRemitUsed_Status caused an unexpected exception, contact your administrator: UpdateRemitUsed_Status: execution of BeforeUpdate caused by: System.StringException: Invalid id: 48000: Trigger.UpdateRemitUsed_Status: line 8, column 1

The other thing i believe which is not right in your code is : - You are holding acclist with list of records where remitfrom is not null.and asssigning those to OldAcc.
So to me this should be the code
if(act.Remit_From__c == oldAcc.Enterprise_Id_c){09                         oldAcc.RemitfromUsedStatus__c=true;

10                         payerAccList.add(oldAcc);
 
Swayam PatiSwayam Pati
The other thing i believe which is not right in your code is : - You are holding acclist with list of records where remitfrom is not null.and asssigning those to OldAcc.
So to me this should be the code:

trigger UpdateRemitUsed_Status on Account (before insert, before update) {
   if(trigger.isbefore && trigger.isUpdate){
       List<Account> accList=[select id,Enterprise_ID__c,RemitfromUsed_Status__c from account where id!=null];
        List<Account> payerAccList = new List<Account>();
        for (Account act: Trigger.new){
            if(accList.size()>0)
                for(Account oldAcc : accList){
                    if(act.Enterprise_ID__c == oldAcc.Remit_From__c){
                        act.RemitfromUsed_Status__c=true;
                        payerAccList.add(act);
                    }
                }
        }
//Update remittance status in payer account list records
        update payerAccList;
}}
Swayam PatiSwayam Pati
Change in the code are last 3 lines
                    for(Account oldAcc : accList){
                    if(act.Enterprise_ID__c == oldAcc.Remit_From__c){ 
                        act.RemitfromUsed_Status__c=true
                        payerAccList.add(act);
Swayam PatiSwayam Pati
I just want to make sure that you are update the payers with the status true and comparing payer.enterpriseid id= children.remitfrom_id
ManojjenaManojjena
HI Swayam,

Can you connect in skpe so that you can share your screen which wil be good to  check the relation ship and your requirment .
If possible connect me in my skype Id : manoj.jena19 .So that I will help you to resolve your issue .
 
Swayam PatiSwayam Pati
Thanks Manoj. That would be awesome. But I am in USA and in Pacific time zone. Let me know what time works for you. I will log in that time.
ManojjenaManojjena
Hi Swayam,

Try with belwo code it will help !!
Trigger RemitStatusUpdate on Account(before insert,before update){
    Set<Id> accIdSet=new Set<Id>();
    for(Account acc :Trigger.new){
       if(acc.Remit_From__c != null){
         acc.RemitfromUsedStatus__c=false;
         accIdSet.add(acc.Remit_From__c);
       }
    }
    if(!accIdSet.isEmpty()){
       List<Account> remitFromAccListToUpdate=new List<Account>();
       for(Id accId :accIdSet){
         Account ac=new Account(Id=accId,RemitfromUsedStatus__c=True);
         remitFromAccListToUpdate.add(ac);
       }
       if(!remitFromAccListToUpdate.isEmpty()){
         try{
            update remitFromAccListToUpdate;
         }catch(DmlException de ){
           System.debug(de);
         }
       }
    }
}

Let me know if it helps !!
Thanks
Manoj
This was selected as the best answer
Swayam PatiSwayam Pati
Thanks Manoj. It was a great help