• Nathan Prats 22
  • NEWBIE
  • 50 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 15
    Questions
  • 10
    Replies
Hi, 

I'm trying to update a field named Primary Partner Gross Margin on the opportunity object based on opportunity line items. 
The code compiles, but it doesn't update the field on update. Any idea why ? 
trigger OpportunityTriggers on Opportunity (before update) {
    
    // Quand 1 Opp a un Primary Partner Account Reseller, remplir le champs "Primary Partner Gross Margin" = Sum OpportunityLineItems Software  * Primary_Partner_Margin__c
    // C'est forcément before car on a besoin d'update des records, et quand on est en after, les records sont lock
    
    // Prendre la liste des opportunités pour lesquelles on va peupler Primary_Partner_Gross_Margin__c
    For (Opportunity opp : [SELECT Id,Primary_Partner_Gross_Margin__c,Primary_Partner_Margin__c 
                            FROM Opportunity
                            WHERE Primary_Partner_Role__c ='Reseller'
                            AND Deal_with_a_partner__c = 'Yes'
                            AND Id IN :Trigger.old
                           ]) {
                               
                               // Updater le champs Primary_Partner_Gross_Margin__c
                               Double GrossMargin = [SELECT TotalPrice 
                                                      FROM OpportunityLineItem
                                                      WHERE PricebookEntry.product2.Family='Software'
                                                      AND  OpportunityId =: opp.Id ][0].TotalPrice;
                               
                               
                               opp.Primary_Partner_Gross_Margin__c=GrossMargin*opp.Primary_Partner_Margin__c;
                               opp.Name = 'Worked';
                               
                           }
}

 
Hello, 

I created this trigger which set a share type for each new content document link. 
trigger ContentDocumentLinkTrigger on ContentDocumentLink (before insert) {
    
    for(ContentDocumentLink cdl: Trigger.new){
        cdl.shareType = 'I';
    } 
}
I tried to create this test class:
@isTest
public class ContentDocumentLinkTriggerTest {
    
    @isTest static void ContentDocumentLinkTriggerTest() {
        
        // Create a ContentVersion
        ContentVersion ContentDoc = new ContentVersion();
        ContentDoc.Title = 'My Doc';
        ContentDoc.ContentUrl= 'test.com';
        Insert ContentDoc;
        
        // Create a ContentDocumentLink
        ContentDocumentLink ContentDL = new ContentDocumentLink();
        ContentDL.ContentDocumentId = ContentDoc.Id;
        Insert ContentDL;
        
        // Verify the share type is = i
        System.assertEquals(ContentDL.ShareType,'I');
        
        
    }
}

But I receive this error: 
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, ContentDocument ID: id value of incorrect type: 0683E0000003EXVQA2: [ContentDocumentId]

Any idea why ? 
Hello, 

I have this code :
 
List<Account> AccList = [SELECT Id,OwnerId,Parent.OwnerId 
                         FROM Account 
                         WHERE Account_Owner_Parent_Owner__c = TRUE 
                         AND IsExcludedFromRealign = FALSE];

Integer X = [SELECT Count()
             FROM Account 
             WHERE Account_Owner_Parent_Owner__c = TRUE 
             AND IsExcludedFromRealign = FALSE];

do {
    for(Account Acc : AccList){ 
        Acc.OwnerId = Acc.Parent.OwnerId;
    }
        
        } while (X > 0);

update AccList;

but each time i run it, i have a Limit Usage for NS error. 

10:45:16:000 LIMIT_USAGE_FOR_NS   Maximum CPU time: 15118 out of 10000 ******* CLOSE TO LIMIT

What can be wrong ? 
Hello, 

I created this piece of code that reassigns all accounts to the right owner based on the account hierarchy. 
The issue is that this code reassigns 1 node at a time. 

I'd like it to do this until the following query returns 0 :
[SELECT Id,OwnerId,Parent.OwnerId FROM Account WHERE Account_Owner_Parent_Owner__c = TRUE AND IsExcludedFromRealign = FALSE]

Current code
global class ReassignAccounts implements Schedulable {
    
    global void execute(SchedulableContext ctx) {
        List<Account> AccList = [SELECT Id,OwnerId,Parent.OwnerId FROM Account WHERE Account_Owner_Parent_Owner__c = TRUE AND IsExcludedFromRealign = FALSE];
        
        for(Account Acc : AccList){    
            Acc.OwnerId = Acc.Parent.OwnerId ;
        }
        
        update AccList; }
    
}
Can I add a while [SELECT Id,OwnerId,Parent.OwnerId FROM Account WHERE Account_Owner_Parent_Owner__c = TRUE AND IsExcludedFromRealign = FALSE] > 0, do ... ? 

Thanks, 

Nathan
 
Hi, 

I have this code that doesn't work. I'm trying to convert it as a map as I read in the best practices that I should use maps. 
The custom field Contact_Owner_Account_Owner__c  returns TRUE when the ownerId is different from the account ownerId. 
List<Contact> ContList = [SELECT Id,OwnerId,Account.OwnerId,Contact_Owner_Account_Owner__c 

             FROM Contact 

             WHERE Contact_Owner_Account_Owner__c = TRUE

             AND AccountId != ''];



for(Contact Cont : ContList){   

  Cont.OwnerId = Cont.Account.OwnerId ;

}



update ContList;
Thanks for your help, 

Nathan
 
Hi, 

My need is quite simple. I need to create a button on the Quote object to send an email with the last attachment attached. 
I already created the button: 

/_ui/core/email/author/EmailAuthor?
p2_lkid=0032X000023Oapf&
rtype=003
&p3_lkid={!QuoteId}&
doc_id=AttachmentId&
&retURL=%2F0{!QuoteId}


I need to Push the AttachmentId To ParentId Custom Field after the Attachement is inserted with a Trigger. 
trigger PushAttachmentIdToParentIdCustomField on Attachment (After Insert) {
    
    

}
Only problem is... I have no idea where to start. 
Any help is more than welcome as I'm fairly new to Apex Development.

Nathan
 
Hi, 

We have 2 Account Record Types, Standard & Partner. I'm trying to create a simple Apex Trigger to prevent a sales rep to change the account record type from Standard to Partner if the account has related opportunities. 

I wrote this, but as I'm new to Apex I'm pretty sure it's plenty wrong. 

trigger PreventEnablePartner on Account (before update) {

For (Account acc : [SELECT Id,RecordTypeId
                    FROM Account
                    WHERE Id IN (SELECT AccountId FROM Opportunity)
                    AND RecordType.Name = 'Partner']) {
                        
Account oldAccount = Trigger.oldMap.get(acc.ID);

IF(acc.RecordTypeId != oldAccount.RecordTypeId ) {
    Trigger.New[0].addError('Cannot "Enable As Partner" an account with related opportunities.');}
    Else {Return;}
}
}

Do you have any idea ? 

Thanks,