+ Start a Discussion
LALALALALALA 

trigger update issue

I have 2 custom object which are OrderFromWeb and PayPal Record.

my question is when Order Number in PayPal Record == Order Number in OrderFromWeb, then update Payment Status from Pending to Paid in OrderFromWeb.

my code:
trigger UpdatePaymentStatus on PayPal_Record__c (after update) {
    
    
   OrderFromWeb__c ofw;
    for(PayPal_Record__c pr: Trigger.new )
    {
         
             if(ofw.OrderNumber__c ==pr.OrderNumber__c)
            {
               ofw.PaymentStatus__c='Paid';
            }
     
    }
    
}
 
Best Answer chosen by LALALA
ManojjenaManojjena
Hi ,
try with below code .
Try  to change the exact api name of the order number in Pay_Pal_Record__c .
trigger UpdatePaymentStatus on PayPal_Record__c (after  Insert) {
   List<OrderFromWeb__c> orderFormNeedsToUpdate=new List<OrderFromWeb__c>();
   Set<String> orderNumberSet=new Set<String>();
   for(PayPal_Record__c pr: Trigger.new ){
       if(pr.OrderNumber__c != null)
	   orderNumberSet.add(pr.OrderNumber__c);
	}
	if(!orderNumberSet.isEmpty())
		for(OrderFromWeb__c ofw : [SELECT Id,OrderNumber__c FROM OrderFromWeb__c  WHERE OrderNumber__c IN : orderNumberSet LIMIT 50000]){
			  ofw.PaymentStatus__c='Paid';
			  orderFormNeedsToUpdate.add(owf);
		}try{
		  update orderFormNeedsToUpdate;
		}catch(DmlException de){
		  System.debug(de);
		}
}

One thing is there any record with null value in OrderNumber__c ?

 

All Answers

ManojjenaManojjena
Hey ,

Is there any relationship between these two objects,Please post the relationship name if possible .
LALALALALALA
@Manoj Kumar jena

Hi, what you mean by relationship?    i just create two custom object, but should be no relationship between them,,,,,you mean the relationship like look up or master relation???
LALALALALALA

@Manoj Kumar jena

two object, data of one object is from a email, other one from another email, those two have the same custom field which is OrderNumber, so when Order Number from PayPal Record object == Order Number from OrderFromWeb, then update paymentStatus from pending to paid in OrderFromWeb Object.
ManojjenaManojjena
Hi ,

Ok fine ,Please clarify my queries below .


1.You have two data sheet you need to upload you need to write trigger if order number match then you need to set  the field PaymentStatus__c to paid on OrderFromWeb__c record  .
2.Which record you will upload first is there any sequence ?Means PayPal_Record__c  first or OrderFromWeb__c first ?
3.Is OrderNumber__c in  each object is unque and read only ?why asking if some one will change then what will happen ?
 
LALALALALALA
@Manoj Kumar jena
1 is yes.
2 because salesforce record info into OrderFromWeb first, so should be upload OrderFromWeb first.
3 I set up as defalut, so should be a read only.
ManojjenaManojjena
Hi ,

I think the  trigger you need to write on insert evet not update .
trigger UpdatePaymentStatus on PayPal_Record__c (after  Insert) {
   List<OrderFromWeb__c> orderFormNeedsToUpdate=new List<OrderFromWeb__c>();
   List<OrderFromWeb__c> ordFormList=[Select OrderNumber__c FROM OrderFromWeb__c  Order By createdDate LIMIT 50000];
    for(PayPal_Record__c pr: Trigger.new ){
	    for(OrderFromWeb__c ofw : ordFormList){
		   if(ofw.OrderNumber__c ==pr.OrderNumber__c) {
               ofw.PaymentStatus__c='Paid';
			   orderFormNeedsToUpdate.add(owf);
            }
		}
    }
	try{
	  update orderFormNeedsToUpdate;
	}catch(DmlException de){
	  System.debug(de);
	}
	
}

If possible you can add filter in line number 3 if you want to check only those record of OrderFromWeb__c  you have inserted today .
If you want to check with all record or only recently created record .
LALALALALALA
Hi @Manoj Kumar jena
Thank you you example code, but still can't change Payment Status__c from Pending to Paid in OrderFromWeb.
seems like didn't trigge this trigger?   but no any issue on code......i dont know why
LALALALALALA
THIS IS my problems,,,,about ID

04:45:04:129 EXCEPTION_THROWN [13]|System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
LALALALALALA
FATAL_ERROR Trigger.PaymentStatusUpdate: line 5, column 1

 FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object


that is the problems from my debug
Pankaj_GanwaniPankaj_Ganwani
Hi,

The exception is caused by not giving the Id of OrderFromWeb while updating the record. FYI : For performing update and delete DMLs on any record we must supply Id. Please try below mentioned code instead:
 
trigger UpdatePaymentStatus on PayPal_Record__c (after  Insert) {

   List<OrderFromWeb__c> orderFormNeedsToUpdate=new List<OrderFromWeb__c>();

   List<OrderFromWeb__c> ordFormList=[Select Id, OrderNumber__c FROM OrderFromWeb__c  Order By createdDate LIMIT 50000];

    for(PayPal_Record__c pr: Trigger.new ){

        for(OrderFromWeb__c ofw : ordFormList){

           if(ofw.OrderNumber__c ==pr.OrderNumber__c) {

               ofw.PaymentStatus__c='Paid';

               orderFormNeedsToUpdate.add(owf);

            }

        }

    }

    try{

      update orderFormNeedsToUpdate;

    }catch(DmlException de){

      System.debug(de);

    }

     

}

 
ManojjenaManojjena
Hi ,
 try with below code it will be better then iterating for loop inside for loop ,I think it will solve your problem .
trigger UpdatePaymentStatus on PayPal_Record__c (after  Insert) {
   List<OrderFromWeb__c> orderFormNeedsToUpdate=new List<OrderFromWeb__c>();
   Set<String> orderNumberSet=new Set<String>();
   for(PayPal_Record__c pr: Trigger.new ){
	   orderNumberSet.add(pr.OrderNumber__c);
	}
	if(!orderNumberSet.isEmpty())
		for(OrderFromWeb__c ofw : [SELECT Id,OrderNumber__c FROM OrderFromWeb__c  WHERE OrderNumber__c IN : orderNumberSet LIMIT 50000]){
			  ofw.PaymentStatus__c='Paid';
			  orderFormNeedsToUpdate.add(owf);
		}try{
		  update orderFormNeedsToUpdate;
		}catch(DmlException de){
		  System.debug(de);
		}
}

 
LALALALALALA
@Manoj Kumar jena

still have problems,,,,,,when i debug will have 4 problems  and in my OrderFromWeb object still didn't change my paymentstatus from pending to paid,

problems:
20:01:22:174 FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object
20:01:22:000 FATAL_ERROR Trigger.PaymentStatusUpdate: line 5, column 1
20:01:22:174 FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object
20:01:22:000 FATAL_ERROR Trigger.PaymentStatusUpdate: line 5, column 1

 
ManojjenaManojjena
Hi ,
try with below code .
Try  to change the exact api name of the order number in Pay_Pal_Record__c .
trigger UpdatePaymentStatus on PayPal_Record__c (after  Insert) {
   List<OrderFromWeb__c> orderFormNeedsToUpdate=new List<OrderFromWeb__c>();
   Set<String> orderNumberSet=new Set<String>();
   for(PayPal_Record__c pr: Trigger.new ){
       if(pr.OrderNumber__c != null)
	   orderNumberSet.add(pr.OrderNumber__c);
	}
	if(!orderNumberSet.isEmpty())
		for(OrderFromWeb__c ofw : [SELECT Id,OrderNumber__c FROM OrderFromWeb__c  WHERE OrderNumber__c IN : orderNumberSet LIMIT 50000]){
			  ofw.PaymentStatus__c='Paid';
			  orderFormNeedsToUpdate.add(owf);
		}try{
		  update orderFormNeedsToUpdate;
		}catch(DmlException de){
		  System.debug(de);
		}
}

One thing is there any record with null value in OrderNumber__c ?

 
This was selected as the best answer
LALALALALALA
@Manoj Kumar jena
yes,,,have possible is nullvalue 
ManojjenaManojjena
Ok Fine ,I think now you will not get the error ,Please try the above code and let me know any issue .
LALALALALALA
@Manoj Kumar jena
I will say Thank you first, because you have good patient..

Then, I debug these code still the same problems...i don't know why....

this is my debug code:
PayPal_Record__c record = new PayPal_Record__c(OrderNumber__c = '2000157');
insert record;     

Is this correct to excute this trigger?

In my opinion, I think your code already change the paymentStatus from pendding to paid in list,,,but in update this step have problems,,,,,it can't put these info to database, so finally still didn't change anything 
LALALALALALA

@Manoj Kumar jena 


Oh god!!!!  It works!!!!    Thank you very much....

you know it is my problems........In my Object of OrderFromWeb have one didn't have ordernumber which mean in this field it is null!!!   so that's why can't update....