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
LALALALALALA 

trriger and can't get data which is what i want

my question is:
I have one custom obj - orderFromweb     : It include such ordernumber, paymentstatus, paymentmethod.......
I have another custom object - orders       : It only include ordernumber, and company info from contact 
I wanna through trigger to automatically create an opportunity but i meet some problems

this is my code:

List<Opportunity> newOpp = new List<Opportunity>();
    Opportunity op = new Opportunity();
    OrderFromWeb__c ofw = [select Id, OrderNumber__c, PaymentMethod__c from OrderFromWeb__c limit 1];
    Account acc = new Account();
    
    for(Orders__c Order : Trigger.new) {
        if(ofw.PaymentMethod__c == 'PayPal Standard' && ofw.OrderNumber__c == order.Name) {
            op.Name = date.Today()+'_W:'+ Order.Name;
        }
        else {                                                                                                    
            op.Name = date.Today()+'_:PO' + Order.Name;
        }

       -----question is:  'if' conditon doesn't work, because Paymentmethod is PayPal Standard, so should be running  op.Name = date.Today()+'_W:'+ Order.Name;   but it running code in 'else',,,,,I think because system doesn't know which ofw and orders to do the comparing, so is that i set Id have problems???  I don't know,


 if(ofw.PaymentMethod__c == 'PayPal Standard' && ofw.OrderNumber__c == order.Name)
        {
            op.PO_Number__c = Order.Name;
        }
else
{
   op.PO_Number__c = ofw.PO_number__c
}
same as last one,, running 'else' not right one,,,,,why?? 


thank you anyone can help me
Best Answer chosen by LALALA
pconpcon
think your issue is that youare not getting the OrderFromWeb__c associated with your Orders.  The code below queries all of your OrderFromWeb__c objects for the Order__c.Name that are in the trigger.  Then builds a map of them and uses them in your if/else condition.  This then builds up a list of Opportunities for you to insert.  Let me know if this helps you out.
 
Set<String> orderNames = new Set<String>();

for (Orders__c order: Trigger.new) {
    orderNames.add(order.Name);
}

Map<String, OrderFromWeb__c> orderFromWebMap = new Map<String, OrderFromWeb__c>();

for (OrderFromWeb__c ofw: [
    select OrderNumber__c,
        PaymentMethod__c
    from OrderFromWeb__c
    where OrderNumber__c in :orderNames
]) {
    orderFroWebMap.put(ofw.OrderNumber__c, ofw);
}

List<Opportunity> opps = new List<Opportunnity>();

for (Orders__c order : Trigger.new) {
    OrderFromWeb__c ofw = orderFromWebMap.get(order.Name);
    Opportunity op = new Opportunity();

    if (
        ofw != null &&
        ofw.PaymentMethod__c == 'PayPal Standard'
    ) {
        op.Name = date.Today() + '_W:' + Order.Name;
    } else {
        op.Name = date.Today() + '_:PO' + Order.Name;
    }

    opps.add(op);
}

insert opps;

NOTE: This code has not been tested and may contain typographical / logical errors.

All Answers

pconpcon
think your issue is that youare not getting the OrderFromWeb__c associated with your Orders.  The code below queries all of your OrderFromWeb__c objects for the Order__c.Name that are in the trigger.  Then builds a map of them and uses them in your if/else condition.  This then builds up a list of Opportunities for you to insert.  Let me know if this helps you out.
 
Set<String> orderNames = new Set<String>();

for (Orders__c order: Trigger.new) {
    orderNames.add(order.Name);
}

Map<String, OrderFromWeb__c> orderFromWebMap = new Map<String, OrderFromWeb__c>();

for (OrderFromWeb__c ofw: [
    select OrderNumber__c,
        PaymentMethod__c
    from OrderFromWeb__c
    where OrderNumber__c in :orderNames
]) {
    orderFroWebMap.put(ofw.OrderNumber__c, ofw);
}

List<Opportunity> opps = new List<Opportunnity>();

for (Orders__c order : Trigger.new) {
    OrderFromWeb__c ofw = orderFromWebMap.get(order.Name);
    Opportunity op = new Opportunity();

    if (
        ofw != null &&
        ofw.PaymentMethod__c == 'PayPal Standard'
    ) {
        op.Name = date.Today() + '_W:' + Order.Name;
    } else {
        op.Name = date.Today() + '_:PO' + Order.Name;
    }

    opps.add(op);
}

insert opps;

NOTE: This code has not been tested and may contain typographical / logical errors.
This was selected as the best answer
LALALALALALA
@pcon
Thanks !! It works....
I have another question is I hace a look up field which name is Account Name, there has a relationship between account and opportunity,,,, and in opportunity have a field name is Account Name but no API, which means I cannot get Like op.AccountName,,,only have AccountId,  So how can I get Account Name from Account to fill this field in opportunity???  thanks 
pconpcon
I'm assuming you have an Account lookup on either the Order__c object or the OrderFromWeb__c object.  While this may have a label of Account Name it is stored as an Id to the Account object.  So you would need to add the Account__c field to your OrderFromWeb__c SOQL query (line 10-11) and add it to your Opportunity in the for loop (line 22) or you can if it's on your Order__c object you can just reference it directly.
 
Opportunity op = new Opportunity();

if (ofw != null) {
    op.AccountId = ofw.Account__c;
}

or
 
Opportunity op = new Opportunity(
    AccountId = order.Account__c
);

 
LALALALALALA
@pcon
Hi, only opportunity have AccountId, and Account and Contact have it. order and ofw doesn't has...

I tried op.AccountId = order.Contacts__r.AccountId   still doesn't working....   don't know why 

by the way, you are so nice,,,thank you help:)
LALALALALALA
@pcon
Or do I need set up a look up relationship between Orders and Account? or Contacts?   to make easier to get Account Name to fill in to opportunity??
LALALALALALA
@pcon
Account acct = new Account(name = 'Test');
    insert acct;
    ID acctID = acct.Id;

opportunity opp = new opportunity(AccountId = acctID);


I know it works,,,but the element of Name is given by me---'Test',,,,,I want name can automatically grab data from account, so what kind of query i need write?

pconpcon
If all you have is a contact on your Order__c object you'll need to build up a list of contact ids, then query their AccountIds.  Then use that in your opportunity creation
 
Set<String> orderNames = new Set<String>();
Set<Id> contactIds = new Set<Id>();

for (Orders__c order: Trigger.new) {
    orderNames.add(order.Name);
    contactIds.add(order.Contact__c);
}

Map<String, OrderFromWeb__c> orderFromWebMap = new Map<String, OrderFromWeb__c>();

for (OrderFromWeb__c ofw: [
    select OrderNumber__c,
        PaymentMethod__c
    from OrderFromWeb__c
    where OrderNumber__c in :orderNames
]) {
    orderFroWebMap.put(ofw.OrderNumber__c, ofw);
}

Map<Id, Contact> contactMap = new Map<Id, Contact>([
    select AccountId
    from Contact
    where Id in :contactIds
]);

List<Opportunity> opps = new List<Opportunnity>();

for (Orders__c order : Trigger.new) {
    OrderFromWeb__c ofw = orderFromWebMap.get(order.Name);
    Opportunity op = new Opportunity();

    if (
        ofw != null &&
        ofw.PaymentMethod__c == 'PayPal Standard'
    ) {
        op.Name = date.Today() + '_W:' + Order.Name;
    } else {
        op.Name = date.Today() + '_:PO' + Order.Name;
    }   

    if (contactMap.containsKey(order.Contact__c)) {
        op.AccountId = contactMap.get(order.Contact__c).AccountId;
    }

    opps.add(op);
}

insert opps;

 
LALALALALALA
@pcon

WoW!!!  That is wonderful!!! You are so helpful!!

I need tell everyone what you answerd that was all amazing:)
Thank you so much.

Could i ask you aonther problems??   
If I need according to this opportunity to create a quote and quote line item,,,what should I do? do I need create a new trigger for these or just write below this code of opportunity?   
can you give me some Idea or some example of code???  thank you so much!!

I wanna say you are good guy and I also learn alot from you:)
LALALALALALA
@pcon
please:)
pconpcon
It would depend.  If you are suppose to create a Quote and QuoteLineItem for all Opportunities created then you'll want to do a new trigger on the Opportunity object.  If you are suppose to create a Quote and QuoteLineItem only for these Opportunities then you'll want to append the code to the bottom of the Orders__c trigger.

Sorry for the delay.  The holiday weekend has put me behind this week.